Git Product home page Git Product logo

maps's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.