我正在使用 pytest。我有一个测试,其中涉及检查是否在发生某些事情时没有进行导入。这很容易实现,但是当测试在 pytest 中运行时,它会在与许多其他测试相同的过程中运行,这些测试可能会事先导入该内容。
是否有某种方法可以将测试标记为在其自己的进程中运行?理想情况下会有某种装饰器,例如
@pytest.mark.run_in_isolation
def test_import_not_made():
....
Run Code Online (Sandbox Code Playgroud)
但我还没有找到类似的东西。
我正在尝试在https://readthedocs.org/上构建文档。
我看不到任何导入matplotlib的文件的文档字符串。
当我查看构建日志时,发现from matplotlib import pyplot as plt失败,并显示以下消息:
/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/checkouts/latest/docs/source/plotting.rst:67: WARNING: autodoc: failed to import function u'dbplot' from module u'artemis.plotting.db_plotting'; the following exception was raised:
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/sphinx/ext/autodoc.py", line 551, in import_object
__import__(self.modname)
File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/artemis_ml-1.6-py2.7.egg/artemis/plotting/db_plotting.py", line 3, in <module>
from artemis.plotting.matplotlib_backend import BarPlot
File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/artemis_ml-1.6-py2.7.egg/artemis/plotting/matplotlib_backend.py", line 7, in <module>
from matplotlib import pyplot as plt
File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup …Run Code Online (Sandbox Code Playgroud) Python有一个很好的功能:"with"语句.它对于进行影响语句中调用的所有代码的全局更改很有用.
例如,你可以定义一个CapturePrintStatements类来捕获在"with"中调用的所有print语句
with CapturePrintStatements() as c:
print 'Stuff Done'
print 'More Stuff Done'
assert c.get_text() == 'Stuff Done'
Run Code Online (Sandbox Code Playgroud)
Java中是否存在等价物?
我有一个点云 C,其中每个点都有一个关联的值。假设这些点在二维空间中,所以每个点都可以用三元组 (x, y, v) 表示。
我想找到局部最大值点的子集。也就是说,对于某个半径 R,我想找到 C 中点 S 的子集,使得对于 S 中的任何点 Pi(具有值 vi),在距离 Pi 的 R 距离内,C 中没有点 Pj 的值 vj 为大于 vi。
我知道如何在 O(N^2) 时间内做到这一点,但这似乎很浪费。有没有一种有效的方法来做到这一点?
旁注:
numpy sparse-array kdtree sparse-matrix computational-geometry
我希望能够在全球范围内订购字典.
我经常发现自己编写这样的代码:
things_to_evaluate = {
'thing 1': lambda: Thing(property = 'a'),
'thing 2': lambda: Thing(property = 'b'),
...
}
...
for k, v in things_to_evaluate.iteritems():
thing = v()
result[k] = thing.process(something)
Run Code Online (Sandbox Code Playgroud)
在我的例子中,字典语法是指定我想尝试的一堆实验的便捷方式.现在,我想实际按照我指定的顺序显示结果.
我知道这是一件可怕的事情,我知道散列和无序集合以及OrderedDict和一切的存在.
尽管如此,如果我可以选择仅仅为了我自己的方便,使用一行代码,覆盖一些原始函数并使其成为所有dicts的命令,结果会很糟糕.
那么,有没有人知道这样做的方法?
我听说使用try-catch来处理在正常程序流程中可能发生的任何事情,并且应该使用if-else代替.
但是,我们想要将它用于初始化(仅发生一次且仅一次的事件)的情况如何.初始化时,您可能希望这取决于第一个传入数据,如下例所示:
class RunningVectorSum{
double[] running_sum;
public double[] add(double vec[]){
try{
for (int i=0; i<running_sum.length; i++)
running_sum[i]+=vec[i];
}
catch(NullPointerException ex){
running_sum = new double[vec.length];
for (int i=0; i<running_sum.length; i++)
running_sum[i] = vec[i];
}
return running_sum;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,从长远来看,使用try-catch与使用时是否应该更快:
public double[] add(double vec[]){
if (running_sum==null)
running_sum = new double[vec.length];
for (int i=0; i<running_sum.length; i++)
running_sum[i]+=vec[i];
return running_sum;
}
Run Code Online (Sandbox Code Playgroud)
代替?
编辑:我所知道的事情:
我不知道的事情:
python ×4
java ×2
if-statement ×1
kdtree ×1
matplotlib ×1
numpy ×1
pytest ×1
sparse-array ×1
tkinter ×1
try-catch ×1