Persistent/Immutable/Functional data structures for Python

Overview

Pyrsistent

Pyrsistent is a number of persistent collections (by some referred to as functional data structures). Persistent in the sense that they are immutable.

All methods on a data structure that would normally mutate it instead return a new copy of the structure containing the requested updates. The original structure is left untouched.

This will simplify the reasoning about what a program does since no hidden side effects ever can take place to these data structures. You can rest assured that the object you hold a reference to will remain the same throughout its lifetime and need not worry that somewhere five stack levels below you in the darkest corner of your application someone has decided to remove that element that you expected to be there.

Pyrsistent is influenced by persistent data structures such as those found in the standard library of Clojure. The data structures are designed to share common elements through path copying. It aims at taking these concepts and make them as pythonic as possible so that they can be easily integrated into any python program without hassle.

If you want to go all in on persistent data structures and use literal syntax to define them in your code rather than function calls check out Pyrthon.

Examples

The collection types and key features currently implemented are:

  • PVector, similar to a python list
  • PMap, similar to dict
  • PSet, similar to set
  • PRecord, a PMap on steroids with fixed fields, optional type and invariant checking and much more
  • PClass, a Python class fixed fields, optional type and invariant checking and much more
  • Checked collections, PVector, PMap and PSet with optional type and invariance checks and more
  • PBag, similar to collections.Counter
  • PList, a classic singly linked list
  • PDeque, similar to collections.deque
  • Immutable object type (immutable) built on the named tuple
  • freeze and thaw functions to convert between pythons standard collections and pyrsistent collections.
  • Flexible transformations of arbitrarily complex structures built from PMaps and PVectors.

Below are examples of common usage patterns for some of the structures and features. More information and full documentation for all data structures is available in the documentation.

PVector

With full support for the Sequence protocol PVector is meant as a drop in replacement to the built in list from a readers point of view. Write operations of course differ since no in place mutation is done but naming should be in line with corresponding operations on the built in list.

Support for the Hashable protocol also means that it can be used as key in Mappings.

Appends are amortized O(1). Random access and insert is log32(n) where n is the size of the vector.

>>> from pyrsistent import v, pvector

# No mutation of vectors once created, instead they
# are "evolved" leaving the original untouched
>>> v1 = v(1, 2, 3)
>>> v2 = v1.append(4)
>>> v3 = v2.set(1, 5)
>>> v1
pvector([1, 2, 3])
>>> v2
pvector([1, 2, 3, 4])
>>> v3
pvector([1, 5, 3, 4])

# Random access and slicing
>>> v3[1]
5
>>> v3[1:3]
pvector([5, 3])

# Iteration
>>> list(x + 1 for x in v3)
[2, 6, 4, 5]
>>> pvector(2 * x for x in range(3))
pvector([0, 2, 4])

PMap

With full support for the Mapping protocol PMap is meant as a drop in replacement to the built in dict from a readers point of view. Support for the Hashable protocol also means that it can be used as key in other Mappings.

Random access and insert is log32(n) where n is the size of the map.

>>> from pyrsistent import m, pmap, v

# No mutation of maps once created, instead they are
# "evolved" leaving the original untouched
>>> m1 = m(a=1, b=2)
>>> m2 = m1.set('c', 3)
>>> m3 = m2.set('a', 5)
>>> m1
pmap({'a': 1, 'b': 2})
>>> m2
pmap({'a': 1, 'c': 3, 'b': 2})
>>> m3
pmap({'a': 5, 'c': 3, 'b': 2})
>>> m3['a']
5

# Evolution of nested persistent structures
>>> m4 = m(a=5, b=6, c=v(1, 2))
>>> m4.transform(('c', 1), 17)
pmap({'a': 5, 'c': pvector([1, 17]), 'b': 6})
>>> m5 = m(a=1, b=2)

# Evolve by merging with other mappings
>>> m5.update(m(a=2, c=3), {'a': 17, 'd': 35})
pmap({'a': 17, 'c': 3, 'b': 2, 'd': 35})
>>> pmap({'x': 1, 'y': 2}) + pmap({'y': 3, 'z': 4})
pmap({'y': 3, 'x': 1, 'z': 4})

# Dict-like methods to convert to list and iterate
>>> m3.items()
pvector([('a', 5), ('c', 3), ('b', 2)])
>>> list(m3)
['a', 'c', 'b']

PSet

With full support for the Set protocol PSet is meant as a drop in replacement to the built in set from a readers point of view. Support for the Hashable protocol also means that it can be used as key in Mappings.

Random access and insert is log32(n) where n is the size of the set.

>>> from pyrsistent import s

# No mutation of sets once created, you know the story...
>>> s1 = s(1, 2, 3, 2)
>>> s2 = s1.add(4)
>>> s3 = s1.remove(1)
>>> s1
pset([1, 2, 3])
>>> s2
pset([1, 2, 3, 4])
>>> s3
pset([2, 3])

# Full support for set operations
>>> s1 | s(3, 4, 5)
pset([1, 2, 3, 4, 5])
>>> s1 & s(3, 4, 5)
pset([3])
>>> s1 < s2
True
>>> s1 < s(3, 4, 5)
False

PRecord

A PRecord is a PMap with a fixed set of specified fields. Records are declared as python classes inheriting from PRecord. Because it is a PMap it has full support for all Mapping methods such as iteration and element access using subscript notation.

>>> from pyrsistent import PRecord, field
>>> class ARecord(PRecord):
...     x = field()
...
>>> r = ARecord(x=3)
>>> r
ARecord(x=3)
>>> r.x
3
>>> r.set(x=2)
ARecord(x=2)
>>> r.set(y=2)
Traceback (most recent call last):
AttributeError: 'y' is not among the specified fields for ARecord

Type information

It is possible to add type information to the record to enforce type checks. Multiple allowed types can be specified by providing an iterable of types.

>>> class BRecord(PRecord):
...     x = field(type=int)
...     y = field(type=(int, type(None)))
...
>>> BRecord(x=3, y=None)
BRecord(y=None, x=3)
>>> BRecord(x=3.0)
Traceback (most recent call last):
PTypeError: Invalid type for field BRecord.x, was float

Custom types (classes) that are iterable should be wrapped in a tuple to prevent their members being added to the set of valid types. Although Enums in particular are now supported without wrapping, see #83 for more information.

Mandatory fields

Fields are not mandatory by default but can be specified as such. If fields are missing an InvariantException will be thrown which contains information about the missing fields.

>>> from pyrsistent import InvariantException
>>> class CRecord(PRecord):
...     x = field(mandatory=True)
...
>>> r = CRecord(x=3)
>>> try:
...    r.discard('x')
... except InvariantException as e:
...    print(e.missing_fields)
...
('CRecord.x',)

Invariants

It is possible to add invariants that must hold when evolving the record. Invariants can be specified on both field and record level. If invariants fail an InvariantException will be thrown which contains information about the failing invariants. An invariant function should return a tuple consisting of a boolean that tells if the invariant holds or not and an object describing the invariant. This object can later be used to identify which invariant that failed.

The global invariant function is only executed if all field invariants hold.

Global invariants are inherited to subclasses.

>>> class RestrictedVector(PRecord):
...     __invariant__ = lambda r: (r.y >= r.x, 'x larger than y')
...     x = field(invariant=lambda x: (x > 0, 'x negative'))
...     y = field(invariant=lambda y: (y > 0, 'y negative'))
...
>>> r = RestrictedVector(y=3, x=2)
>>> try:
...    r.set(x=-1, y=-2)
... except InvariantException as e:
...    print(e.invariant_errors)
...
('y negative', 'x negative')
>>> try:
...    r.set(x=2, y=1)
... except InvariantException as e:
...    print(e.invariant_errors)
...
('x larger than y',)

Invariants may also contain multiple assertions. For those cases the invariant function should return a tuple of invariant tuples as described above. This structure is reflected in the invariant_errors attribute of the exception which will contain tuples with data from all failed invariants. Eg:

>>> class EvenX(PRecord):
...     x = field(invariant=lambda x: ((x > 0, 'x negative'), (x % 2 == 0, 'x odd')))
...
>>> try:
...    EvenX(x=-1)
... except InvariantException as e:
...    print(e.invariant_errors)
...
(('x negative', 'x odd'),)

Factories

It's possible to specify factory functions for fields. The factory function receives whatever is supplied as field value and the actual returned by the factory is assigned to the field given that any type and invariant checks hold. PRecords have a default factory specified as a static function on the class, create(). It takes a Mapping as argument and returns an instance of the specific record. If a record has fields of type PRecord the create() method of that record will be called to create the "sub record" if no factory has explicitly been specified to override this behaviour.

>>> class DRecord(PRecord):
...     x = field(factory=int)
...
>>> class ERecord(PRecord):
...     d = field(type=DRecord)
...
>>> ERecord.create({'d': {'x': '1'}})
ERecord(d=DRecord(x=1))

Collection fields

It is also possible to have fields with pyrsistent collections.

>>> from pyrsistent import pset_field, pmap_field, pvector_field
>>> class MultiRecord(PRecord):
...     set_of_ints = pset_field(int)
...     map_int_to_str = pmap_field(int, str)
...     vector_of_strs = pvector_field(str)
...

Serialization

PRecords support serialization back to dicts. Default serialization will take keys and values "as is" and output them into a dict. It is possible to specify custom serialization functions to take care of fields that require special treatment.

>>> from datetime import date
>>> class Person(PRecord):
...     name = field(type=unicode)
...     birth_date = field(type=date,
...                        serializer=lambda format, d: d.strftime(format['date']))
...
>>> john = Person(name=u'John', birth_date=date(1985, 10, 21))
>>> john.serialize({'date': '%Y-%m-%d'})
{'birth_date': '1985-10-21', 'name': u'John'}

PClass

A PClass is a python class with a fixed set of specified fields. PClasses are declared as python classes inheriting from PClass. It is defined the same way that PRecords are and behaves like a PRecord in all aspects except that it is not a PMap and hence not a collection but rather a plain Python object.

>>> from pyrsistent import PClass, field
>>> class AClass(PClass):
...     x = field()
...
>>> a = AClass(x=3)
>>> a
AClass(x=3)
>>> a.x
3

Checked collections

Checked collections currently come in three flavors: CheckedPVector, CheckedPMap and CheckedPSet.

>>> from pyrsistent import CheckedPVector, CheckedPMap, CheckedPSet, thaw
>>> class Positives(CheckedPSet):
...     __type__ = (long, int)
...     __invariant__ = lambda n: (n >= 0, 'Negative')
...
>>> class Lottery(PRecord):
...     name = field(type=str)
...     numbers = field(type=Positives, invariant=lambda p: (len(p) > 0, 'No numbers'))
...
>>> class Lotteries(CheckedPVector):
...     __type__ = Lottery
...
>>> class LotteriesByDate(CheckedPMap):
...     __key_type__ = date
...     __value_type__ = Lotteries
...
>>> lotteries = LotteriesByDate.create({date(2015, 2, 15): [{'name': 'SuperLotto', 'numbers': {1, 2, 3}},
...                                                         {'name': 'MegaLotto',  'numbers': {4, 5, 6}}],
...                                     date(2015, 2, 16): [{'name': 'SuperLotto', 'numbers': {3, 2, 1}},
...                                                         {'name': 'MegaLotto',  'numbers': {6, 5, 4}}]})
>>> lotteries
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')])})

# The checked versions support all operations that the corresponding
# unchecked types do
>>> lottery_0215 = lotteries[date(2015, 2, 15)]
>>> lottery_0215.transform([0, 'name'], 'SuperDuperLotto')
Lotteries([Lottery(numbers=Positives([1, 2, 3]), name='SuperDuperLotto'), Lottery(numbers=Positives([4, 5, 6]), name='MegaLotto')])

# But also makes asserts that types and invariants hold
>>> lottery_0215.transform([0, 'name'], 999)
Traceback (most recent call last):
PTypeError: Invalid type for field Lottery.name, was int

>>> lottery_0215.transform([0, 'numbers'], set())
Traceback (most recent call last):
InvariantException: Field invariant failed

# They can be converted back to python built ins with either thaw()
# or serialize() (which provides possibilities to customize serialization)
>>> thaw(lottery_0215)
[{'numbers': set([1, 2, 3]), 'name': 'SuperLotto'}, {'numbers': set([4, 5, 6]), 'name': 'MegaLotto'}]
>>> lottery_0215.serialize()
[{'numbers': set([1, 2, 3]), 'name': 'SuperLotto'}, {'numbers': set([4, 5, 6]), 'name': 'MegaLotto'}]

Transformations

Transformations are inspired by the cool library instar for Clojure. They let you evolve PMaps and PVectors with arbitrarily deep/complex nesting using simple syntax and flexible matching syntax.

The first argument to transformation is the path that points out the value to transform. The second is the transformation to perform. If the transformation is callable it will be applied to the value(s) matching the path. The path may also contain callables. In that case they are treated as matchers. If the matcher returns True for a specific key it is considered for transformation.

# Basic examples
>>> from pyrsistent import inc, freeze, thaw, rex, ny, discard
>>> v1 = freeze([1, 2, 3, 4, 5])
>>> v1.transform([2], inc)
pvector([1, 2, 4, 4, 5])
>>> v1.transform([lambda ix: 0 < ix < 4], 8)
pvector([1, 8, 8, 8, 5])
>>> v1.transform([lambda ix, v: ix == 0 or v == 5], 0)
pvector([0, 2, 3, 4, 0])

# The (a)ny matcher can be used to match anything
>>> v1.transform([ny], 8)
pvector([8, 8, 8, 8, 8])

# Regular expressions can be used for matching
>>> scores = freeze({'John': 12, 'Joseph': 34, 'Sara': 23})
>>> scores.transform([rex('^Jo')], 0)
pmap({'Joseph': 0, 'Sara': 23, 'John': 0})

# Transformations can be done on arbitrarily deep structures
>>> news_paper = freeze({'articles': [{'author': 'Sara', 'content': 'A short article'},
...                                   {'author': 'Steve', 'content': 'A slightly longer article'}],
...                      'weather': {'temperature': '11C', 'wind': '5m/s'}})
>>> short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:25] + '...' if len(c) > 25 else c)
>>> very_short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:15] + '...' if len(c) > 15 else c)
>>> very_short_news.articles[0].content
'A short article'
>>> very_short_news.articles[1].content
'A slightly long...'

# When nothing has been transformed the original data structure is kept
>>> short_news is news_paper
True
>>> very_short_news is news_paper
False
>>> very_short_news.articles[0] is news_paper.articles[0]
True

# There is a special transformation that can be used to discard elements. Also
# multiple transformations can be applied in one call
>>> thaw(news_paper.transform(['weather'], discard, ['articles', ny, 'content'], discard))
{'articles': [{'author': 'Sara'}, {'author': 'Steve'}]}

Evolvers

PVector, PMap and PSet all have support for a concept dubbed evolvers. An evolver acts like a mutable view of the underlying persistent data structure with "transaction like" semantics. No updates of the original data structure is ever performed, it is still fully immutable.

The evolvers have a very limited API by design to discourage excessive, and inappropriate, usage as that would take us down the mutable road. In principle only basic mutation and element access functions are supported. Check out the documentation of each data structure for specific examples.

Examples of when you may want to use an evolver instead of working directly with the data structure include:

  • Multiple updates are done to the same data structure and the intermediate results are of no interest. In this case using an evolver may be a more efficient and easier to work with.
  • You need to pass a vector into a legacy function or a function that you have no control over which performs in place mutations. In this case pass an evolver instance instead and then create a new pvector from the evolver once the function returns.
>>> from pyrsistent import v

# In place mutation as when working with the built in counterpart
>>> v1 = v(1, 2, 3)
>>> e = v1.evolver()
>>> e[1] = 22
>>> e = e.append(4)
>>> e = e.extend([5, 6])
>>> e[5] += 1
>>> len(e)
6

# The evolver is considered *dirty* when it contains changes compared to the underlying vector
>>> e.is_dirty()
True

# But the underlying pvector still remains untouched
>>> v1
pvector([1, 2, 3])

# Once satisfied with the updates you can produce a new pvector containing the updates.
# The new pvector will share data with the original pvector in the same way that would have
# been done if only using operations on the pvector.
>>> v2 = e.persistent()
>>> v2
pvector([1, 22, 3, 4, 5, 7])

# The evolver is now no longer considered *dirty* as it contains no differences compared to the
# pvector just produced.
>>> e.is_dirty()
False

# You may continue to work with the same evolver without affecting the content of v2
>>> e[0] = 11

# Or create a new evolver from v2. The two evolvers can be updated independently but will both
# share data with v2 where possible.
>>> e2 = v2.evolver()
>>> e2[0] = 1111
>>> e.persistent()
pvector([11, 22, 3, 4, 5, 7])
>>> e2.persistent()
pvector([1111, 22, 3, 4, 5, 7])

freeze and thaw

These functions are great when your cozy immutable world has to interact with the evil mutable world outside.

>>> from pyrsistent import freeze, thaw, v, m
>>> freeze([1, {'a': 3}])
pvector([1, pmap({'a': 3})])
>>> thaw(v(1, m(a=3)))
[1, {'a': 3}]

By default, freeze will also recursively convert values inside PVectors and PMaps. This behaviour can be changed by providing freeze with the flag strict=False.

>>> from pyrsistent import freeze, v, m
>>> freeze(v(1, v(2, [3])))
pvector([1, pvector([2, pvector([3])])])
>>> freeze(v(1, v(2, [3])), strict=False)
pvector([1, pvector([2, [3]])])
>>> freeze(m(a=m(b={'c': 1})))
pmap({'a': pmap({'b': pmap({'c': 1})})})
>>> freeze(m(a=m(b={'c': 1})), strict=False)
pmap({'a': pmap({'b': {'c': 1}})})

In this regard, thaw operates as the inverse of freeze so will thaw values inside native data structures unless passed the strict=False flag.

Compatibility

Pyrsistent is developed and tested on Python 3.6+ and PyPy3.

Performance

Pyrsistent is developed with performance in mind. Still, while some operations are nearly on par with their built in, mutable, counterparts in terms of speed, other operations are slower. In the cases where attempts at optimizations have been done, speed has generally been valued over space.

Pyrsistent comes with two API compatible flavors of PVector (on which PMap and PSet are based), one pure Python implementation and one implemented as a C extension. The latter generally being 2 - 20 times faster than the former. The C extension will be used automatically when possible.

The pure python implementation is fully PyPy compatible. Running it under PyPy speeds operations up considerably if the structures are used heavily (if JITed), for some cases the performance is almost on par with the built in counterparts.

Type hints

PEP 561 style type hints for use with mypy and various editors are available for most types and functions in pyrsistent.

Type classes for annotating your own code with pyrsistent types are also available under pyrsistent.typing.

Installation

pip install pyrsistent

Documentation

Available at http://pyrsistent.readthedocs.org/

Brief presentation available at http://slides.com/tobiasgustafsson/immutability-and-python/

Contributors

Tobias Gustafsson https://github.com/tobgu

Christopher Armstrong https://github.com/radix

Anders Hovmöller https://github.com/boxed

Itamar Turner-Trauring https://github.com/itamarst

Jonathan Lange https://github.com/jml

Richard Futrell https://github.com/Futrell

Jakob Hollenstein https://github.com/jkbjh

David Honour https://github.com/foolswood

David R. MacIver https://github.com/DRMacIver

Marcus Ewert https://github.com/sarum90

Jean-Paul Calderone https://github.com/exarkun

Douglas Treadwell https://github.com/douglas-treadwell

Travis Parker https://github.com/teepark

Julian Berman https://github.com/Julian

Dennis Tomas https://github.com/dtomas

Neil Vyas https://github.com/neilvyas

doozr https://github.com/doozr

Kamil Galuszka https://github.com/galuszkak

Tsuyoshi Hombashi https://github.com/thombashi

nattofriends https://github.com/nattofriends

agberk https://github.com/agberk

Waleed Khan https://github.com/arxanas

Jean-Louis Fuchs https://github.com/ganwell

Carlos Corbacho https://github.com/ccorbacho

Felix Yan https://github.com/felixonmars

benrg https://github.com/benrg

Jere Lahelma https://github.com/je-l

Max Taggart https://github.com/MaxTaggart

Vincent Philippon https://github.com/vphilippon

Semen Zhydenko https://github.com/ss18

Till Varoquaux https://github.com/till-varoquaux

Michal Kowalik https://github.com/michalvi

ossdev07 https://github.com/ossdev07

Kerry Olesen https://github.com/qhesz

johnthagen https://github.com/johnthagen

Bastien Vallet https://github.com/djailla

Ram Rachum https://github.com/cool-RR

Vincent Philippon https://github.com/vphilippon

Andrey Bienkowski https://github.com/hexagonrecursion

Ethan McCue https://github.com/bowbahdoe

Jason R. Coombs https://github.com/jaraco

Nathan https://github.com/ndowens

Geert Barentsen https://github.com/barentsen

phil-arh https://github.com/phil-arh

Contributing

Want to contribute? That's great! If you experience problems please log them on GitHub. If you want to contribute code, please fork the repository and submit a pull request.

Run tests

Tests can be executed using tox.

Install tox: pip install tox

Run test for Python 3.8: tox -e py38

Release

  • pip install -r requirements.txt
  • Update CHANGES.txt
  • Update README.rst with any new contributors and potential info needed.
  • Update _pyrsistent_version.py
  • 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'
  • Push commit and tags: git push && git push --tags
  • Build new release using Github actions

Project status

Pyrsistent can be considered stable and mature (who knows, there may even be a 1.0 some day :-)). The project is maintained, bugs fixed, PRs reviewed and merged and new releases made. I currently do not have time for development of new features or functionality which I don't have use for myself. I'm more than happy to take PRs for new functionality though!

There are a bunch of issues marked with enhancement and help wanted that contain requests for new functionality that would be nice to include. The level of difficulty and extend of the issues varies, please reach out to me if you're interested in working on any of them.

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 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 additional maintainers to the project!

Comments
  • ERROR: Package 'pyrsistent' requires a different Python: 2.7.8 not in '>=3.5'

    ERROR: Package 'pyrsistent' requires a different Python: 2.7.8 not in '>=3.5'

    Hello,

    The latest version of the pyrsistent dropped Python 2 support after one of the contributors added python_requires='>=3.5' in setup.py.

    pip install pyrsistent Collecting pyrsistent Downloading https://files.pythonhosted.org/packages/83/14/6d02fad9caeb3903f06f9442e57789ca2fbb3cf7daf66d4de3aa4dc867dc/pyrsistent-0.17.1.tar.gz (106kB) 100% |████████████████████████████████| 112kB 604kB/s pyrsistent requires Python '>=3.5' but the running Python is 2.7.17

    The fix added to declare Python 2 support drop seems to be ok but I think there is a problem with the sdist. I mean, what version of setuptools did you used to create the sdist? Because supporting python_requires requires setuptools>=24.2.0 and pip>=9.0.0 to benefit from it

    https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires

    opened by Nicusor97 24
  • Upload wheels to PyPI

    Upload wheels to PyPI

    Wheels (.whl) for this package are currently missing from PyPI. Could wheels be uploaded for the current and future releases?

    Read more about the advantages of wheels to understand why generating wheel distributions are important.

    To create a wheel along with source distribution:

    (venv) $ pip install --upgrade pip setuptools wheel
    (venv) $ python setup.py sdist bdist_wheel
    
    # See dist/*.whl
    

    To upload wheels:

    (venv) $ pip install twine
    (venv) $ twine upload dist/*
    
    opened by johnthagen 14
  • Make collection type classes subscriptable for type checking compatibility

    Make collection type classes subscriptable for type checking compatibility

    This adds non-meaningful __getitem__s to the collection types so type hints involving the collection types do not raise an exception when using pytest. (Python itself ignores the type hints.)

    This fixes the following issue when using Pyrsistent with type annotations and pytest: (names changed to protect the innocent)

    tests/test.py:1: in <module>
        from a.b import C
    <frozen importlib._bootstrap>:961: in _find_and_load
        ???
    <frozen importlib._bootstrap>:950: in _find_and_load_unlocked
        ???
    <frozen importlib._bootstrap>:646: in _load_unlocked
        ???
    <frozen importlib._bootstrap>:616: in _load_backward_compatible
        ???
    .tox/py36/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:216: in load_module
        py.builtin.exec_(co, mod.__dict__)
    a/b.py:203: in C
        def stuff(self) -> PVector[str]:
    E   TypeError: 'ABCMeta' object is not subscriptable
    
    opened by nattofriends 14
  • Instances of different PRecord subclasses compare equal to each other

    Instances of different PRecord subclasses compare equal to each other

    >>> from pyrsistent import PRecord, field
    >>> class A(PRecord):
    ...     x = field()
    ... 
    >>> class B(PRecord):
    ...     x = field()
    ... 
    >>> A(x=1) == B(x=1)
    True
    

    This is undesirable because it is idiomatic for different Python classes to represent different types. If objects of different type accidentally share the same field names and values, they should still be considered different.

    By way of analogy,

    >>> (1,) == [1]
    False
    >>> (1,) == {1}
    False
    >>> (1,) == "\x01"
    False
    >>> 
    
    opened by exarkun 12
  • alternative names for single-letter constructors

    alternative names for single-letter constructors

    The single-letter constructors can be pretty annoying

    • they're hard to search for in a module
    • they very easily conflict with local variable names

    It'd be nice if we had alternative, longer names for v, s, m, and b.

    opened by radix 12
  • Sometimes psets don't compare correctly with sets or frozensets

    Sometimes psets don't compare correctly with sets or frozensets

    My branch adding a bunch of pyrsistent 0.9 usage is failing consistently on our build machines - but not on my local desktop. Sample failure:

    twisted.trial.unittest.FailTest: not equal:
    a = [frozenset([<Application(name=u'mysql-clusterhq', image=<object object at 0x7ffacf447ba0>, ports=frozenset([]), volume=None, links=frozenset([]), environment=None, memory_limit=None, cpu_shares=None, restart_policy=<RestartNever()>)>,
                <Application(name=u'site-clusterhq.com', image=<object object at 0x7ffacf447bb0>, ports=frozenset([]), volume=None, links=frozenset([]), environment=None, memory_limit=None, cpu_shares=None, restart_policy=<RestartNever()>)>]),
     u'example.com']
    b = [pset([<Application(name=u'site-clusterhq.com', image=<object object at 0x7ffacf447bb0>, ports=frozenset([]), volume=None, links=frozenset([]), environment=None, memory_limit=None, cpu_shares=None, restart_policy=<RestartNever()>)>, <Application(name=u'mysql-clusterhq', image=<object object at 0x7ffacf447ba0>, ports=frozenset([]), volume=None, links=frozenset([]), environment=None, memory_limit=None, cpu_shares=None, restart_policy=<RestartNever()>)>]),
     u'example.com']
    

    Those two seem like they ought to be equal, and on my computer they are. And yet. (Application is a class using the characteristic library; eventually we'll probably switch everything to pyrsistent).

    Full set of failures: http://build.clusterhq.com/builders/flocker-ubuntu-14.04/builds/1272/steps/trial/logs/problems

    opened by itamarst 12
  • Sometimes a pyrsistent-using program crashes with SIGSEGV

    Sometimes a pyrsistent-using program crashes with SIGSEGV

    We started noticing segfaults after introducing some hypothesis-based testing of some of our pyrsistent-using code. Here's a specific failure we ran into, https://clusterhq.atlassian.net/browse/FLOC-2913

    I haven't constructed a minimal reproducing example yet (as I understand it, hypothesis is generating some random data and initializing a bunch of pyrsistent structures with it and some random data is causing the problem ... but I can't see which random data).

    Maybe some hypothesis-based tests in pyrsistent would be a good way to narrow down the problem.

    opened by exarkun 11
  • Define recursive data types

    Define recursive data types

    This is a bit of a wishlist feature.

    I'd like to be able to define a data structure that looks like this:

    class Leaf(PClass):
      foo = field(type=unicode)
    
    class Tree(PClass):
      children = pvector_field((Tree, Leaf))
    

    However, Python will object and say that Tree is not defined. Is there a way to specify the field's type after the class construction?

    opened by jml 10
  • Add succinct idiom for checked types

    Add succinct idiom for checked types

    I'm writing code that pre-checked types looks like this:

    class MyRec(PRecord):
        paths = field(type=PMap, initial=pmap(), factory=pmap, mandatory=True)
    

    Switching to CheckedPMap, it's now something like this:

    class MyRec(PRecord):
        class _PathMap(CheckedPMap):
            __key_type__ = unicode
            __value_type__ = FilePath
        paths = field(type=_PathMap, initial=_PathMap(), factory=_PathMap, mandatory=True)
    

    This is rather verbose, but likely to be a common use case. I'm thinking that perhaps some utility functions would be helpful. For example:

    class MyRec(PRecord):
        # If mandatory=True then automatically set initial too? Probably.
        paths = pmap_field(unicode, FilePath, mandatory=True)  # optional invariant keyword argument
    

    If I end up writing this for work I'll submit a PR.

    opened by itamarst 10
  • Update freeze to operate on pyrsistent data structures

    Update freeze to operate on pyrsistent data structures

    Fixes issue #81 in theory. In practice we might want to also update thaw:

    # currently
    thaw({'a': m(b=1)})
    # returns {'a': m(b=1))}
    
    # improved thaw
    thaw({'a': m(b=1)})
    # would return {'a': {'b': 1}}
    

    If you think that makes sense I should prob make that change in this PR so we can add all the new functionality at once.

    One other thing - I'm not a fan of the name nonstrict that I chose for the flag to make freeze ignore pmaps and pvectors. If you've got a better suggestion I'm very happy to change it.

    opened by phil-arh 9
  • Allow positional arguments to PClass.__init__

    Allow positional arguments to PClass.__init__

    PClass subclasses must be given keyword arguments for initialization, currently. This is often quite a good thing. However, particularly for a type with only one field, the ability to use a positional argument instead is rather convenient.

    Compare:

    self.assertThat(foo, MappingLikeEquals(expected=bar))`
    

    vs

    self.assertThat(foo, MappingLikeEquals(bar))`
    

    It's hardly an earth shattering difference but I don't find the keyword argument adds anything to the former (and it stands out against the idiom of the API it's being used with, which includes other such types as Equals(bar), Contains(bar), etc).

    enhancement help wanted 
    opened by exarkun 9
  • Feature request: `@dataclass_pyrsistent` for use with dataclasses

    Feature request: `@dataclass_pyrsistent` for use with dataclasses

    Hi, I just started using pyrsistent, but it's working great so far. Thanks for maintaining it.

    Here's some code that I dislike.

    @dataclass_json
    @dataclass(frozen=True)
    class Foo:
        bar: Dict[str, str]
    

    I dislike it because you might think that frozen=True means that you're in immutable-land, but it's actually quite possible to mutate objects of this type:

    x = Foo({"a":"a"})
    x.bar["b"] = "b"
    print(x.to_json()) # {"bar": {"a": "a", "b": "b"}}
    

    You can be disciplined, and have both an immutable dataclass and immutable field types:

    @dataclass_json
    @dataclass(frozen=True)
    class Foo:
        bar: PMap[str, str]
    

    But it's easy to forget/miscommunicate and accidentally add a mutable field. That's not pyrsistent's problem, but maybe pyrsistent is in a good position to provide a solution:

    @dataclass_json
    @dataclass_pyrsistent
    @dataclass(frozen=True)
    class Foo:
        bar: Dict[str, str]
    

    The decorator would translate the mutable container type to PMap (or PVector) and call freeze on Dicts (or Lists) upon initialization. If you provide a type that pyrsistent can't translate (like a non-pyrsistent-dataclass) then it could just fail with a nice message.

    I've included dataclass_json in this request as an example of a library that:

    • does something similar
    • might appear in context with the proposed decorator

    Thanks for considering it!

    opened by MatrixManAtYrService 2
  • Continuous fuzzing by way of OSS-Fuzz

    Continuous fuzzing by way of OSS-Fuzz

    Hi,

    I was wondering if you would like to integrate continuous fuzzing by way of OSS-Fuzz? Fuzzing is a way to automate test-case generation and can be used to find unexpected exceptions in Python. In this PR https://github.com/google/oss-fuzz/pull/8234 I did an initial integration into OSS-Fuzz, and this initial fuzzer should be enough to cover a reasonable amount of the code in pyrsistent. The goal is to make the fuzzer have optimal code coverage and down the extend with e.g. property-based fuzzers.

    If you would like to integrate, the only thing I need is a list of email(s) that will get access to the data produced by OSS-Fuzz, such as bug reports, coverage reports and more stats. Notice the emails affiliated with the project will be public in the OSS-Fuzz repo, as they will be part of a configuration file.

    opened by DavidKorczynski 1
  • Transformation that not strict with missing fields

    Transformation that not strict with missing fields

    I would suggest a version of the transform method, where there is no change if a field is missing.

    My motivation is, that I like the idea of pyrsistent's tranformation, but I use document databases with optional fields and subfields and I would like to keep the original state where a field or parent field is missing. (Also pydantic can use Optional fields.) Coming up with some weird lambda function instead of ny would mess up the code.

    I give you some examples:

    data = v(
            m(timeout="13", description="send email"),
            m(description="start database"),
        )
    
    loose_transform(data, (ny, "timeout"), int)
    # or
    data.transform((ny, "timeout"), int, mode="loose")
    

    would give back

    data = v(
            m(timeout=13, description="send email"),    # The only place that changes is timeout here.
            m(description="start database"),
        )
    

    doesn't do anything with the second element.

    A second example:

    data = m(
        composer=m(birth=m(place="Salzburg", year=1777), name="Felix"),
        lyrics=m(name="John"),
        arranged_by=m(name="Jack", birth=m(year=1977))
    )
    result = loose_transform(data, [ny, "birth", "place"], str.upper)
    

    gives back

    m(
        composer=m(birth=m(place="SALZBURG", year=1777), name="Felix"),   # The only place that changes is place here.
        lyrics=m(name="John"),
        arranged_by=m(name="Jack", birth=m(year=1977))
    )
    

    doesn't create birth/place in lyrics and arranged_by. And I don't need to use a more difficult function instead of str.upper to handle the missing fields.

    I have a loose_transform implementation and unit tests as gist.

    Maybe pyrsistent could use the same transform method with an additional mode keyword argument, that is "strict" by default with the current behavior, and maybe "loose" is the behavior I presented here. (I'm not a native English speaker, so I couldn't find better word than loose.)

    I think that discard could work with the same way, so using discard instead of str.upper in the second example would just remove place="Salzburg" from the composer field.

    opened by horvatha 2
  • Discard transformation adds nodes

    Discard transformation adds nodes

    https://github.com/tobgu/pyrsistent/commit/30f138142923669c9da327abe214986919d52a07 changed the way transforms work so that they add intervening nodes from the matcher which didn't previously exist.

    This addressed the case in https://github.com/tobgu/pyrsistent/issues/154 and made

    >>> freeze({}).transform(['foo','bar'],m())
    

    Have the intuitive result:

    pmap({'foo': pmap({'bar': pmap({})})})
    

    Rather than

    pmap({})
    

    But I think this added an unintuitive side-effect to discard where it will add nodes.

    in versions >=0.15.2:

    >>> freeze({}).transform(['foo','bar'],discard)
    pmap({'foo': pmap({})})
    

    in versions <0.15.2:

    >>> freeze({}).transform(['foo','bar'],discard)
    pmap({})
    

    I don't think discard should ever add anything to a PMap

    This caused serious headache for me as we were using a transformation with discard to remove an empty map with a specific key if it existed, but when we bumped the version of pyrsistent we were using, that transformation started adding new empty maps one level up from the potential location of the empty map.

    For example:

    versions <0.15.2

    >>> freeze({'baz':{'foo':{'bar':1}},'boo':{'far':2}}).transform([ny,'foo','bar'],discard)
    pmap({'boo': pmap({'far': 2}), 'baz': pmap({'foo': pmap({})})}) # this is what I expect
    

    versions >=0.15.2

    >>> freeze({'baz':{'foo':{'bar':1}},'boo':{'far':2}}).transform([ny,'foo','bar'],discard)
    pmap({'baz': pmap({'foo': pmap({})}), 'boo': pmap({'foo': pmap({}), 'far': 2})}) # added new PMap boo.foo
    
    opened by mut3 1
  • Expected type arguments for generic class

    Expected type arguments for generic class "PRecord" (pylance/pyright)

    When trying the example with pylance/pyright in vscode, I get an "PylancereportMissingTypeArgument" type hint error:

    e.g.:

    class ARecord(PRecord):
        x = field()
    

    PRecord is flagged with 'Expected type arguments for generic class "PRecord"'. Is this fixable somehow?

    help wanted 
    opened by khusmann 2
Owner
Tobias Gustafsson
Constantly amazed by how much a computer can achieve when programmed well and how little it achieves when programmed badly...
Tobias Gustafsson
A tool for light-duty persistent memoization of API calls

JSON Memoize What is this? json_memoize is a straightforward tool for light-duty persistent memoization, created with API calls in mind. It stores the

null 1 Dec 11, 2021
A functional standard library for Python.

Toolz A set of utility functions for iterators, functions, and dictionaries. See the PyToolz documentation at https://toolz.readthedocs.io LICENSE New

null 4.1k Jan 4, 2023
a simple functional programming language compiler written in python

Functional Programming Language A compiler for my small functional language. Written in python with SLY lexer/parser generator library. Requirements p

Ashkan Laei 3 Nov 5, 2021
This is a library to do functional programming in Python.

Fpylib This is a library to do functional programming in Python. Index Fpylib Index Features Intelligents Ranges with irange Lazyness to functions Com

Fabián Vega Alcota 4 Jul 17, 2022
Functional collections extension functions for Python

pyfuncol pyfuncol Installation Usage API Documentation Compatibility Contributing License A Python functional collections library. It extends collecti

Andrea Veneziano 32 Nov 16, 2022
Software for visualization of RTStruct structures on CT images

This script is responsible for the operation of the program, it is responsible for both creating the GUI and the process of processing images from dicom files. The program is based on the use of the PyQt5 library, on the basis of which the entire interface containing the appropriate buttons and functions was created.

Adam Piszczek 0 Jun 29, 2022
A fancy and practical functional tools

Funcy A collection of fancy functional tools focused on practicality. Inspired by clojure, underscore and my own abstractions. Keep reading to get an

Alexander Schepanovski 2.9k Dec 29, 2022
Repository, with small useful and functional applications

Repositorio,com pequenos aplicativos uteis e funcionais A ideia e ir deselvolvendo pequenos aplicativos funcionais e adicionar a este repositorio List

GabrielDuke 6 Dec 6, 2021
Functional interface for concurrent futures, including asynchronous I/O.

Futured provides a consistent interface for concurrent functional programming in Python. It wraps any callable to return a concurrent.futures.Future,

A. Coady 11 Nov 27, 2022
A collection of examples of using cocotb for functional verification of VHDL designs with GHDL.

At the moment, this repo is in an early state and serves as a learning tool for me. So it contains a a lot of quirks and code which can be done much better by cocotb-professionals.

T. Meissner 7 Mar 10, 2022
A simple but fully functional calculator that will take multiple operations.

Functional-Calculator A simple but fully functional calculator that will take multiple operations. Usage Run the following command through terminal: p

Uzziel Ariel 1 Dec 22, 2022
A Pythonic Data Catalog powered by Ray that brings exabyte-level scalability and fast, ACID-compliant, change-data-capture to your big data workloads.

DeltaCAT DeltaCAT is a Pythonic Data Catalog powered by Ray. Its data storage model allows you to define and manage fast, scalable, ACID-compliant dat

null 45 Oct 15, 2022
An unofficial python API for trading on the DeGiro platform, with the ability to get real time data and historical data.

DegiroAPI An unofficial API for the trading platform Degiro written in Python with the ability to get real time data and historical data for products.

Jorrick Sleijster 5 Dec 16, 2022
Python for downloading model data (HRRR, RAP, GFS, NBM, etc.) from NOMADS, NOAA's Big Data Program partners (Amazon, Google, Microsoft), and the University of Utah Pando Archive System.

Python for downloading model data (HRRR, RAP, GFS, NBM, etc.) from NOMADS, NOAA's Big Data Program partners (Amazon, Google, Microsoft), and the University of Utah Pando Archive System.

Brian Blaylock 194 Jan 2, 2023
A program made in PYTHON🐍 that automatically performs data insertions into a POSTGRES database 🐘 , using as base a .CSV file 📁 , useful in mass data insertions

A program made in PYTHON?? that automatically performs data insertions into a POSTGRES database ?? , using as base a .CSV file ?? , useful in mass data insertions.

Davi Galdino 1 Oct 17, 2022
Explore-bikeshare-data - GitHub project as part of the Programming for Data Science with Python Nanodegree from Udacity

Date created February 10, 2022 Project Title Explore US Bikeshare Data Descripti

Thárcyla 1 Feb 14, 2022
Viewflow is an Airflow-based framework that allows data scientists to create data models without writing Airflow code.

Viewflow Viewflow is a framework built on the top of Airflow that enables data scientists to create materialized views. It allows data scientists to f

DataCamp 114 Oct 12, 2022
resultados (data) de elecciones 2021 y código para extraer data de la ONPE

elecciones-peru-2021-ONPE Resultados (data) de elecciones 2021 y código para extraer data de la ONPE Data Licencia liberal, pero si vas a usarlo por f

Ragi Yaser Burhum 21 Jun 14, 2021
Yunqi Chen 7 Oct 30, 2022