我发现自己import numpy as np每次打开python解释器时都会打字.如何设置python或ipython解释器以便自动导入numpy?
我正在尝试使用主管来管理我的django项目,在virtualenv中运行gunicorn.我的conf文件如下所示:
[program:diasporamas]
command=/var/www/django/bin/gunicorn_django
directory=/var/www/django/django_test
process_name=%(program_name)s
user=www-data
autostart=false
stdout_logfile=/var/log/gunicorn_diasporamas.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=2
stderr_logfile=/var/log/gunicorn_diasporamas_errors.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=2enter code here
Run Code Online (Sandbox Code Playgroud)
问题是,我需要主管在我的virtualenv中运行'source bin/activate'之后启动命令.我一直在谷歌试图寻找答案,但没有找到任何答案.
注意:我不想使用virtualenvwrapper
有什么帮助吗?
我有一个C扩展项目需要numpy.理想情况下,我希望下载我的项目的人能够运行python setup.py install或使用一个电话pip.我setup.py遇到的问题是,我需要导入numpy来获取头文件的位置,但我希望numpy只是一个常规要求,install_requires以便从Python包索引中自动下载.
以下是我正在尝试做的一个示例:
from setuptools import setup, Extension
import numpy as np
ext_modules = [Extension('vme', ['vme.c'], extra_link_args=['-lvme'],
include_dirs=[np.get_include()])]
setup(name='vme',
version='0.1',
description='Module for communicating over VME with CAEN digitizers.',
ext_modules=ext_modules,
install_requires=['numpy','pyzmq', 'Sphinx'])
Run Code Online (Sandbox Code Playgroud)
显然,import numpy在安装之前我不能处于顶端.我已经看到一个setup_requires参数传递给setup()但是找不到任何关于它的用途的文档.
这可能吗?
是否可以replace-string在emacs中的矩形区域内执行操作?如果是这样,怎么样?
我似乎无法在任何地方找到答案!我在这里找到了一个讨论,但尝试这个我得到一个TypeError: 'NoneType' object is not iterable:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x, y = np.meshgrid(np.arange(10),np.arange(10))
>>> z = x + y
>>> cs = plt.contourf(x,y,z,levels=[2,3])
>>> cs.collections[0].set_label('test')
>>> plt.legend()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2791, in legend
ret = gca().legend(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 4475, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend.py", line 365, in __init__
self._init_legend_box(handles, labels) …Run Code Online (Sandbox Code Playgroud) 为什么python只允许命名参数跟随函数调用中的元组解包表达式?
>>> def f(a,b,c):
... print a, b, c
...
>>> f(*(1,2),3)
File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
Run Code Online (Sandbox Code Playgroud)
它只是一种美学选择,还是存在允许这会导致一些含糊不清的情况?
我有一个TCP连接.服务器只是从客户端读取数据.现在,如果连接丢失,客户端在将数据写入管道(断开的管道)时将收到错误,但服务器仍会侦听该管道.如果连接是UP还是NOT,我能找到任何方法吗?
我正在运行python 2.7.3,我注意到以下奇怪的行为.考虑这个最小的例子:
from multiprocessing import Process, Queue
def foo(qin, qout):
while True:
bar = qin.get()
if bar is None:
break
qout.put({'bar': bar})
if __name__ == '__main__':
import sys
qin = Queue()
qout = Queue()
worker = Process(target=foo,args=(qin,qout))
worker.start()
for i in range(100000):
print i
sys.stdout.flush()
qin.put(i**2)
qin.put(None)
worker.join()
Run Code Online (Sandbox Code Playgroud)
当我循环超过10,000或更多时,我的脚本会挂起worker.join().当循环仅达到1,000时,它工作正常.
有任何想法吗?
有一个numpy函数可以沿着轴将数组与另一个数组中的元素分开吗?例如,假设我有一个带有形状(l,m,n)的数组a和一个带有形状(m,)的数组b ; 我正在寻找相当于的东西:
def divide_along_axis(a,b,axis=None):
if axis is None:
return a/b
c = a.copy()
for i, x in enumerate(c.swapaxes(0,axis)):
x /= b[i]
return c
Run Code Online (Sandbox Code Playgroud)
例如,这在规范化向量数组时很有用:
>>> a = np.random.randn(4,3)
array([[ 1.03116167, -0.60862215, -0.29191449],
[-1.27040355, 1.9943905 , 1.13515384],
[-0.47916874, 0.05495749, -0.58450632],
[ 2.08792161, -1.35591814, -0.9900364 ]])
>>> np.apply_along_axis(np.linalg.norm,1,a)
array([ 1.23244853, 2.62299312, 0.75780647, 2.67919815])
>>> c = divide_along_axis(a,np.apply_along_axis(np.linalg.norm,1,a),0)
>>> np.apply_along_axis(np.linalg.norm,1,c)
array([ 1., 1., 1., 1.])
Run Code Online (Sandbox Code Playgroud) 我有一个使用创建的数组
array1 = np.array([[25, 160, 154, 233],
[61, 244, 198, 248],
[227, 226, 141, 72 ],
[190, 43, 42, 8]],np.int) ;
Run Code Online (Sandbox Code Playgroud)
显示为
[[25, 160, 154, 233]
[61, 244, 198, 248]
[227, 226, 141, 72]
[190, 43, 42 , 8]]
Run Code Online (Sandbox Code Playgroud)
如何将此数组显示为十六进制数字,如下所示:
[[0x04, 0xe0, 0x48, 0x28]
[0x66, 0xcb, 0xf8, 0x06]
[0x81, 0x19, 0xd3, 0x26]
[0xe5, 0x9a, 0x7a, 0x4c]]
Run Code Online (Sandbox Code Playgroud)
注意:十六进制中的数字可能不是int中数字的真实转换.我已经填充十六进制数组只是为了举例说明我需要的东西.