README.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. Pyrsistent
  2. ==========
  3. .. image:: https://travis-ci.org/tobgu/pyrsistent.png?branch=master
  4. :target: https://travis-ci.org/tobgu/pyrsistent
  5. .. image:: https://badge.fury.io/py/pyrsistent.svg
  6. :target: https://badge.fury.io/py/pyrsistent
  7. .. image:: https://coveralls.io/repos/tobgu/pyrsistent/badge.svg?branch=master&service=github
  8. :target: https://coveralls.io/github/tobgu/pyrsistent?branch=master
  9. .. _Pyrthon: https://www.github.com/tobgu/pyrthon/
  10. Pyrsistent is a number of persistent collections (by some referred to as functional data structures). Persistent in
  11. the sense that they are immutable.
  12. All methods on a data structure that would normally mutate it instead return a new copy of the structure containing the
  13. requested updates. The original structure is left untouched.
  14. This will simplify the reasoning about what a program does since no hidden side effects ever can take place to these
  15. data structures. You can rest assured that the object you hold a reference to will remain the same throughout its
  16. lifetime and need not worry that somewhere five stack levels below you in the darkest corner of your application
  17. someone has decided to remove that element that you expected to be there.
  18. Pyrsistent is influenced by persistent data structures such as those found in the standard library of Clojure. The
  19. data structures are designed to share common elements through path copying.
  20. It aims at taking these concepts and make them as pythonic as possible so that they can be easily integrated into any python
  21. program without hassle.
  22. If you want to go all in on persistent data structures and use literal syntax to define them in your code rather
  23. than function calls check out Pyrthon_.
  24. Examples
  25. --------
  26. .. _Sequence: collections_
  27. .. _Hashable: collections_
  28. .. _Mapping: collections_
  29. .. _Mappings: collections_
  30. .. _Set: collections_
  31. .. _collections: https://docs.python.org/3/library/collections.abc.html
  32. .. _documentation: http://pyrsistent.readthedocs.org/
  33. The collection types and key features currently implemented are:
  34. * PVector_, similar to a python list
  35. * PMap_, similar to dict
  36. * PSet_, similar to set
  37. * PRecord_, a PMap on steroids with fixed fields, optional type and invariant checking and much more
  38. * PClass_, a Python class fixed fields, optional type and invariant checking and much more
  39. * `Checked collections`_, PVector, PMap and PSet with optional type and invariance checks and more
  40. * PBag, similar to collections.Counter
  41. * PList, a classic singly linked list
  42. * PDeque, similar to collections.deque
  43. * Immutable object type (immutable) built on the named tuple
  44. * freeze_ and thaw_ functions to convert between pythons standard collections and pyrsistent collections.
  45. * Flexible transformations_ of arbitrarily complex structures built from PMaps and PVectors.
  46. Below are examples of common usage patterns for some of the structures and features. More information and
  47. full documentation for all data structures is available in the documentation_.
  48. .. _PVector:
  49. PVector
  50. ~~~~~~~
  51. With full support for the Sequence_ protocol PVector is meant as a drop in replacement to the built in list from a readers
  52. point of view. Write operations of course differ since no in place mutation is done but naming should be in line
  53. with corresponding operations on the built in list.
  54. Support for the Hashable_ protocol also means that it can be used as key in Mappings_.
  55. Appends are amortized O(1). Random access and insert is log32(n) where n is the size of the vector.
  56. .. code:: python
  57. >>> from pyrsistent import v, pvector
  58. # No mutation of vectors once created, instead they
  59. # are "evolved" leaving the original untouched
  60. >>> v1 = v(1, 2, 3)
  61. >>> v2 = v1.append(4)
  62. >>> v3 = v2.set(1, 5)
  63. >>> v1
  64. pvector([1, 2, 3])
  65. >>> v2
  66. pvector([1, 2, 3, 4])
  67. >>> v3
  68. pvector([1, 5, 3, 4])
  69. # Random access and slicing
  70. >>> v3[1]
  71. 5
  72. >>> v3[1:3]
  73. pvector([5, 3])
  74. # Iteration
  75. >>> list(x + 1 for x in v3)
  76. [2, 6, 4, 5]
  77. >>> pvector(2 * x for x in range(3))
  78. pvector([0, 2, 4])
  79. .. _PMap:
  80. PMap
  81. ~~~~
  82. With full support for the Mapping_ protocol PMap is meant as a drop in replacement to the built in dict from a readers point
  83. of view. Support for the Hashable_ protocol also means that it can be used as key in other Mappings_.
  84. Random access and insert is log32(n) where n is the size of the map.
  85. .. code:: python
  86. >>> from pyrsistent import m, pmap, v
  87. # No mutation of maps once created, instead they are
  88. # "evolved" leaving the original untouched
  89. >>> m1 = m(a=1, b=2)
  90. >>> m2 = m1.set('c', 3)
  91. >>> m3 = m2.set('a', 5)
  92. >>> m1
  93. pmap({'a': 1, 'b': 2})
  94. >>> m2
  95. pmap({'a': 1, 'c': 3, 'b': 2})
  96. >>> m3
  97. pmap({'a': 5, 'c': 3, 'b': 2})
  98. >>> m3['a']
  99. 5
  100. # Evolution of nested persistent structures
  101. >>> m4 = m(a=5, b=6, c=v(1, 2))
  102. >>> m4.transform(('c', 1), 17)
  103. pmap({'a': 5, 'c': pvector([1, 17]), 'b': 6})
  104. >>> m5 = m(a=1, b=2)
  105. # Evolve by merging with other mappings
  106. >>> m5.update(m(a=2, c=3), {'a': 17, 'd': 35})
  107. pmap({'a': 17, 'c': 3, 'b': 2, 'd': 35})
  108. >>> pmap({'x': 1, 'y': 2}) + pmap({'y': 3, 'z': 4})
  109. pmap({'y': 3, 'x': 1, 'z': 4})
  110. # Dict-like methods to convert to list and iterate
  111. >>> m3.items()
  112. pvector([('a', 5), ('c', 3), ('b', 2)])
  113. >>> list(m3)
  114. ['a', 'c', 'b']
  115. .. _PSet:
  116. PSet
  117. ~~~~
  118. With full support for the Set_ protocol PSet is meant as a drop in replacement to the built in set from a readers point
  119. of view. Support for the Hashable_ protocol also means that it can be used as key in Mappings_.
  120. Random access and insert is log32(n) where n is the size of the set.
  121. .. code:: python
  122. >>> from pyrsistent import s
  123. # No mutation of sets once created, you know the story...
  124. >>> s1 = s(1, 2, 3, 2)
  125. >>> s2 = s1.add(4)
  126. >>> s3 = s1.remove(1)
  127. >>> s1
  128. pset([1, 2, 3])
  129. >>> s2
  130. pset([1, 2, 3, 4])
  131. >>> s3
  132. pset([2, 3])
  133. # Full support for set operations
  134. >>> s1 | s(3, 4, 5)
  135. pset([1, 2, 3, 4, 5])
  136. >>> s1 & s(3, 4, 5)
  137. pset([3])
  138. >>> s1 < s2
  139. True
  140. >>> s1 < s(3, 4, 5)
  141. False
  142. .. _PRecord:
  143. PRecord
  144. ~~~~~~~
  145. A PRecord is a PMap with a fixed set of specified fields. Records are declared as python classes inheriting
  146. from PRecord. Because it is a PMap it has full support for all Mapping methods such as iteration and element
  147. access using subscript notation.
  148. .. code:: python
  149. >>> from pyrsistent import PRecord, field
  150. >>> class ARecord(PRecord):
  151. ... x = field()
  152. ...
  153. >>> r = ARecord(x=3)
  154. >>> r
  155. ARecord(x=3)
  156. >>> r.x
  157. 3
  158. >>> r.set(x=2)
  159. ARecord(x=2)
  160. >>> r.set(y=2)
  161. Traceback (most recent call last):
  162. AttributeError: 'y' is not among the specified fields for ARecord
  163. Type information
  164. ****************
  165. It is possible to add type information to the record to enforce type checks. Multiple allowed types can be specified
  166. by providing an iterable of types.
  167. .. code:: python
  168. >>> class BRecord(PRecord):
  169. ... x = field(type=int)
  170. ... y = field(type=(int, type(None)))
  171. ...
  172. >>> BRecord(x=3, y=None)
  173. BRecord(y=None, x=3)
  174. >>> BRecord(x=3.0)
  175. Traceback (most recent call last):
  176. PTypeError: Invalid type for field BRecord.x, was float
  177. Custom types (classes) that are iterable should be wrapped in a tuple to prevent their
  178. members being added to the set of valid types. Although Enums in particular are now
  179. supported without wrapping, see #83 for more information.
  180. Mandatory fields
  181. ****************
  182. Fields are not mandatory by default but can be specified as such. If fields are missing an
  183. *InvariantException* will be thrown which contains information about the missing fields.
  184. .. code:: python
  185. >>> from pyrsistent import InvariantException
  186. >>> class CRecord(PRecord):
  187. ... x = field(mandatory=True)
  188. ...
  189. >>> r = CRecord(x=3)
  190. >>> try:
  191. ... r.discard('x')
  192. ... except InvariantException as e:
  193. ... print(e.missing_fields)
  194. ...
  195. ('CRecord.x',)
  196. Invariants
  197. **********
  198. It is possible to add invariants that must hold when evolving the record. Invariants can be
  199. specified on both field and record level. If invariants fail an *InvariantException* will be
  200. thrown which contains information about the failing invariants. An invariant function should
  201. return a tuple consisting of a boolean that tells if the invariant holds or not and an object
  202. describing the invariant. This object can later be used to identify which invariant that failed.
  203. The global invariant function is only executed if all field invariants hold.
  204. Global invariants are inherited to subclasses.
  205. .. code:: python
  206. >>> class RestrictedVector(PRecord):
  207. ... __invariant__ = lambda r: (r.y >= r.x, 'x larger than y')
  208. ... x = field(invariant=lambda x: (x > 0, 'x negative'))
  209. ... y = field(invariant=lambda y: (y > 0, 'y negative'))
  210. ...
  211. >>> r = RestrictedVector(y=3, x=2)
  212. >>> try:
  213. ... r.set(x=-1, y=-2)
  214. ... except InvariantException as e:
  215. ... print(e.invariant_errors)
  216. ...
  217. ('y negative', 'x negative')
  218. >>> try:
  219. ... r.set(x=2, y=1)
  220. ... except InvariantException as e:
  221. ... print(e.invariant_errors)
  222. ...
  223. ('x larger than y',)
  224. Invariants may also contain multiple assertions. For those cases the invariant function should
  225. return a tuple of invariant tuples as described above. This structure is reflected in the
  226. invariant_errors attribute of the exception which will contain tuples with data from all failed
  227. invariants. Eg:
  228. .. code:: python
  229. >>> class EvenX(PRecord):
  230. ... x = field(invariant=lambda x: ((x > 0, 'x negative'), (x % 2 == 0, 'x odd')))
  231. ...
  232. >>> try:
  233. ... EvenX(x=-1)
  234. ... except InvariantException as e:
  235. ... print(e.invariant_errors)
  236. ...
  237. (('x negative', 'x odd'),)
  238. Factories
  239. *********
  240. It's possible to specify factory functions for fields. The factory function receives whatever
  241. is supplied as field value and the actual returned by the factory is assigned to the field
  242. given that any type and invariant checks hold.
  243. PRecords have a default factory specified as a static function on the class, create(). It takes
  244. a *Mapping* as argument and returns an instance of the specific record.
  245. If a record has fields of type PRecord the create() method of that record will
  246. be called to create the "sub record" if no factory has explicitly been specified to override
  247. this behaviour.
  248. .. code:: python
  249. >>> class DRecord(PRecord):
  250. ... x = field(factory=int)
  251. ...
  252. >>> class ERecord(PRecord):
  253. ... d = field(type=DRecord)
  254. ...
  255. >>> ERecord.create({'d': {'x': '1'}})
  256. ERecord(d=DRecord(x=1))
  257. Collection fields
  258. *****************
  259. It is also possible to have fields with ``pyrsistent`` collections.
  260. .. code:: python
  261. >>> from pyrsistent import pset_field, pmap_field, pvector_field
  262. >>> class MultiRecord(PRecord):
  263. ... set_of_ints = pset_field(int)
  264. ... map_int_to_str = pmap_field(int, str)
  265. ... vector_of_strs = pvector_field(str)
  266. ...
  267. Serialization
  268. *************
  269. PRecords support serialization back to dicts. Default serialization will take keys and values
  270. "as is" and output them into a dict. It is possible to specify custom serialization functions
  271. to take care of fields that require special treatment.
  272. .. code:: python
  273. >>> from datetime import date
  274. >>> class Person(PRecord):
  275. ... name = field(type=unicode)
  276. ... birth_date = field(type=date,
  277. ... serializer=lambda format, d: d.strftime(format['date']))
  278. ...
  279. >>> john = Person(name=u'John', birth_date=date(1985, 10, 21))
  280. >>> john.serialize({'date': '%Y-%m-%d'})
  281. {'birth_date': '1985-10-21', 'name': u'John'}
  282. .. _instar: https://github.com/boxed/instar/
  283. .. _PClass:
  284. PClass
  285. ~~~~~~
  286. A PClass is a python class with a fixed set of specified fields. PClasses are declared as python classes inheriting
  287. from PClass. It is defined the same way that PRecords are and behaves like a PRecord in all aspects except that it
  288. is not a PMap and hence not a collection but rather a plain Python object.
  289. .. code:: python
  290. >>> from pyrsistent import PClass, field
  291. >>> class AClass(PClass):
  292. ... x = field()
  293. ...
  294. >>> a = AClass(x=3)
  295. >>> a
  296. AClass(x=3)
  297. >>> a.x
  298. 3
  299. Checked collections
  300. ~~~~~~~~~~~~~~~~~~~
  301. Checked collections currently come in three flavors: CheckedPVector, CheckedPMap and CheckedPSet.
  302. .. code:: python
  303. >>> from pyrsistent import CheckedPVector, CheckedPMap, CheckedPSet, thaw
  304. >>> class Positives(CheckedPSet):
  305. ... __type__ = (long, int)
  306. ... __invariant__ = lambda n: (n >= 0, 'Negative')
  307. ...
  308. >>> class Lottery(PRecord):
  309. ... name = field(type=str)
  310. ... numbers = field(type=Positives, invariant=lambda p: (len(p) > 0, 'No numbers'))
  311. ...
  312. >>> class Lotteries(CheckedPVector):
  313. ... __type__ = Lottery
  314. ...
  315. >>> class LotteriesByDate(CheckedPMap):
  316. ... __key_type__ = date
  317. ... __value_type__ = Lotteries
  318. ...
  319. >>> lotteries = LotteriesByDate.create({date(2015, 2, 15): [{'name': 'SuperLotto', 'numbers': {1, 2, 3}},
  320. ... {'name': 'MegaLotto', 'numbers': {4, 5, 6}}],
  321. ... date(2015, 2, 16): [{'name': 'SuperLotto', 'numbers': {3, 2, 1}},
  322. ... {'name': 'MegaLotto', 'numbers': {6, 5, 4}}]})
  323. >>> lotteries
  324. LotteriesByDate({datetime.date(2015, 2, 15): Lotteries([Lottery(numbers=Positives([1, 2, 3]), name='SuperLotto'), Lottery(numbers=Positives([4, 5, 6]), name='MegaLotto')]), datetime.date(2015, 2, 16): Lotteries([Lottery(numbers=Positives([1, 2, 3]), name='SuperLotto'), Lottery(numbers=Positives([4, 5, 6]), name='MegaLotto')])})
  325. # The checked versions support all operations that the corresponding
  326. # unchecked types do
  327. >>> lottery_0215 = lotteries[date(2015, 2, 15)]
  328. >>> lottery_0215.transform([0, 'name'], 'SuperDuperLotto')
  329. Lotteries([Lottery(numbers=Positives([1, 2, 3]), name='SuperDuperLotto'), Lottery(numbers=Positives([4, 5, 6]), name='MegaLotto')])
  330. # But also makes asserts that types and invariants hold
  331. >>> lottery_0215.transform([0, 'name'], 999)
  332. Traceback (most recent call last):
  333. PTypeError: Invalid type for field Lottery.name, was int
  334. >>> lottery_0215.transform([0, 'numbers'], set())
  335. Traceback (most recent call last):
  336. InvariantException: Field invariant failed
  337. # They can be converted back to python built ins with either thaw()
  338. # or serialize() (which provides possibilities to customize serialization)
  339. >>> thaw(lottery_0215)
  340. [{'numbers': set([1, 2, 3]), 'name': 'SuperLotto'}, {'numbers': set([4, 5, 6]), 'name': 'MegaLotto'}]
  341. >>> lottery_0215.serialize()
  342. [{'numbers': set([1, 2, 3]), 'name': 'SuperLotto'}, {'numbers': set([4, 5, 6]), 'name': 'MegaLotto'}]
  343. .. _transformations:
  344. Transformations
  345. ~~~~~~~~~~~~~~~
  346. Transformations are inspired by the cool library instar_ for Clojure. They let you evolve PMaps and PVectors
  347. with arbitrarily deep/complex nesting using simple syntax and flexible matching syntax.
  348. The first argument to transformation is the path that points out the value to transform. The
  349. second is the transformation to perform. If the transformation is callable it will be applied
  350. to the value(s) matching the path. The path may also contain callables. In that case they are
  351. treated as matchers. If the matcher returns True for a specific key it is considered for transformation.
  352. .. code:: python
  353. # Basic examples
  354. >>> from pyrsistent import inc, freeze, thaw, rex, ny, discard
  355. >>> v1 = freeze([1, 2, 3, 4, 5])
  356. >>> v1.transform([2], inc)
  357. pvector([1, 2, 4, 4, 5])
  358. >>> v1.transform([lambda ix: 0 < ix < 4], 8)
  359. pvector([1, 8, 8, 8, 5])
  360. >>> v1.transform([lambda ix, v: ix == 0 or v == 5], 0)
  361. pvector([0, 2, 3, 4, 0])
  362. # The (a)ny matcher can be used to match anything
  363. >>> v1.transform([ny], 8)
  364. pvector([8, 8, 8, 8, 8])
  365. # Regular expressions can be used for matching
  366. >>> scores = freeze({'John': 12, 'Joseph': 34, 'Sara': 23})
  367. >>> scores.transform([rex('^Jo')], 0)
  368. pmap({'Joseph': 0, 'Sara': 23, 'John': 0})
  369. # Transformations can be done on arbitrarily deep structures
  370. >>> news_paper = freeze({'articles': [{'author': 'Sara', 'content': 'A short article'},
  371. ... {'author': 'Steve', 'content': 'A slightly longer article'}],
  372. ... 'weather': {'temperature': '11C', 'wind': '5m/s'}})
  373. >>> short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:25] + '...' if len(c) > 25 else c)
  374. >>> very_short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:15] + '...' if len(c) > 15 else c)
  375. >>> very_short_news.articles[0].content
  376. 'A short article'
  377. >>> very_short_news.articles[1].content
  378. 'A slightly long...'
  379. # When nothing has been transformed the original data structure is kept
  380. >>> short_news is news_paper
  381. True
  382. >>> very_short_news is news_paper
  383. False
  384. >>> very_short_news.articles[0] is news_paper.articles[0]
  385. True
  386. # There is a special transformation that can be used to discard elements. Also
  387. # multiple transformations can be applied in one call
  388. >>> thaw(news_paper.transform(['weather'], discard, ['articles', ny, 'content'], discard))
  389. {'articles': [{'author': 'Sara'}, {'author': 'Steve'}]}
  390. Evolvers
  391. ~~~~~~~~
  392. PVector, PMap and PSet all have support for a concept dubbed *evolvers*. An evolver acts like a mutable
  393. view of the underlying persistent data structure with "transaction like" semantics. No updates of the original
  394. data structure is ever performed, it is still fully immutable.
  395. The evolvers have a very limited API by design to discourage excessive, and inappropriate, usage as that would
  396. take us down the mutable road. In principle only basic mutation and element access functions are supported.
  397. Check out the documentation_ of each data structure for specific examples.
  398. Examples of when you may want to use an evolver instead of working directly with the data structure include:
  399. * Multiple updates are done to the same data structure and the intermediate results are of no
  400. interest. In this case using an evolver may be a more efficient and easier to work with.
  401. * You need to pass a vector into a legacy function or a function that you have no control
  402. over which performs in place mutations. In this case pass an evolver instance
  403. instead and then create a new pvector from the evolver once the function returns.
  404. .. code:: python
  405. >>> from pyrsistent import v
  406. # In place mutation as when working with the built in counterpart
  407. >>> v1 = v(1, 2, 3)
  408. >>> e = v1.evolver()
  409. >>> e[1] = 22
  410. >>> e = e.append(4)
  411. >>> e = e.extend([5, 6])
  412. >>> e[5] += 1
  413. >>> len(e)
  414. 6
  415. # The evolver is considered *dirty* when it contains changes compared to the underlying vector
  416. >>> e.is_dirty()
  417. True
  418. # But the underlying pvector still remains untouched
  419. >>> v1
  420. pvector([1, 2, 3])
  421. # Once satisfied with the updates you can produce a new pvector containing the updates.
  422. # The new pvector will share data with the original pvector in the same way that would have
  423. # been done if only using operations on the pvector.
  424. >>> v2 = e.persistent()
  425. >>> v2
  426. pvector([1, 22, 3, 4, 5, 7])
  427. # The evolver is now no longer considered *dirty* as it contains no differences compared to the
  428. # pvector just produced.
  429. >>> e.is_dirty()
  430. False
  431. # You may continue to work with the same evolver without affecting the content of v2
  432. >>> e[0] = 11
  433. # Or create a new evolver from v2. The two evolvers can be updated independently but will both
  434. # share data with v2 where possible.
  435. >>> e2 = v2.evolver()
  436. >>> e2[0] = 1111
  437. >>> e.persistent()
  438. pvector([11, 22, 3, 4, 5, 7])
  439. >>> e2.persistent()
  440. pvector([1111, 22, 3, 4, 5, 7])
  441. .. _freeze:
  442. .. _thaw:
  443. freeze and thaw
  444. ~~~~~~~~~~~~~~~
  445. These functions are great when your cozy immutable world has to interact with the evil mutable world outside.
  446. .. code:: python
  447. >>> from pyrsistent import freeze, thaw, v, m
  448. >>> freeze([1, {'a': 3}])
  449. pvector([1, pmap({'a': 3})])
  450. >>> thaw(v(1, m(a=3)))
  451. [1, {'a': 3}]
  452. Compatibility
  453. -------------
  454. Pyrsistent is developed and tested on Python 2.7, 3.5, 3.6, 3.7 and PyPy (Python 2 and 3 compatible). It will most
  455. likely work on all other versions >= 3.4 but no guarantees are given. :)
  456. Compatibility issues
  457. ~~~~~~~~~~~~~~~~~~~~
  458. .. _27: https://github.com/tobgu/pyrsistent/issues/27
  459. There is currently one known compatibility issue when comparing built in sets and frozensets to PSets as discussed in 27_.
  460. It affects python 2 versions < 2.7.8 and python 3 versions < 3.4.0 and is due to a bug described in
  461. http://bugs.python.org/issue8743.
  462. Comparisons will fail or be incorrect when using the set/frozenset as left hand side of the comparison. As a workaround
  463. you need to either upgrade Python to a more recent version, avoid comparing sets/frozensets with PSets or always make
  464. sure to convert both sides of the comparison to the same type before performing the comparison.
  465. Performance
  466. -----------
  467. Pyrsistent is developed with performance in mind. Still, while some operations are nearly on par with their built in,
  468. mutable, counterparts in terms of speed, other operations are slower. In the cases where attempts at
  469. optimizations have been done, speed has generally been valued over space.
  470. Pyrsistent comes with two API compatible flavors of PVector (on which PMap and PSet are based), one pure Python
  471. implementation and one implemented as a C extension. The latter generally being 2 - 20 times faster than the former.
  472. The C extension will be used automatically when possible.
  473. The pure python implementation is fully PyPy compatible. Running it under PyPy speeds operations up considerably if
  474. the structures are used heavily (if JITed), for some cases the performance is almost on par with the built in counterparts.
  475. Type hints
  476. ----------
  477. PEP 561 style type hints for use with mypy and various editors are available for most types and functions in pyrsistent.
  478. Type classes for annotating your own code with pyrsistent types are also available under pyrsistent.typing.
  479. Installation
  480. ------------
  481. pip install pyrsistent
  482. Documentation
  483. -------------
  484. Available at http://pyrsistent.readthedocs.org/
  485. Brief presentation available at http://slides.com/tobiasgustafsson/immutability-and-python/
  486. Contributors
  487. ------------
  488. Tobias Gustafsson https://github.com/tobgu
  489. Christopher Armstrong https://github.com/radix
  490. Anders Hovmöller https://github.com/boxed
  491. Itamar Turner-Trauring https://github.com/itamarst
  492. Jonathan Lange https://github.com/jml
  493. Richard Futrell https://github.com/Futrell
  494. Jakob Hollenstein https://github.com/jkbjh
  495. David Honour https://github.com/foolswood
  496. David R. MacIver https://github.com/DRMacIver
  497. Marcus Ewert https://github.com/sarum90
  498. Jean-Paul Calderone https://github.com/exarkun
  499. Douglas Treadwell https://github.com/douglas-treadwell
  500. Travis Parker https://github.com/teepark
  501. Julian Berman https://github.com/Julian
  502. Dennis Tomas https://github.com/dtomas
  503. Neil Vyas https://github.com/neilvyas
  504. doozr https://github.com/doozr
  505. Kamil Galuszka https://github.com/galuszkak
  506. Tsuyoshi Hombashi https://github.com/thombashi
  507. nattofriends https://github.com/nattofriends
  508. agberk https://github.com/agberk
  509. Waleed Khan https://github.com/arxanas
  510. Jean-Louis Fuchs https://github.com/ganwell
  511. Carlos Corbacho https://github.com/ccorbacho
  512. Felix Yan https://github.com/felixonmars
  513. benrg https://github.com/benrg
  514. Jere Lahelma https://github.com/je-l
  515. Max Taggart https://github.com/MaxTaggart
  516. Vincent Philippon https://github.com/vphilippon
  517. Semen Zhydenko https://github.com/ss18
  518. Till Varoquaux https://github.com/till-varoquaux
  519. Michal Kowalik https://github.com/michalvi
  520. ossdev07 https://github.com/ossdev07
  521. Kerry Olesen https://github.com/qhesz
  522. Contributing
  523. ------------
  524. Want to contribute? That's great! If you experience problems please log them on GitHub. If you want to contribute code,
  525. please fork the repository and submit a pull request.
  526. Run tests
  527. ~~~~~~~~~
  528. .. _tox: https://tox.readthedocs.io/en/latest/
  529. Tests can be executed using tox_.
  530. Install tox: ``pip install tox``
  531. Run test for Python 2.7: ``tox -epy27``
  532. Release
  533. ~~~~~~~
  534. * Update CHANGES.txt
  535. * Update README with any new contributors and potential info needed.
  536. * Update _pyrsistent_version.py
  537. * python setup.py sdist upload
  538. * Commit and tag with new version: git add -u . && git commit -m 'Prepare version vX.Y.Z' && git tag -a vX.Y.Z -m 'vX.Y.Z'
  539. * Push commit and tags: git push && git push --tags
  540. Project status
  541. --------------
  542. Pyrsistent can be considered stable and mature (who knows, there may even be a 1.0 some day :-)). The project is
  543. maintained, bugs fixed, PRs reviewed and merged and new releases made. I currently do not have time for development
  544. of new features or functionality which I don't have use for myself. I'm more than happy to take PRs for new
  545. functionality though!
  546. There are a bunch of issues marked with ``enhancement`` and ``help wanted`` that contain requests for new functionality
  547. that would be nice to include. The level of difficulty and extend of the issues varies, please reach out to me if you're
  548. interested in working on any of them.
  549. If you feel that you have a grand master plan for where you would like Pyrsistent to go and have the time to put into
  550. it please don't hesitate to discuss this with me and submit PRs for it. If all goes well I'd be more than happy to add
  551. additional maintainers to the project!