有没有办法找到什么函数称为当前函数?例如:
def first():
second()
def second():
# print out here what function called this one
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
In Python 2.6 it is possible to suppress warnings from the warnings module by using
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
Run Code Online (Sandbox Code Playgroud)
Versions of Python before 2.6 don't support with however, so I'm wondering if there alternatives to the above that would work with pre-2.6 versions?
我正在使用nose编写单元测试,我想检查函数是否引发警告(函数使用warnings.warn).这是否可以轻松完成?
通过OO API使用Matplotlib对于非交互式后端来说非常简单:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
canvas.print_figure('test.png')
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试用交互式后端重复类似的东西,我就会失败(我甚至无法让交互式人物出现在第一位).有没有人有任何通过OO API使用Matplotlib来创建交互式数据的例子?
给定1D数组值,最简单的方法是找出最合适的双峰分布是什么,每个"模式"是正态分布?或者换句话说,如何才能找到两个正态分布的组合,以便再现一维数组?
具体来说,我有兴趣在python中实现它,但答案不一定是语言特定的.
谢谢!
是否有一种简单的方法可以将记录/行添加到numpy重新排列而无需创建新的重新排列?假设我有一个在内存中占用1Gb的重新排列,我希望能够在没有python暂时占用2Gb内存的情况下添加一行.
我试图为numpy数组的每一行选择特定的列元素.例如,在以下示例中:
In [1]: a = np.random.random((3,2))
Out[1]:
array([[ 0.75670668, 0.1283942 ],
[ 0.51326555, 0.59378083],
[ 0.03219789, 0.53612603]])
Run Code Online (Sandbox Code Playgroud)
我想选择第一行的第一个元素,第二行的第二个元素和第三行的第一个元素.所以我尝试做以下事情:
In [2]: b = np.array([0,1,0])
In [3]: a[:,b]
Run Code Online (Sandbox Code Playgroud)
但是这会产生以下输出:
Out[3]:
array([[ 0.75670668, 0.1283942 , 0.75670668],
[ 0.51326555, 0.59378083, 0.51326555],
[ 0.03219789, 0.53612603, 0.03219789]])
Run Code Online (Sandbox Code Playgroud)
这显然不是我想要的.有没有一种简单的方法可以在不使用循环的情况下做我想做的事情?
我有一个Python包,我想在其中包含一个空目录作为源代码分发的一部分.我尝试添加
include empty_directory
Run Code Online (Sandbox Code Playgroud)
到MANIFEST.in文件,但是当我跑
python setup.py sdist
Run Code Online (Sandbox Code Playgroud)
空目录仍未包含在内.关于如何做到这一点的任何提示?
根据sphinx文档,该.. autoattribute指令应该能够记录实例属性.但是,如果我这样做::
.. currentmodule:: xml.etree.ElementTree
.. autoclass:: ElementTree
.. autoattribute:: ElementTree._root
Run Code Online (Sandbox Code Playgroud)
然后在构建时我得到一个AttributeError:
Traceback (most recent call last):etree.ElementTree.ElementTree
File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/ext/autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "/Volumes/Raptor/Library/Python/2.7/lib/python/site-packages/sphinx/util/inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: _root
Run Code Online (Sandbox Code Playgroud)
即使我实例化ElementTree并尝试访问该_root属性,它也可以正常工作::
>>> from xml.etree.ElementTree import ElementTree
>>> e = ElementTree()
>>> hasattr(e, '_root')
True
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
(我实际上有一个我自己的类的问题,但我只是使用ElementTree类作为一个例子,因为它在标准库中)
我使用pytest运行在一个Python包的考验,我想知道是否有被作为测试是提高废弃警告(当所有的测试都通过)的一部分执行的代码.有谁知道这样做的方法?
python ×10
numpy ×2
warnings ×2
algorithm ×1
autodoc ×1
installation ×1
matplotlib ×1
nose ×1
pytest ×1
testing ×1
unit-testing ×1