• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

pyrsistent/H07-May-2022-4,7563,605

LICENCE.mitH A D31-Mar-20221 KiB2219

MANIFEST.inH A D31-Mar-2022111 55

PKG-INFOH A D31-Mar-202231.6 KiB743568

READMEH A D31-Mar-202225.3 KiB726552

README.rstH A D31-Mar-202225.3 KiB726552

_pyrsistent_version.pyH A D31-Mar-202223 21

pvectorcmodule.cH A D31-Mar-202249.6 KiB1,6431,249

setup.cfgH A D31-Mar-202263 85

setup.pyH A D31-Mar-20222.8 KiB8272

README

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

README.rst

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