I was writing a new random number generator for numpy that produces random numbers according to an arbitrary distribution when I came across this really weird behavior: this is test.pyx #cython: boundscheck=False #cython: wraparound=False import numpy as np cimport numpy as np cimport cython def BareBones(np.ndarray[double, ndim=1] a,np.ndarray[double, ndim=1] u,r): return u def UntypedW
我正在为numpy编写一个新的随机数生成器,当我遇到这种非常奇怪的行为时,根据任意分布产生随机数: 这是test.pyx #cython: boundscheck=False #cython: wraparound=False import numpy as np cimport numpy as np cimport cython def BareBones(np.ndarray[double, ndim=1] a,np.ndarray[double, ndim=1] u,r): return u def UntypedWithLoop(a,u,r): cdef int i,j=0 for i in range(u.shape[0]): j+=i
I've come across at least three ways to print to stderr: import sys print >> sys.stderr, 'spam' sys.stderr.write('spamn') from __future__ import print_function print('spam', file=sys.stderr) It seems to contradict zen of Python #13 †, so what's the preferred way to do it? Are there any advantages or disadvantages to one way or the other? † There should be one — and pref
我遇到过至少三种打印到stderr的方法: import sys print >> sys.stderr, 'spam' sys.stderr.write('spamn') from __future__ import print_function print('spam', file=sys.stderr) 这似乎与Python#13†的禅宗相矛盾,所以最好的方法是什么? 这种方式或其他方式有什么优点或缺点? †应该有一个 - 而且最好只有一个 - 明显的方法来做到这一点。 我发现这是唯一一个短+灵活+便携+可读的: from __future__
I'm trying to run this code, to run the same command (with little changes) with every frame that I have: traj.reset() import os #os.chdir(outname) for i, frame in enumerate(traj): frame.superpose() comando = "python hollow.py -c constraint -o hollow_%s.pdb urei%s.pdb" % (i, i) os.system(comando) pml_cmd = "pymol urei%s.pdb hollow_%s.pdb -c -d 'as cartoon, urei%s;color gray90,
我试图运行这个代码,对每一帧我都有相同的命令(几乎没有变化): traj.reset() import os #os.chdir(outname) for i, frame in enumerate(traj): frame.superpose() comando = "python hollow.py -c constraint -o hollow_%s.pdb urei%s.pdb" % (i, i) os.system(comando) pml_cmd = "pymol urei%s.pdb hollow_%s.pdb -c -d 'as cartoon, urei%s;color gray90, urei%s;center chain A;set_view (-0.605158150,
I just installed ArcGIS v10.2 64bit background processing which installs Python 2.7.3 64bit and NumPy 1.6.1. I installed SciPy 0.12.0 64bit to the same Python installation. When I opened my Python interpreter I was able to successfully import arcpy, numpy, and scipy . However, when I tried to import scipy.ndimage I got an error that said numpy.core.multiarray failed to import . Everything I
我刚刚安装了安装Python 2.7.3 64bit和NumPy 1.6.1的ArcGIS v10.2 64位后台处理。 我将SciPy 0.12.0 64bit安装到相同的Python安装中。 当我打开我的Python解释器时,我能够成功import arcpy, numpy, and scipy 。 但是,当我试图import scipy.ndimage我得到一个错误,说numpy.core.multiarray failed to import 。 我在网上找到的与这个错误有关的所有东西都引用了scipy和numpy之间的问题,并建议升级到numpy 1.6.1 。 我
Some time ago, I saw a Mono application with colored output, presumably because of its log system (because all the messages were standardized). Now, Python has the logging module, which lets you specify a lot of options to customize output. So, I'm imagining something similar would be possible with Python, but I can't find out how to do this anywhere. Is there any way to make the Pyt
前段时间,我看到一个彩色输出的Mono应用程序,大概是因为它的日志系统(因为所有的消息都是标准化的)。 现在,Python拥有logging模块,它可以让你指定很多选项来自定义输出。 所以,我在想像Python可能会有类似的东西,但我无法找到如何在任何地方做到这一点。 有什么办法让Python logging模块输出颜色? 我想要的(例如)红色错误,蓝色或黄色调试信息等等。 当然,这可能需要一个兼容的终端(大多数现代终端); 但
I found that in 123 , d matches 1 and 3 but not 2 . I was wondering if d matches a digit satisfying what kind of requirement? I am talking about Python style regex. Regular expression plugin in Gedit is using Python style regex. I created a text file with its content being 123 Only 1 and 3 are matched by the regex d ; 2 is not. Generally for a sequence of digit numbers without other cha
我发现在123 , d匹配1和3但不是2 。 我想知道d符合满足什么样的要求的数字? 我正在谈论Python风格的正则表达式。 Gedit中的正则表达式插件使用Python风格的正则表达式。 我创建了一个内容为的文本文件 123 只有1和3匹配正则表达式d ; 2不是。 一般来说,对于一系列没有其他字符的数字序列,只有奇数序号是匹配的,而偶数序号则不是。 例如,在12345中,匹配是1 , 3和5 。 [0-9] 并不总是等同于d 。 在python3中
This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) return primes >>> timeit.Timer(stmt='get_primes.get_primes(1000000)', setup='import get_primes').timeit(1) 1.1499958793645562 Can it be ma
这是我能想到的最好的算法。 def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) return primes >>> timeit.Timer(stmt='get_primes.get_primes(1000000)', setup='import get_primes').timeit(1) 1.1499958793645562 它可以做得更快吗? 这个代
From what I understand, the Global Interpreter Lock allows only a single thread to access the interpreter and execute bytecode. If that's the case, then at any given time, only a single thread will be using the interpreter and its memory. With that I believe that it is fair to exclude the possibility of having race cases, since no two threads can access the interpreter's memory at the
据我所知,全局解释器锁只允许一个线程访问解释器并执行字节码。 如果是这样的话,那么在任何时候,只有一个线程会使用解释器和它的内存。 因此,我认为排除出现种族情况的可能性是公平的,因为没有两个线程可以同时访问解释器的内存,但我仍然看到有关确保数据结构“线程安全”的警告。 有可能它会覆盖python解释器的所有实现(比如cython),它可以关闭GIL并允许真正的多线程。 我了解在没有启用GIL的解释器环境中线程安全
I've been testing a cacheing system of my making. Its purpose is to speed up a Django web application. It stores everything in-memory. According to cProfile most of the time in my tests is spent inside QuerySet._clone() which turns out to be terribly inefficient (it's actually not that strange given the implementation). I was having high hopes for using PyPy to speed things up. I
我一直在测试我制作的缓存系统。 它的目的是加速Django Web应用程序。 它将所有内容存储在内存中。 根据cProfile,在我的测试中,大部分时间都花费在QuerySet._clone()中,结果是效率非常低(实际上并不奇怪)。 我对使用PyPy加快速度寄予厚望。 我有一台64位机器。 然而,在安装了所有必需的库之后,PyPy编译后的代码运行速度比普通Python代码慢2.5倍左右,我不知道该如何解决它。 代码是CPU绑定的(绝对没有数据库
From the Google Open Source Blog: PyPy is a reimplementation of Python in Python, using advanced techniques to try to attain better performance than CPython. Many years of hard work have finally paid off. Our speed results often beat CPython, ranging from being slightly slower, to speedups of up to 2x on real application code, to speedups of up to 10x on small benchmarks. How is this possib
来自Google开源博客: PyPy是Python中Python的重新实现,它使用先进的技术来获得比CPython更好的性能。 多年的努力终于得到了回报。 我们的速度结果经常超过CPython,范围从稍微慢一些,到实际应用程序代码的高达两倍,以便在小型基准测试中将速度提高10倍。 这怎么可能? 哪个Python实现被用来实现PyPy? CPython的? PyPyPy或PyPyPyPy击败他们的分数有什么机会? (在相关说明中......为什么有人会尝试这样的事情?