跳转到主要内容

持久集合是具有标准Python集合API的持久化对象

项目描述

0.2 (2020-05-14)

  • 使Python-3兼容

  • 添加对PyPy和PyPy3的支持。

0.1 (2007-05-09)

  • 从未发布到PyPI

持久集合是具有标准Python集合API的持久化对象。持久集合应该与正常集合的工作方式相同,但对其的更改是持久的。

它们与持久列表和持久映射具有相同的限制,如《persistent》包中所示:与《BTree》包数据结构不同,更改会复制数据库中的整个对象。这通常意味着持久集合,如持久列表和持久映射,不适用于非常大的集合。对于这些集合,请使用《BTree》数据结构。

此文件其余部分是测试,而不是文档。有关Python集合API的信息,请参阅标准Python文档(例如,https://docs.pythonlang.cn/lib/types-set.html),有关持久性的信息,请参阅ZODB文档(例如,http://www.zope.org/Wikis/ZODB/FrontPage/guide/index.html)。

持久集合模块包含一个简单的持久化集合版本,它继承自持久化.Persistent并在任何可能修改的操作中标记_p_changed = True。

>>> from ZODB.tests.util import DB
>>> db = DB()
>>> conn = db.open()
>>> root = conn.root()
>>> import zope.app.folder # import rootFolder
>>> app = root['Application'] = zope.app.folder.rootFolder()
>>> import transaction
>>> transaction.commit()
>>> from zc.set import Set
>>> s = Set()
>>> app['s'] = s
>>> transaction.commit()
>>> import persistent.interfaces
>>> persistent.interfaces.IPersistent.providedBy(s)
True
>>> original = factory() # set in one test run; a persistent set in another
>>> sorted(set(dir(original)) - set(dir(s)))
[]

添加集合_p_changed

>>> s._p_changed = False
>>> s.add(1) # add
>>> s._p_changed
True
__repr__包括模块、类以及类似于正常集合的内容视图
>>> s # __repr__
zc.set.Set([1])

update按正常方式工作,但设置_p_changed

>>> s._p_changed = False
>>> s.update((2,3,4,5,6,7)) # update
>>> s._p_changed
True

__iter__工作正常

>>> sorted(s) # __iter__
[1, 2, 3, 4, 5, 6, 7]

__len__工作正常

>>> len(s)
7

__contains__也正常工作

>>> 3 in s
True
>>> 'kumquat' in s
False

__gt__、__ge__、__eq__、__ne__、__lt__和__le__正常工作,与正常集合相等,至少如果拼写正确。

>>> s > original
True
>>> s >= original
True
>>> s < original
False
>>> s <= original
False
>>> s == original
False
>>> s != original
True
>>> original.update(s)
>>> s > original
False
>>> s >= original
True
>>> s < original
False
>>> s <= original
True
>>> s == original
True
>>> s != original
False
>>> original.add(8)
>>> s > original
False
>>> s >= original
False
>>> s < original
True
>>> s <= original
True
>>> s == original
False
>>> s != original
True

我不知道__cmp__ supposed to do什么——它不与集合一起工作——所以我不会测试它。

当它是子集时,issubset和issuperset可以正常工作。

>>> s.issubset(original)
True
>>> s.issuperset(original)
False

__ior__可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s |= original
>>> s._p_changed
True
>>> s == original
True

当集合相等时,issubset和issuperset可以正常工作。

>>> s.issubset(original)
True
>>> s.issuperset(original)
True

当它是超集时,issubset和issuperset可以正常工作。

>>> s.add(9)
>>> s.issubset(original)
False
>>> s.issuperset(original)
True

__hash__可以正常工作,按照预期引发错误。

>>> hash(original)
Traceback (most recent call last):
...
TypeError:...unhashable...

__iand__可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s &= original
>>> s._p_changed
True
>>> sorted(s)
[1, 2, 3, 4, 5, 6, 7, 8]

__isub__可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s -= factory((1, 2, 3, 4, 5, 6, 7))
>>> s._p_changed
True
>>> sorted(s)
[8]

__ixor__可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s ^= original
>>> s._p_changed
True
>>> sorted(s)
[1, 2, 3, 4, 5, 6, 7]

difference_update可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s.difference_update((7, 8))
>>> s._p_changed
True
>>> sorted(s)
[1, 2, 3, 4, 5, 6]

intersection_update可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s.intersection_update((2, 3, 4, 5, 6, 7))
>>> s._p_changed
True
>>> sorted(s)
[2, 3, 4, 5, 6]

symmetric_difference_update可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> original.add(9)
>>> s.symmetric_difference_update(original)
>>> s._p_changed
True
>>> sorted(s)
[1, 7, 8, 9]

remove可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s.remove(1)
>>> s._p_changed
True
>>> sorted(s)
[7, 8, 9]

如果引发错误,则_p_changed不会设置。

>>> s._p_changed = False
>>> s.remove(1)
Traceback (most recent call last):
...
KeyError: 1
>>> s._p_changed
False
>>> sorted(s)
[7, 8, 9]

discard可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s.discard(9)
>>> s._p_changed
True
>>> sorted(s)
[7, 8]

如果您放弃不在集合中的内容,_p_changed仍然会被设置。这更多的是一个效率决策,而不是我们期望的行为。

>>> s._p_changed = False
>>> s.discard(9)
>>> s._p_changed
True
>>> sorted(s)
[7, 8]

pop可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s.pop() in (7, 8)
True
>>> s._p_changed
True
>>> len(s)
1

clear可以正常工作,包括设置_p_changed

>>> s._p_changed = False
>>> s.clear()
>>> s._p_changed
True
>>> len(s)
0

所有返回集合的方法都返回持久集合。它们在其他方面工作相同。

__and__

>>> s.update((0,1,2,3,4))
>>> res = s & original
>>> sorted(res)
[1, 2, 3, 4]
>>> res.__class__ is s.__class__
True

__or__

>>> res = s | original
>>> sorted(res)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> res.__class__ is s.__class__
True

__sub__

>>> res = s - original
>>> sorted(res)
[0]
>>> res.__class__ is s.__class__
True

__xor__

>>> res = s ^ original
>>> sorted(res)
[0, 5, 6, 7, 8, 9]
>>> res.__class__ is s.__class__
True

__rand__

>>> res = set((3,4,5)) & s
>>> sorted(res)
[3, 4]
>>> res.__class__ is s.__class__
True

__ror__

>>> res = set((3,4,5)) | s
>>> sorted(res)
[0, 1, 2, 3, 4, 5]
>>> res.__class__ is s.__class__
True

__rsub__

>>> res = set((3,4,5)) - s
>>> sorted(res)
[5]
>>> res.__class__ is s.__class__
True

__rxor__

>>> res = set((3,4,5)) ^ s
>>> sorted(res)
[0, 1, 2, 5]
>>> res.__class__ is s.__class__
True

difference

>>> res = s.difference((3,4,5))
>>> sorted(res)
[0, 1, 2]
>>> res.__class__ is s.__class__
True

intersection

>>> res = s.intersection((3,4,5))
>>> sorted(res)
[3, 4]
>>> res.__class__ is s.__class__
True

symmetric_difference

>>> res = s.symmetric_difference((3,4,5))
>>> sorted(res)
[0, 1, 2, 5]
>>> res.__class__ is s.__class__
True

union

>>> res = s.union((3,4,5))
>>> sorted(res)
[0, 1, 2, 3, 4, 5]
>>> res.__class__ is s.__class__
True

copy返回…一个副本。

>>> res = s.copy()
>>> res == s
True
>>> res.__class__ is s.__class__
True

项目详情


下载文件

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

源代码分发

zc.set-0.2.tar.gz (10.2 kB 查看哈希值)

上传时间 源代码

已构建的发行版

zc.set-0.2-py3-none-any.whl (8.7 kB 查看哈希值)

上传时间 Python 3

zc.set-0.2-py2.py3-none-any.whl (8.7 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者

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