相对于Python标准类型的增强
项目描述
此项目为Python提供了一些增强类型
“约束集”(有序或无序)
现在就到这里。
它还提供了Django(1.7 - 1.10)的扩展。
它已在2.7至3.4的所有Python版本上进行了全面测试;并且以BSD许可证分发。
链接
GitHub上的存储库和问题:http://github.com/rbarrois/extypes
在https://extypes.readthedocs.io/上的文档(尚未提供)
入门
从PyPI使用pip安装包
$ pip install extypes
或从GitHub
$ git clone git://github.com/rbarrois/extypes
$ cd extypes
$ python setup.py install
要检查是否一切顺利,请启动Python外壳并导入extypes
import extypes
简介
extypes提供了一个新的类型,ConstrainedSet。
这是一个类似于set()的对象,但值只能从一组特定的选项中获取。
ConstrainedSet的声明方式与collections.namedtuple非常相似
import extypes
Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
这将声明一个新类,Foods,其实例是仅接受选项为'eggs'、'spam'和'bacon'的ConstrainedSet。
这些对象可以用作简单的set()对象
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['spam', 'bacon'])
>>> fresh = Foods(['bacon', 'eggs'])
>>> 'eggs' in meat
False
>>> 'eggs' in fresh
True
>>> meat & fresh
Foods(['bacon'])
作为一个set()对象,它们是可变的
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['spam', 'bacon'])
>>> meat.remove('spam')
>>> meat
Foods(['bacon'])
并且可迭代的
>>> import extypes
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> meat = Foods(['bacon', 'spam'])
>>> list(meat)
['spam', 'bacon']
但只接受有效选项
>>> Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
>>> greens = Foods(['spinach']
Traceback (most recent call last):
...
ValueError: Invalid keys ['spinach'], please use a value in ['spam', 'bacon', 'eggs'].
扩展:Django
extypes 还为 Django 提供自定义字段 - 兼容 Django 1.7 及以上版本。
from django.db import models
import extypes
import extypes.django
Foods = extypes.ConstrainedSet(['eggs', 'spam', 'bacon'])
class Fridge(models.Model):
contents = extypes.django.SetField(choices=Foods)
该字段将简单地表现为一个简单的 ConstrainedSet。
>>> fridge = Fridge(contents=['bacon'])
>>> fridge.contents.add('eggs')
>>> fridge.save()
在表单中,它显示为一个多选字段。在数据库中,它以启用值的 | 分隔列表的形式保存(在上面的例子中,字段存储为 |eggs|bacon|)。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
extypes-2.0.0.tar.gz (11.5 kB 查看哈希值)
构建分布
extypes-2.0.0-py2.py3-none-any.whl (6.9 kB 查看哈希值)
关闭
extypes-2.0.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 10bf455305c7e4a54bd9a5fe04a148fa2b6e33d1a7c3ce937d239fb1907f34b0 |
|
MD5 | f86006382c42b930e152458c6540b7c6 |
|
BLAKE2b-256 | 7f7b4ecf5dd02431375d4906ae58cd9bd5af1d1e6a5239f6c44fd65aec5d99e9 |
关闭
extypes-2.0.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a58468eee18e1608bff86d28fbe900b6a1af210eea026003ab7b41b1874dcba0 |
|
MD5 | dd9acdc32c82c80d6c5a1146ed25ecc8 |
|
BLAKE2b-256 | b5fc9fcaca281c7c90520656f8fdbcb1a6efa65c9a53c46385ec233a0458ae32 |