Skip to content

pcattori/maps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)