为现有类型定义泛型方法的一种简单而直接的方式。
项目描述
它提供了一种简单而直接的方式为现有类型定义泛型方法。您可以使用它来创建重载方法。
兼容性
类型查询不依赖于任何非标准库。它在以下环境中工作
Python 2.5–2.7, 3.2–3.5
CPython, Stackless, PyPy, Jython
安装
使用pip安装
$ pip install TypeQuery
示例:JSON编码器
from typequery import GenericMethod from sys import version_info from re import sub from numbers import Real from collections import Mapping, Iterable if version_info.major > 2: basestring = string = str else: string = unicode json = GenericMethod('json') @json.of(type(None)) def json(value): return 'null' @json.of(bool) def json(value): return 'true' if value else 'false' @json.of(Real) def json(value): return str(value) @json.of(string) def json(value): def escape(match): s = match.group(0) if s in ('\\', '"', '\b', '\f', '\n', '\r', '\t'): return '\\' + s n = ord(s) if n < 0x10000: return r'\u%04x' % n n -= 0x10000 s1 = 0xd800 | ((n >> 10) & 0x3ff) s2 = 0xdc00 | (n & 0x3ff) return r'\u%04x\u%04x' % (s1, s2) return '"%s"' % sub(r'([\\"]|[^\ -~])', escape, value) @json.of(Iterable) def json(value): return '[%s]' % ', '.join(json(element) for element in value) @json.of(Mapping) def json(value): return '{%s}' % ', '.join('%s: %s' % (json(string(key)), json(value)) for key, value in value.items())
定义的json函数工作方式如下
>>> json(123) '123' >>> json(True) 'true' >>> json({'apple': 3, 'banana': 5, 'carrot': 1}) '{"apple": 3, "banana": 5, "carrot": 1}'
如上所示,您可以为包括ABC如collections.Iterable在内的现有类型定义类型感知实例方法。
变更日志
版本 0.1.5
发布于2016年6月23日。
与最新Python版本兼容:Python 3.3至3.5,以及PyPy3。
版本0.1.4
发布于2012年10月18日。
请不要再使用bitbucket-distutils以避免与打包和分发相关的几个头痛问题。
版本0.1.3
发布于2012年8月29日。
修复了由一个函数处理多种类型的bug。[#2]
版本0.1.2
发布于2012年8月15日。
您可以使用GenericMethod.inherit()方法继承现有的泛型方法。
为GenericMethod.of()装饰器和Decorator构造函数添加了with_receiver选项。
版本0.1.1
发布于2012年5月22日。
现在它与除了abc.ABCMeta之外的抽象基类一起工作得很好。
修复了GenericMethod.clone()方法的bug。它不再反映克隆对象对原始对象的变化。[#1]
版本0.1.0
发布于2012年5月21日。
初始发布。
项目详情
关闭
TypeQuery-0.1.5.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 00f60785053723bca1edb519264fb05c3becbe462ba157da47a90e7ae34fb9bd |
|
MD5 | a8e1dbdc023c8fe4c01229f564a81ac0 |
|
BLAKE2b-256 | acf97015735ceac509438f54a944fb0e17f7f429e39712ee6c7e01273dbd3a37 |
关闭
TypeQuery-0.1.5-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a8fc9642eff2e04eb705897c3361ccc6530cffc71ec222a54135e6dba28517d6 |
|
MD5 | 287f1f57069cd1b81f28c4f6cacd3991 |
|
BLAKE2b-256 | 4a89c30bd30a30cc65c71dad3c54d85453173141856017c21f03dfa4ab0eeb25 |