我已经通过Stackoverflow上的其他问题阅读了,但仍然没有更接近.对不起,如果已经回答了这个问题,但我没有得到任何建议.
>>> import re
>>> m = re.match(r'^/by_tag/(?P<tag>\w+)/(?P<filename>(\w|[.,!#%{}()@])+)$', '/by_tag/xmas/xmas1.jpg')
>>> print m.groupdict()
{'tag': 'xmas', 'filename': 'xmas1.jpg'}
Run Code Online (Sandbox Code Playgroud)
一切都很好,然后我尝试用挪威字符(或更像unicode)的东西:
>>> m = re.match(r'^/by_tag/(?P<tag>\w+)/(?P<filename>(\w|[.,!#%{}()@])+)$', '/by_tag/påske/øyfjell.jpg')
>>> print m.groupdict()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groupdict'
Run Code Online (Sandbox Code Playgroud)
如何匹配典型的unicode字符,例如øæå?我希望能够在上面的标签组和文件名的标签组中匹配这些字符.
python regex unicode non-ascii-characters character-properties
我想创建一个装饰器,用于描述方法并记录结果.如何才能做到这一点?
我想在python中伪造一个包.我想定义一些代码可以做的事情
from somefakepackage.morefakestuff import somethingfake
Run Code Online (Sandbox Code Playgroud)
并且somefakepackage在代码中定义,因此它下面的所有内容都是如此.那可能吗?这样做的原因是欺骗我的单元测试我得到了一个包(或者我在标题中说的一个模块)在python路径中实际上只是为这个单元测试模拟的东西.
谢谢!
为什么这不起作用:
class X:
var1 = 1
def __enter__(self): pass
def __exit__(self, type, value, traceback): pass
with X() as z:
print z.var1
Run Code Online (Sandbox Code Playgroud)
我明白了:
print z.var1
AttributeError: 'NoneType' object has no attribute 'var1'
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用fabric来安装和部署一个Web项目,在此期间我需要创建一个postgresql数据库并配置一个RabbitMQ服务器.这两个操作都是交互式的,需要用户输入以创建数据库,添加用户,设置密码等(至少据我所知).
我可以使用结构脚本来执行这样的交互式shell操作吗?
我遵循了http://celeryq.org/docs/django-celery/getting-started/first-steps-with-django.html中的指南,创建了一个在tasks.py中调用我的测试方法的视图:
import time
from celery.decorators import task
@task()
def add(x, y):
time.sleep(10)
return x + y
Run Code Online (Sandbox Code Playgroud)
但是如果我的add-method需要很长时间来响应,那么如何在调用add.delay(1,2)时存储我得到的结果对象并使用它来使用get来检查进度/成功/结果?
是否有可能在数据库中腌制或以某种方式存储django查询?这不会起作用:
u = User.objects.all
import cPickle
pickled_query = cPickle.dumps(u) # and store the pickled_query in a db-field.
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
更新:
import cPickle
class CustomData(models.Model):
name = models.CharField(max_length = 30)
pickled_query = models.CharField(max_length = 300)
def get_custom_result(self):
q = cPickle.loads(self.pickled_query)
return q()
>>> cd = CustomData(name="My data", pickled_query=cPickle.dumps(User.objects.all))
>>> cd.save()
>>> for item in cd.get_custom_result(): print item
# prints all the users in the database, not printing the query result
# when pickled, but when called like cd.get_custom_result(), that is when
# …Run Code Online (Sandbox Code Playgroud) from BeautifulSoup import BeautifulStoneSoup
xml_data = """
<doc>
<test>test</test>
<foo:bar>Hello world!</foo:bar>
</doc>
"""
soup = BeautifulStoneSoup(xml_data)
print soup.prettify()
make = soup.find('foo:bar')
print make
# prints <foo:bar>Hello world!</foo:bar>
make.contents = ['Top of the world Ma!']
print make
# prints <foo:bar></foo:bar>
Run Code Online (Sandbox Code Playgroud)
如何更改元素的内容,在这种情况下是变量"make"中的元素,而不会丢失内容?如果您能指出我可以修改现有xml文档的其他纯python模块,请告诉我.
PS!BeautifulSoup非常适合HTML和XML的屏幕分析和解析!