Skip to main content

Powerful dict subclass(es) with aliasing & attribute access

Project description

PyPI - Package Version PyPI - Python Version PyPI - License CircleCI Codecov

WHAT

Lexicon is a simple collection of Python dict subclasses providing extra power:

  • AliasDict, a dictionary supporting both simple and complex key aliasing:

    • Alias a single key to another key, so that e.g. mydict['bar'] points to mydict['foo'], for both reads and writes.

    • Alias a single key to a list of other keys, for writing only, e.g. with active_groups = AliasDict({'ops': True, 'biz': True, 'dev': True, 'product': True}) one can make an alias 'tech' mapping to ('ops', 'dev') and then e.g. active_groups['tech'] = False.

    • Aliasing is recursive: an alias pointing to another alias will behave as if it points to the other alias’ target.

  • AttributeDict, supporting attribute read & write access, e.g. mydict = AttributeDict({'foo': 'bar'}) exhibits mydict.foo and mydict.foo = 'new value'.

  • Lexicon, a subclass of both of the above which exhibits both sets of behavior.

HOW

  • pip install lexicon

  • from lexicon import Lexicon (or one of the superclasses)

  • Use as needed.

If you have a clone of the source repository, you can run the tests like so:

  • pip install -r dev-requirements.txt

  • inv test

API

AliasDict

In all examples, 'myalias' is the alias and 'realkey' is the “real”, unaliased key.

  • alias(from_'myalias', to='realkey'): Alias myalias to realkey so d['myalias'] behaves exactly like d['realkey'] for both reads and writes.

    • from_ is the first keyword argument, but typically it can be omitted and still reads fine. See below examples for this usage. See below for details on how an alias affects other dict operations.

  • alias('myalias', to=('realkey', 'otherrealkey')): Alias myalias to both realkey and otherrealkey. As you might expect, this only works well for writes, as there is never any guarantee that all targets of the alias will contain the same value.

  • unalias('myalias'): Removes the myalias alias; any subsequent reads/writes to myalias will behave as normal for a regular dict.

  • 'myalias' in d (aka __contains__): Returns True when given an alias, so if myalias is an alias to some other key, dictionary membership tests will behave as if myalias is set.

  • del d['myalias'] (aka __delitem__): This effectively becomes del d['realkey'] – to remove the alias itself, use unalias().

  • del d['realkey']: Deletes the real key/value pair (i.e. it calls dict.__del__) but doesn’t touch any aliases pointing to realkey.

    • As a result, “dangling” aliases pointing to nonexistent keys will raise KeyError on access, but will continue working if the target key is repopulated later.

Caveats:

  • Because of the single-key/multi-key duality, AliasDict is incapable of honoring non-string-type keys when aliasing (it must test isinstance(key, basestring) to tell strings apart from non-string iterables).

    • AliasDict instances may still use non-string keys, of course – it just can’t use them as alias targets.

AttributeDict

  • d.key = 'value' (aka __setattr__): Maps directly to d['key'] = 'value'.

  • d.key (aka __getattr__): Maps directly to d['key'].

  • del d.key (aka __delattr__): Maps directly to del d['key'].

  • Collisions between “real” or pre-existing attributes, and attributes-as-dict-keys, always results in the real attribute winning. Thus it isn’t possible to use attribute access to access e.g. d['get'].

Lexicon

Lexicon subclasses from AttributeDict first, then AliasDict, with the end result that attribute access will honor aliases. E.g.:

d = Lexicon() d.alias(‘myalias’, to=’realkey’) d.myalias = ‘foo’ print d.realkey # prints ‘foo’

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lexicon-2.0.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

lexicon-2.0.1-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file lexicon-2.0.1.tar.gz.

File metadata

  • Download URL: lexicon-2.0.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.1 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.4

File hashes

Hashes for lexicon-2.0.1.tar.gz
Algorithm Hash digest
SHA256 49a072372e890e88a60f713a297e95788628f2fbae27943b010ae078ed9ab082
MD5 45bfee6c887f0c8b36dfbc0105cee35a
BLAKE2b-256 81309ffe4cd08882379174f7dc2f70a5d67c15760a180b8789db9842cbad4ef7

See more details on using hashes here.

File details

Details for the file lexicon-2.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: lexicon-2.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.1 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.4

File hashes

Hashes for lexicon-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ba9352053174a0aa7641ddc24b1cace8686a6dc4107d55d20c8996f858a30e74
MD5 b69690b12a2ce570e91c2a3b2214ca1b
BLAKE2b-256 ec63f1fea06f13cfac1d64391175128a6798b69009fae2daa71ebd6ae60dd620

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page