我需要使用 django 测试虚拟客户端为 Django 中的自定义 handler404 和 handler500 编写一些测试。第一个很容易测试,而我对第二个有问题。
基本上,问题在于 Django 测试客户端没有捕获异常并且没有路由到正确的处理程序。这是一个问题,因为我们需要测试是否使用了正确的自定义处理程序和模板。
我们有一个简单的中间件类来模拟测试异常:
class HTTPStatusCodeMiddleware(object):
def process_view(self, request, *args):
if 'cookie_500_error' in request.COOKIES:
raise Exception('Test exception')
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于浏览器中的手动测试。
现在,测试是:
def test_404_template(self):
c = Client()
url = '/not_existing.html'
response = c.get(url)
self.assertEqual(response.status_code, 404) # success
self.assertTemplateUsed(response, 'custom/404.html') # success
def test_500_template(self):
c = Client()
c.cookies['cookie_500_error'] = '1'
response = c.get('/') # here middleware raises exception!!!
self.assertEqual(response.status_code, 500)
self.assertTemplateUsed(response, 'custom/500.html')
Run Code Online (Sandbox Code Playgroud)
任何的想法?我没有选择使用硒。谢谢!
我希望我能在这里找到解决这个相当复杂的问题的方法。
我使用 sphinx 和 intersphinx 来记录我的项目。
我有一个继承自 mongoengine.Document 的类。
当我使用 sphinx-apidoc 和 sphinx-build (通过 sphinx-quickstart 自动生成的 Makefile)构建 sphinx 文档时,对 mongoengine.Document 类的引用显示为 mongoengine.document.Document,这实际上是正确的完全限定名称,但这是一个问题,因为在 mongoengine 项目上该类被标记为 mongoengine.Document 所以 intersphinx 根本不链接。
有没有办法告诉 sphinx 在导入基类时生成有关基类的信息(在我的代码中,我有 from mongoengine import Document)而不是其完整模块路径?
下面的代码:
from mongoengine import Document, EmbeddedDocumentListField
class MyDocument(Document):
""" my docstring """
Run Code Online (Sandbox Code Playgroud)
它会生成一些 html,例如:
class myproj.models.MyDocument(*args, **values) Bases:
mongoengine.document.Document <-- intersphinx does not find the link to external doc!
Run Code Online (Sandbox Code Playgroud)
代替
class myproj.models.MyDocument(*args, **values)
Bases: mongoengine.Document <-- here intersphinx will properly link
Run Code Online (Sandbox Code Playgroud) PostgreSQL 中的 round(numeric,integer) 函数仅向上舍入:
round(cast (41.0255 as numeric),3) ==> 41.026
Run Code Online (Sandbox Code Playgroud)
由于我们需要一个返回 41.025 的 round 函数,并且(非常令人惊讶)PostgreSQL 中没有这样的函数(我们使用的是 9.1.5),因此我们编写了一个“包装器”函数,该函数在第一个版本中非常幼稚,并且粗糙......但由于 plpgsql 中缺乏对此类问题的本机支持,我们没有找到更好的东西。
代码如下所示。问题是它对于我们的目的来说太慢了。您能建议一个更好的方法来处理这项任务吗?
这是代码:
CREATE OR REPLACE FUNCTION round_half_down(numeric,integer) RETURNS numeric
AS $$
DECLARE
arg ALIAS FOR $1;
rnd ALIAS FOR $2;
tmp1 numeric;
res numeric;
BEGIN
tmp1:=arg;
IF cast(tmp1 as varchar) ~ '5$' THEN res:=trunc(arg,rnd);
ELSE res:=round(arg,rnd);
END IF;
RETURN res;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我需要转换数值并使用正则表达式......这就是(我认为)破坏性能的原因。
正如您所知:我们需要这个,因为我们必须比较存储在两个不同列(在两个不同表上)但具有不同数字数据类型的数字:一个是双精度数,一个是实数。问题是,当插入到真实数据类型列时,PostgreSQL 执行 ROUND HALF DOWN,但它没有通过其数学函数提供这样的选项!
编辑:
该功能实际上有问题。第一次快速重写是为了提高工作功能的性能,但速度非常慢。
行为必须符合以下条件:
IF舍入时推迟的小数位向上<=5 => trunc
ELSE舍入。
一些例子: …
postgresql floating-point rounding plpgsql floating-accuracy
我有三个numpy数组:
row = np.array([1,2,3,4,5])
# a is a subset of row:
a = np.array([1, 5])
# b is an array that I use to change some elements in the first row array:
b = np.array([10, 550])
Run Code Online (Sandbox Code Playgroud)
我需要做的是一次性改变a对应的b元素中存在的行数组的元素.
即:
>> modified_row
array([10, 2, 3, 4, 500])
Run Code Online (Sandbox Code Playgroud)
以天真的方式这样做将是:
for i in range(len(a)):
row[np.where(row==a[i])]= b[i]
Run Code Online (Sandbox Code Playgroud)
我想要一个解决方案;
row[np.where(row==a)] = b
Run Code Online (Sandbox Code Playgroud)
但这不起作用......
提前致谢!
我需要遍历一个巨大的 numpy 数组来构建三个列表,这取决于昂贵的 C 库调用的结果,它接受标量值并且不能被向量化(或者至少我不知道如何去做)。这个循环可能需要几个小时到几天不等,我可以看到性能随着时间的推移而下降(我记录了进度,我可以看到最后会慢得多)可能是由于列表大小的增加(??)。
代码如下(我省略了与打印进度和一些微优化相关的代码):
import numpy as np
import swig_c_lib
def build_indexes(large_numpy_array_1, large_numpy_array_2):
xs = []
ys = []
idxs = []
for (x, y), value in np.ndenumerate(large_numpy_array_1):
if not (value <= -1.0e+10):
try:
index = swig_c_lib.slow_computation(np.asscalar(large_numpy_array_2[x, y]), np.asscalar(large_numpy_array_1[x, y]))
except swig_lib.InternalError:
pass
else:
xs.append(x)
ys.append(y)
idxs.append(index)
return np.asarray(xs), np.asarray(ys), np.asarray(idxs)
Run Code Online (Sandbox Code Playgroud)
可能,一种解决方案是将大型输入 numpy 数组拆分为 4 个子数组并使用多处理(但我不确定如何合并结果)。任何人都可以在这里提供帮助?
我有一个asyncio TCP服务器从客户端接收消息,在服务器上执行stuff()并发回文本.服务器在正确接收和发送数据的意义上运行良好.问题是我无法从客户端的服务器取回消息,因为我在控制台输入时有阻塞例程(基本上从不执行data_received方法).只有exit命令工作正常(它关闭循环).怎么解决这个?这是服务器和客户端代码.它基本上是EchoClient asyncio版本,还有一些练习的管道代码.
# client.py
import abc
import asyncio
import sys
MENU = '''
a) do x
b) do y
c) exit
'''
loop_ = asyncio.get_event_loop()
class XCommand:
def run(self):
self.client.send_data_to_tcp('X:') # to bytes
class YCommand(Command):
def run(self):
s = input('Input for Y ### ')
self.client.send_data_to_tcp('Y:' + s)
class ExitCommand(Command):
def run(self):
self.client.send_data_to_tcp('EXIT:')
print('Goodbye!')
loop_.close()
exit()
class CommandFactory:
_cmds = {'a': ACommand,
'b': BCommand,
'c': ExitCommand,
}
@classmethod
def get_cmd(cls, cmd):
cmd_cls = cls._cmds.get(cmd)
return cmd_cls
def show_menu(client):
print(MENU)
while …Run Code Online (Sandbox Code Playgroud)