GitXplorerGitXplorer
p

maps

public
13 stars
1 forks
3 issues

Commits

List of commits on branch master.
Verified
bccd827e1a7ed946a28c9098e53d732b4d85fdbd

Merge pull request #44 from pcattori/repr-fixes

ppcattori committed 7 years ago
Unverified
89b1d8c93cd2d34f1a51bf840a359d6e02e3e202

patch version bump for infinite recursion fix when repr subclass of NamedDict

ppcattori committed 7 years ago
Unverified
ad4ed25e9073618337920c14c530df6c1d516d5b

Hardcoded NamedDict in repr to avoid infinite recursion

ppcattori committed 7 years ago
Unverified
7bb841d31833397f3a9c6ef8058d47c9881fb060

test for NamedDict subclass repr infinite recursion

ppcattori committed 7 years ago
Unverified
eb6bb5a89b89b7d126b858d3964be356262f2787

implement missing repr for fixedkeymap

ppcattori committed 7 years ago
Verified
e79d76fb796b5243a80d0537bdb4da41dcb277db

Merge pull request #43 from pcattori/recurse

ppcattori committed 7 years ago

README

The README file for this repository.

PyPI version Build Status Test Code Coverage Compatible Python Versions Documentation Status

maps

Python's missing mappings

Frozen Fixed-key Mutable
bracket access maps.FrozenMap maps.FixedKeyMap dict
dot and bracket access maps.namedfrozen maps.namedfixedkey maps.NamedDict

Install

$ pip install maps

API

Check out the official Maps docs for more!

NamedDict

Just a plain ol' Python dict, but super-charged with access via dot-notation (i.e. __getattr__ and __setattr__).

>>> import maps
>>> d = maps.NamedDict({'a': 1, 'b': 2})
>>> isinstance(d, dict) # drop-in replacement for a `dict`! Can do anything a `dict` can!
True
>>> d.a
1
>>> d.b = 'two'
>>> d
NamedDict({'a': 1, 'b': 'two'})
>>> d.c = 3
>>> d
NamedDict({'a': 1, 'b': 'two', 'c': 3})

namedfrozen

namedfrozen is like namedtuple, but with collections.abc.Mapping under the hood instead of tuple.

In other words, its an immutable mapping with access via bracket-notation (i.e. __getitem__) as well as dot-notation (i.e. __getattr__).

>>> import maps
>>> RGB = maps.namedfrozen('RGB', ['red', 'green', 'blue'])
>>> rgb = RGB(red='rouge', green='forest', blue='azul')
>>> print(rgb)
RGB(red='rouge', green='forest', blue='azul')
>>> rgb['red'] # access via bracket-notation
'rouge'
>>> rgb.green # access via dot-notation
'forest'

namedfixedkey

The namedfixedkey variant is more flexible, allowing edits to existing keys.

>>> import maps
>>> CMYK = maps.namedfixkey('CMYK', ['cyan', 'magenta', 'yellow', 'black'])
>>> cmyk = CMYK(255, 30, 25, 55) # same API as `namedfrozen`, except...
>>> print(cymk)
CMYK(255, 30, 25, 55)
>>> cmyk['magenta'] = 'periwinkle' # overwrite existing items
>>> cmyk.black += 45 # overwrite existing items
>>> print(cmyk)
CMYK(255, 30, 25, 100)