Django-Tastypie有ImmediateHttpResponse例外,允许立即回复客户端:
raise ImmediateHttpResponse(response='a message')
Run Code Online (Sandbox Code Playgroud)
Django有Http404,但我找不到更普遍的例外ImmediateHttpResponse.
您使用什么技术立即向客户返回400响应?
例如有模型:
class Subscriber(Model):
def delete(self, *args, **kwargs):
raise ImmediateHttpResponse('Deleting subcribers is not allowed!')
Run Code Online (Sandbox Code Playgroud)
并尝试删除对象将返回给客户端一个400给定消息的响应.
我nosetests在详细模式下运行我的测试:
....
test_cache_region (tests.test_sysutil.TestCachedMethodDecorator) ... ok
test_expire (tests.test_sysutil.TestCachedMethodDecorator) ... ok
test_lru (tests.test_sysutil.TestCachedMethodDecorator) ... ok
test_max_capacity (tests.test_sysutil.TestCachedMethodDecorator) ... ok
test_DecimalJSONEncoder (tests.test_util.UtilTestCase) ... ok
test_cdecimal_support (tests.test_util.UtilTestCase) ... ok
test_ceil_with_ndigits (tests.test_util.UtilTestCase) ... ok
test_switch_keyboard (tests.test_util.UtilTestCase) ... ok
...
Run Code Online (Sandbox Code Playgroud)
有没有办法轻松地将报告格式更改为:
...
tests.test_sysutil.TestCachedMethodDecorator.test_lru ... ok
tests.test_sysutil.TestCachedMethodDecorator.test_max_capacity ... ok
tests.test_util.UtilTestCase.test_DecimalJSONEncoder ... ok
tests.test_util.UtilTestCase.test_cdecimal_support ... ok
...
Run Code Online (Sandbox Code Playgroud) 我正在查看一长串 DNA 核苷酸,并正在寻找以起始代码“AAA”开头并以停止代码“CCC”结尾的序列。由于核苷酸是三联体,我发现的每个序列的开始和结束之间的核苷酸数量必须是三的倍数。
例如,“AAAGGGCCC”是一个有效序列,但“AAAGCCC”不是。
此外,在每个终止码之前,我想要关于特定阅读框架的最长链。
例如,如果 DNA 是“AAAGGGAAACCC”,那么“AAAGGGAAACCC”和“AAACCC”在技术上都是有效的,但由于它们共享相同的停止代码实例,我只想要最长的 DNA 链“AAAGGGAAACCC”。此外,如果我的链是“AAAAGGCCCCC”,我必须返回“AAAAGGCCC”和“AAAGGCCCC”,因为它们处于不同的阅读框架(一个阅读框架是 mod 3,另一个是 mod 1。)
虽然我认为我有代码来搜索满足 3 的倍数要求且不重叠的字符串,但我不确定如何实现保持相同阅读框架的第二个标准。我下面的代码只会返回不重叠的最长字符串,但不区分阅读帧,因此在上面的示例中,它会捕获“AAAAGGCCC”而不是“AAAGGCCCC”:
match = re.finditer(r"AAA\w{3}{%d}BBB$"% (minNucleotide-6, math.ceil((minNucleotide-6)/3))
Run Code Online (Sandbox Code Playgroud)
抱歉啰嗦了,感谢您的浏览!
我正在添加我的剧本中的另一本剧本。我需要向它传递一个名为 的变量required_node_version。我在我的剧本中使用相同的变量,因此期望包含的剧本将继承它。但它不会导致“未定义”错误。所以我尝试使用这个不太优雅的解决方案:
- hosts: localhost
connection: local
vars:
required_node_version: v6.3.1
...
- include: ../../base/ci/build.yml
vars:
required_node_version: "{{required_node_version}}"
Run Code Online (Sandbox Code Playgroud)
什么导致{"failed": true, "msg": "ERROR! recursive loop detected in template string: {{required_node_version}}"}。
有什么优雅的解决方案吗?
格式规范中是否有选项将零值显示为空白,否则使用格式?
>>> from decimal import Decimal
>>> '{:+010,.2f}'.format(Decimal('1234.56'))
'+01,234.56'
>>> '{:???f}'.format(Decimal(0))
''
>>>
Run Code Online (Sandbox Code Playgroud)
更新:
我需要与此处相同的行为:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SectionSeparator
如果Python在标准库中没有它,请确认这一点,我将接受它作为答案.
请参阅最后的更新.
我使用的是Ubuntu Linux 11.10,Python 3.
我写了一个Python脚本,它使用pyuic4将一些Qt*.ui文件转换为*.py.然后我想将获得的*.py文件编译为*.pyc并删除*.py文件.
出于某种原因,当我删除转换后的*.py文件时,*.pyc版本也会被删除:
try:
command = 'pyuic4 -o /home/vic/ui_form.py /home/vic/form.ui'
output = subprocess.check_output(command, shell= True, stderr= subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print('Failed:', e.output)
else:
print('Converted %s to %s' % (source, targetName))
# convert *.py to *.pyc and delete the source
source = '/home/vic/ui_form.py'
target = source + 'c' # py -> pyc
py_compile.compile(source, target)
#shutil.copy(target, target + '_') # if uncommented - the *.pyc_ file remains
os.remove(source) # if commented - both *.py and *.pyc files …Run Code Online (Sandbox Code Playgroud) 根据python样式指南,python关键字is应该用于代替==运算符.
然而,它们并不总是与此处所示完全相同.为什么?实际差异是什么,适当的用法是什么?
import unittest
class testIS(unittest.TestCase):
def test_is(self):
self.assertEqual(1,1)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
哪个有效...但以下不...
import unittest
class testIS(unittest.TestCase):
def test_is(self):
self.assertEqual(1,1)
if __name__ is '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud) 我看到SELECT EXISTS使用了很多:
if db.query("""
SELECT EXISTS (
SELECT 1 FROM checkout
WHERE checkout_id = %s
)
""" % checkout_id).getresult()[0][0] == 't':
Run Code Online (Sandbox Code Playgroud)
与我喜欢的:
if db.query("""
SELECT 1 FROM checkout
WHERE checkout_id = %s
LIMIT 1
""" % checkout_id).getresult():
Run Code Online (Sandbox Code Playgroud)
哪个是首选,为什么?
PS我正在使用Python和PosgreSQL.
cert=> explain SELECT EXISTS (SELECT 1 FROM checkout WHERE checkout_id = 3);
QUERY PLAN
--------------------------------------------------------------------------------------
Result (cost=4.03..4.03 rows=1 width=0)
InitPlan
-> Index Scan using checkout_pkey on checkout (cost=0.00..4.03 rows=1 width=0)
Index Cond: (checkout_id = 3)
(4 rows)
cert=> explain …Run Code Online (Sandbox Code Playgroud) 运行它(Python 3.3.1):
from collections import OrderedDict
class MyType(type):
@classmethod
def __prepare__(*args):
return OrderedDict()
class MyClass2(metaclass=MyType):
attr1 = 1
attr2 = 2
attr3 = 3
attr4 = 4
attr5 = 5
def __init__(self):
self.attr6 = 6
def func(self):
pass
print(MyClass2.__dict__.items())
Run Code Online (Sandbox Code Playgroud)
我正进入(状态:
dict_items([('__weakref__', <attribute '__weakref__' of 'MyClass2' objects>), ('__dict__', <attribute '__dict__' of 'MyClass2' objects>), ('__init__', <function MyClass2.__init__ at 0x7f08a106dc20>), ('__doc__', None), ('attr4', 4), ('attr5', 5), ('attr2', 2), ('attr3', 3), ('attr1', 1), ('func', <function MyClass2.func at 0x7f089f995c20>), ('__module__', '__main__')])
Run Code Online (Sandbox Code Playgroud)
类属性不按其定义顺序排序.
OrderedDict作为班级使用我做错了__dict__ …
我有这个脚本:
#!/bin/bash
function contains() {
local -n array=$1
local value=$2
for item in "${array[@]}"; do
[ "$item" = "$value" ] && return 0
done
return 1
}
array=(a "b c" "d")
value="b c"
contains array value
Run Code Online (Sandbox Code Playgroud)
运行它我收到此错误:
***: line 6: warning: array: circular name reference
Run Code Online (Sandbox Code Playgroud)
这是什么意思?如何解决这个问题?