为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同时。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用于批量操作的java RESTful API分块响应   java读取在线存储的文本文件   在Java ME中将双精度舍入到小数点后5位   java查找一个数字的最接近因子   java更改JMenuBar的字体   java Kmeans聚类算法运行时间和复杂性   java是否可以阻止try catch返回null   java内容解析器指向具有正确URI的错误表   java Android Kotlin插装测试未被识别为插装测试   java TestNG@Dataprovider   在forloop和print语句中声明变量时发生java错误   java在Android Studio 3中设置JNI