Tag: python

协程与Python 3.5中未来/任务的区别?

假设我们有一个虚拟函数: async def foo(arg): result = await some_remote_call(arg) return result.upper() 有什么区别: coros = [] for i in range(5): coros.append(foo(i)) loop = get_event_loop() loop.run_until_complete(wait(coros)) 和: from asyncio import ensure_future futures = [] for i in range(5): futures.append(ensure_future(foo(i))) loop = get_event_loop() loop.run_until_complete(wait(futures)) 注意 :该示例返回结果,但这不是问题的重点。 当返回值很重要时,使用gather()而不是wait() 。 无论返回值如何,我正在寻找ensure_future()清晰度。 wait(coros)和wait(coros)两者都运行协程,那么何时以及为什么要将协程打包在ensure_future ? 基本上,使用Python 3.5的async运行一堆非阻塞操作的正确方法是什么? 额外的信用,如果我想批量打电话? 例如,我需要调用some_remote_call(…) 1000次,但我不想用1000个同时连接粉碎web服务器/数据库/等。 这是可行的线程或进程池,但有没有办法用asyncio做到这asyncio ?

确定Python所需最低版本的工具?

有没有类似于Pylint,会看Python脚本(或运行它),并确定每个行(或function)需要哪个版本的Python? 例如,理论用法: $ magic_tool <EOF with something: pass EOF 1: 'with' statement requires Python 2.6 or greater $ magic_tool <EOF class Something: @classmethod def blah(cls): pass EOF 2: classmethod requires Python 2.2 or greater $ magic_tool <EOF print """Test """ EOF 1: Triple-quote requires Python 1.5 of later 这样的事情可能吗? 我想最简单的方法是将所有Python版本放在光盘上,然后运行脚本,看看发生了什么错误。

编写Python 2.7代码尽可能接近Python 3.x语法

由于Django还不支持Python 3.x,因此我使用Python 2.7。 不过,我想继续前进,并尽可能熟悉新的Python 3.x语法。 这导致我的问题: 编写Python 2.7代码的最好方法是什么?它将尽可能与Python 3.x兼容? 我知道运行python -3会 警告2to3无法修复的Python 3.x不兼容问题。 不过,我仍然习惯使用Python3.7语法,而仍然使用Python2.7。 例如,似乎我应该使用以下导入到我的代码: from __future__ import print_function from __future__ import unicode_literals from __future__ import division from __future__ import absolute_import 上述四个__future__ import语句从Python 3.0开始是必需的,但在Python 2.7.3的文档27.11中描述的2.7中不需要。 未来的陈述定义 还有什么?

重新提高Pythonexception并保留堆栈跟踪

我试图捕捉一个线程中的exception,并重新在主线程中提出: import threading import sys class FailingThread(threading.Thread): def run(self): try: raise ValueError('x') except ValueError: self.exc_info = sys.exc_info() failingThread = FailingThread() failingThread.start() failingThread.join() print failingThread.exc_info raise failingThread.exc_info[1] 这基本上工作,并产生以下输出: (<type 'exceptions.ValueError'>, ValueError('x',), <traceback object at 0x1004cc320>) Traceback (most recent call last): File "test.py", line 16, in <module> raise failingThread.exc_info[1] 但是,例外的来源指向第16行,在那里发生了重新抬头。 原来的exception来自第7行。我如何修改主线程,以便输出结果如下: Traceback (most recent call last): File […]

如何识别脚本是否在tty上运行?

我希望我的脚本在交互式shell会话中执行不同的操作,并在redirectstdout(例如,通过pipe道连接到其他命令时)运行。 如何识别这两个脚本中的哪一个发生在Python脚本中? 现有程序中此类行为的示例:grep –color = auto在交互式shell中运行时突出显示匹配项,但不会在pipe理其他项时突出显示。

“from __future__ import braces”代码在哪里?

我想知道什么是在命令上执行的代码: >>> from __future__ import braces SyntaxError: not a chance 所以,由于python是开源的,我打开C:\Python27\Lib\__future__.py并查找。 令人惊讶的是,我发现那里没有处理导入braces模块。 所以,我的问题是,处理这个代码在哪里? 当我运行这个命令时会发生什么?

运营商的非常奇怪的行为是与方法

为什么第一个结果是False ,如果不是True ? >>> from collections import OrderedDict >>> OrderedDict.__repr__ is OrderedDict.__repr__ False >>> dict.__repr__ is dict.__repr__ True

Vim,Python和Django自动完成(pysmell?)

有谁知道如何设置自动完成与python,django和vim很好地工作? 我一直在尝试使用pysmell,但我似乎无法正确设置(或者我不知道它是如何工作的)。 现在,我在django目录下运行pysmell(我正在使用trunk),并将结果标签移动到我的项目目录中,然后在项目目录中运行pysmell。 但是,Vim并没有拿起django标签,而且他们也没有自动完成。 有谁知道如何在vim中设置自动完成function,以便在我自己的代码中完成long django函数(如get_object_or_404)以及类/函数? 我在谷歌上search,但没有find任何好的资源。 谢谢。

为什么Python的itertools.permutations包含重复项? (当原始列表重复时)

普遍认为,n个不同符号的列表有n! 排列。 但是,当符号不明确时,在math和其他地方最常见的惯例似乎是只计算不同的排列。 因此,列表[1, 1, 2] 1,1,2 [1, 1, 2]的排列通常被认为是 [1, 1, 2], [1, 2, 1], [2, 1, 1] 。 事实上,下面的C ++代码正好打印出这三个: int a[] = {1, 1, 2}; do { cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; } while(next_permutation(a,a+3)); 另一方面,Python的itertools.permutations似乎打印别的东西: import itertools for a in itertools.permutations([1, 1, 2]): print a 这打印 (1, 1, 2) (1, 2, 1) (1, 1, […]

这两个python shebang有什么区别

我曾经使用shebang #!/usr/bin/env python 什么时候使用更好? #!/usr/bin/python 他们之间的确切区别是什么?