OpenQuizz
Une application de gestion des contenus pédagogiques
PMap Class Reference
Inheritance diagram for PMap:
Collaboration diagram for PMap:

Data Structures

class  _Evolver
 

Public Member Functions

def __new__ (cls, size, buckets)
 
def __getitem__ (self, key)
 
def __contains__ (self, key)
 
def __iter__ (self)
 
def __getattr__ (self, key)
 
def iterkeys (self)
 
def itervalues (self)
 
def iteritems (self)
 
def values (self)
 
def keys (self)
 
def items (self)
 
def __len__ (self)
 
def __repr__ (self)
 
def __eq__ (self, other)
 
def __lt__ (self, other)
 
def __str__ (self)
 
def __hash__ (self)
 
def set (self, key, val)
 
def remove (self, key)
 
def discard (self, key)
 
def update (self, *maps)
 
def update_with (self, update_fn, *maps)
 
def __add__ (self, other)
 
def __reduce__ (self)
 
def transform (self, *transformations)
 
def copy (self)
 
def evolver (self)
 

Static Public Attributes

 get
 

Detailed Description

Persistent map/dict. Tries to follow the same naming conventions as the built in dict where feasible.

Do not instantiate directly, instead use the factory functions :py:func:`m` or :py:func:`pmap` to
create an instance.

Was originally written as a very close copy of the Clojure equivalent but was later rewritten to closer
re-assemble the python dict. This means that a sparse vector (a PVector) of buckets is used. The keys are
hashed and the elements inserted at position hash % len(bucket_vector). Whenever the map size exceeds 2/3 of
the containing vectors size the map is reallocated to a vector of double the size. This is done to avoid
excessive hash collisions.

This structure corresponds most closely to the built in dict type and is intended as a replacement. Where the
semantics are the same (more or less) the same function names have been used but for some cases it is not possible,
for example assignments and deletion of values.

PMap implements the Mapping protocol and is Hashable. It also supports dot-notation for
element access.

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

The following are examples of some common operations on persistent maps

>>> m1 = m(a=1, b=3)
>>> m2 = m1.set('c', 3)
>>> m3 = m2.remove('a')
>>> m1
pmap({'b': 3, 'a': 1})
>>> m2
pmap({'c': 3, 'b': 3, 'a': 1})
>>> m3
pmap({'c': 3, 'b': 3})
>>> m3['c']
3
>>> m3.c
3

Member Function Documentation

◆ __add__()

def __add__ (   self,
  other 
)

◆ __contains__()

def __contains__ (   self,
  key 
)

◆ __eq__()

def __eq__ (   self,
  other 
)

◆ __getattr__()

def __getattr__ (   self,
  key 
)

◆ __getitem__()

def __getitem__ (   self,
  key 
)

◆ __hash__()

def __hash__ (   self)

◆ __iter__()

def __iter__ (   self)

◆ __len__()

def __len__ (   self)

◆ __lt__()

def __lt__ (   self,
  other 
)

◆ __new__()

def __new__ (   cls,
  size,
  buckets 
)

Reimplemented in CheckedPMap.

◆ __reduce__()

def __reduce__ (   self)

Reimplemented in CheckedPMap, and PRecord.

◆ __repr__()

def __repr__ (   self)

Reimplemented in CheckedPMap, and PRecord.

◆ __str__()

def __str__ (   self)

◆ copy()

def copy (   self)

◆ discard()

def discard (   self,
  key 
)
Return a new PMap without the element specified by key. Returns reference to itself
if element is not present.

>>> m1 = m(a=1, b=2)
>>> m1.discard('a')
pmap({'b': 2})
>>> m1 is m1.discard('c')
True

◆ evolver()

def evolver (   self)
Create a new evolver for this pmap. For a discussion on evolvers in general see the
documentation for the pvector evolver.

Create the evolver and perform various mutating updates to it:

>>> m1 = m(a=1, b=2)
>>> e = m1.evolver()
>>> e['c'] = 3
>>> len(e)
3
>>> del e['a']

The underlying pmap remains the same:

>>> m1
pmap({'b': 2, 'a': 1})

The changes are kept in the evolver. An updated pmap can be created using the
persistent() function on the evolver.

>>> m2 = e.persistent()
>>> m2
pmap({'c': 3, 'b': 2})

The new pmap will share data with the original pmap in the same way that would have
been done if only using operations on the pmap.

Reimplemented in CheckedPMap, and PRecord.

◆ items()

def items (   self)

◆ iteritems()

def iteritems (   self)

◆ iterkeys()

def iterkeys (   self)

◆ itervalues()

def itervalues (   self)

◆ keys()

def keys (   self)

◆ remove()

def remove (   self,
  key 
)
Return a new PMap without the element specified by key. Raises KeyError if the element
is not present.

>>> m1 = m(a=1, b=2)
>>> m1.remove('a')
pmap({'b': 2})

◆ set()

def set (   self,
  key,
  val 
)
Return a new PMap with key and val inserted.

>>> m1 = m(a=1, b=2)
>>> m2 = m1.set('a', 3)
>>> m3 = m1.set('c' ,4)
>>> m1
pmap({'b': 2, 'a': 1})
>>> m2
pmap({'b': 2, 'a': 3})
>>> m3
pmap({'c': 4, 'b': 2, 'a': 1})

◆ transform()

def transform (   self,
transformations 
)
Transform arbitrarily complex combinations of PVectors and PMaps. A transformation
consists of two parts. One match expression that specifies which elements to transform
and one transformation function that performs the actual transformation.

>>> from pyrsistent import freeze, ny
>>> 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

◆ update()

def update (   self,
maps 
)
Return a new PMap with the items in Mappings inserted. If the same key is present in multiple
maps the rightmost (last) value is inserted.

>>> m1 = m(a=1, b=2)
>>> m1.update(m(a=2, c=3), {'a': 17, 'd': 35})
pmap({'c': 3, 'b': 2, 'a': 17, 'd': 35})

◆ update_with()

def update_with (   self,
  update_fn,
maps 
)
Return a new PMap with the items in Mappings maps inserted. If the same key is present in multiple
maps the values will be merged using merge_fn going from left to right.

>>> from operator import add
>>> m1 = m(a=1, b=2)
>>> m1.update_with(add, m(a=2))
pmap({'b': 2, 'a': 3})

The reverse behaviour of the regular merge. Keep the leftmost element instead of the rightmost.

>>> m1 = m(a=1)
>>> m1.update_with(lambda l, r: l, m(a=2), {'a':3})
pmap({'a': 1})

◆ values()

def values (   self)

Field Documentation

◆ get

get
static

The documentation for this class was generated from the following file: