为zope开发收集有用的装饰工具
hexagonit.decorators的Python项目详细描述
内容
该包包含有助于实现 重复性任务。它主要面向zope开发。
Change History
0.2 (2007-09-05)
- Fixed problem with methods not being passed parameters from the query string. When a view method was called directly using an URL the parameters from the query string were lost when a view method was decorated. The decorators now push back the query parameters into the method call.
0.1 (2007-08-31)
- First public release.
Detailed Documentation
^{tt1}$ module
这个模块提供了对zope 3浏览器有用的装饰器。 视图(在zope 2中也可用)。
JSON decorator
json装饰器使用simplejson对 修饰函数为json。
>>> from hexagonit.decorators.browser import json>>> @json ... def json_callback(): ... """JSON callback method.""" ... return {'bool' : (True, False) } >>> json_callback() '{"bool": [true, false]}'
传递给decorator的任何关键字参数都将传递给 simplejson.dumps函数。
>>> @json(sort_keys=True, ensure_ascii=False) ... def json_callback(): ... """JSON callback method.""" ... return {'foo' : None, 'bool' : (True, False), 'ints' : [1,2,3]} >>> json_callback() u'{"bool": [true, false], "foo": null, "ints": [1, 2, 3]}'
HTTP cache disabling decorator
nocachedecorator用于zope 3视图并设置 适当的http头以禁用 装饰方法。
>>> from hexagonit.decorators.browser import nocache
我们将演示如何将decorator与一个虚拟视图对象一起使用。
>>> class Dummy(object):pass >>> class BrowserView(object): ... """Dummy view class that fakes the ``request`` object.""" ... def __init__(self): ... self.request = Dummy() ... self.request.response = Dummy() ... self.headers = [] ... self.request.response.setHeader = lambda header,value: self.headers.append('%s: %s' % (header, value)) ... ... @nocache ... def ajax_callback(self): ... print '\n'.join(self.headers)>>> view = BrowserView() >>> view.ajax_callback() Pragma: no-cache Expires: Sat, 1 Jan 2000 00:00:00 GMT Cache-Control: no-cache, must-revalidate
还可以同时使用jsondecorator和 nocachedecorator同时。