跳转到主要内容

用于Zope开发的实用装饰器集合

项目描述

该软件包包含帮助实现重复性任务的函数装饰器。它主要面向Zope开发。

变更历史

0.2 (2007-09-05)

  • 修复了从查询字符串传递参数到方法的问题。当直接使用URL调用视图方法时,在装饰视图方法时丢失了查询字符串参数。现在装饰器将查询参数推回到方法调用中。

0.1 (2007-08-31)

  • 首次公开发布。

详细文档

browser 模块

此模块提供与Zope 3浏览器视图(也可用于Zope 2)有用的装饰器。

JSON装饰器

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]}'

传递给装饰器的任何关键字参数都将传递给 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缓存禁用装饰器

nocache 装饰器用于Zope 3视图,并设置适当的HTTP头以禁用装饰方法的结果的缓存。

>>> from hexagonit.decorators.browser import nocache

我们将通过一个模拟视图对象演示装饰器的使用。

>>> 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

也可以同时使用 json 装饰器和 nocache 装饰器。

下载

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

hexagonit.decorators-0.2.tar.gz (4.4 kB 查看散列值)

上传时间 源代码

构建分发

hexagonit.decorators-0.2-py2.4.egg (8.8 kB 查看散列值)

上传时间 源代码

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面