标签: iterable-unpacking

使用C/C++从库中解压缩可执行文件

我正在开发一个在业务过程中使用一个或多个帮助程序可执行文件的库.我当前的实现要求用户在已知位置的系统上安装帮助程序可执行文件.要使库正常运行,帮助应用程序必须位于正确的位置并且是正确的版本.

我想删除以上述方式配置系统的要求.

有没有办法捆绑库中的帮助程序可执行文件,以便它可以在运行时解压缩,安装在临时目录中,并在一次运行期间使用?在运行结束时,可以删除临时可执行文件.

我已经考虑过自动生成一个包含unsigned char数组的文件,该数组包含可执行文件的文本.这将在编译时完成,作为构建过程的一部分.在运行时,此字符串将写入文件,从而创建可执行文件.

如果不将可执行文件写入磁盘(可能是某种RAM磁盘),是否可以执行此类任务?我可以设想某些病毒扫描程序和其他安全软件反对这种操作.还有其他问题我应该担心吗?

该库正在使用C/C++开发,以便在Windows和Linux上进行跨平台使用.

c c++ filesystems cross-platform iterable-unpacking

2
推荐指数
1
解决办法
1023
查看次数

如何选择函数返回的x-tuple的某些元素?

我是Python的新手.考虑str.partition()返回3元组的函数.如果我只对这个元组的元素0和2感兴趣,那么从这样的元组中选择某些元素的最佳方法是什么?

我现在可以做到:

# Introduces "part1" variable, which is useless
(part0, part1, part2) = str.partition(' ')
Run Code Online (Sandbox Code Playgroud)

要么:

# Multiple calls and statements, again redundancy
part0 = str.partition(' ')[0]
part2 = str.partition(' ')[2]
Run Code Online (Sandbox Code Playgroud)

我希望能够做到这样的事情,但不能:

(part0, , part2) = str.partition(' ')
# Or:
(part0, part2)   = str.partition(' ')[0, 2]
Run Code Online (Sandbox Code Playgroud)

python tuples list iterable-unpacking

2
推荐指数
1
解决办法
973
查看次数

python 2.5中的格式字符串解压缩列表

有没有办法在Python 2.5中做这样的事情:

b = ('{!s}'*3)
b.format(*[i for i in xrange (3)])
Run Code Online (Sandbox Code Playgroud)

因为这不起作用:

b = ('%s'*3)
b % (*[i for i in xrange (3)])
Run Code Online (Sandbox Code Playgroud)

python tuples string-formatting iterable-unpacking

2
推荐指数
1
解决办法
921
查看次数

如何使用可选参数和参数解包来定义方法?

我有一个关于如何使用Python中的可选参数和*args参数来定义和调用方法的问题.例如,

def test_func(arg1, optional_arg=None, *args):
    ...
Run Code Online (Sandbox Code Playgroud)

我希望能够使用以下任一语句调用该方法:

test_func("foo", arg1, arg2, ..., optional_arg="bar")
...
test_func("foo", arg1, arg2, ...) 
# arg1, arg2 ... belongs to *args;
# optional_arg is unset (defaults to None)
Run Code Online (Sandbox Code Playgroud)

这有可能吗,或者我在调用这样的方法时总是要将optional_arg设置为某个东西吗?即我只想在调用函数时专门编写optional_arg = ...时设置optional_arg.

python optional-parameters python-2.7 iterable-unpacking

2
推荐指数
1
解决办法
4222
查看次数

q,w = 1,2如果1 <2,则为2; ValueError:要解压缩的值太多.为什么?

正如标题中所述

q, w = 1, 2 if 1 < 2 else 2, 1
ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)

这里发生了什么??

python if-statement iterable-unpacking

2
推荐指数
1
解决办法
160
查看次数

使用map()时使用*idiom解包参数

在Python中*使用map内置函数时,有没有办法用习语解包元组?

理想情况下,我想做以下事情:

def foo(a, b):
  return a**2 + b

x = [(1,2), (3,4), (5,6)]

results = map(foo, *x)
Run Code Online (Sandbox Code Playgroud)

结果会相等 [3, 13, 31]

python map iterable-unpacking

2
推荐指数
1
解决办法
86
查看次数

使用样式拆开多行

在Python中,我想做这样的事情

an_explicit_variable_name, another_explicit_variable_name, an_even_more_explicit_variable_name = function(foo)
Run Code Online (Sandbox Code Playgroud)

但我也希望这个可读并适合几条短线而不是一条非常长的线.我没有在PEP 08找到任何帮助.这个问题有点关系,但答案并不是我想要的.


就线长而言,这是我能想到的最好的,但我并不喜欢使用a[0]等等.

a = function(foo)
an_explicit_variable_name = a[0]
another_explicit_variable_name = a[1]
an_even_more_explicit_variable_name = a[2]
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为第二行仍然太长

_ = function(foo)
an_explicit_variable_name, another_explicit_variable_name, an_even_more_explicit_variable_name = _
Run Code Online (Sandbox Code Playgroud)

我应该将我在多行中声明的变量分开吗?如果是这样,我该如何缩进?

an_explicit_variable_name, \
    another_explicit_variable_name, \
    an_even_more_explicit_variable_name \
    = function(foo)


an_explicit_variable_name, \
        another_explicit_variable_name, \
        an_even_more_explicit_variable_name \
    = function(foo)
Run Code Online (Sandbox Code Playgroud)

在这种情况下采用什么样的风格?

python coding-style python-2.7 iterable-unpacking

2
推荐指数
1
解决办法
799
查看次数

如何使用理解减少dict值中的集合?

我有

x = {'a':set([1]) , 'b':set([2]), 'c':set([3]) }
Run Code Online (Sandbox Code Playgroud)

保证集合中只有一个元素.我需要将其转换为

{'a': 1, 'c': 3, 'b': 2}
Run Code Online (Sandbox Code Playgroud)

以下作品:

x1 = {k:x[k].pop() for k in x.keys()}  OR
x1 = {k:next(iter(x[k])) for k in x.keys()}
Run Code Online (Sandbox Code Playgroud)

但我不喜欢它,因为pop()这里正在修改原始集合.我需要帮助.

  • 如何 在理解中使用此处提到的拆包.
  • 有什么办法,我可以用functools.reduce它.
  • 什么可以是更好或Pythonic的方式来做这个整体?

python dictionary python-2.7 functools iterable-unpacking

2
推荐指数
1
解决办法
234
查看次数

如何解压缩对象,因为它是for循环中的元组?

我尝试创建以下代码:

class Test(object):

    def __init__(self, arg):
        self.arg1 = arg + 1
        self.arg2 = arg + 2
        self.arg3 = arg + 3

    def __iter__(self):
        return self

    def __next__(self):
        yield self.arg1, self.arg2, self.arg3


test_list = [Test(0), Test(1), Test(2)]

for arg1, arg2, arg3 in test_list:
    print('arg1', arg1, 'arg2', arg2, 'arg3', arg3)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行时,python说:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    for arg1, arg2, arg3 in test_list:
ValueError: too many values to unpack (expected 3)
Run Code Online (Sandbox Code Playgroud)

我可以通过手工拆包来解决它:

class Test(object):

    def __init__(self, arg): …
Run Code Online (Sandbox Code Playgroud)

python iterator generator iterable-unpacking python-3.6

2
推荐指数
2
解决办法
893
查看次数

如何更优雅地将具有一个键值对的dict分解为两个变量?

目前,我正在使用此:

d = {'a': 'xyz'}
k, v = list(*d.items())
Run Code Online (Sandbox Code Playgroud)

这里需要加星号的表达式,因为省略它会使列表函数/构造函数返回带有单个元组的列表,该元组包含键和值。

但是,我想知道是否有更好的方法可以做到这一点。

python dictionary python-3.x iterable-unpacking

2
推荐指数
3
解决办法
78
查看次数