WHAT
Lexicon is a simple Python 2.6+ and 3.3+ compatible collection of 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 tomydict['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.
- Alias a single key to another key, so that e.g.
AttributeDict
, supporting attribute read & write access, e.g.mydict = AttributeDict({'foo': 'bar'})
exhibitsmydict.foo
andmydict.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.
You can install the development
version
via pip install -e git+https://github.com/bitprophet/lexicon#egg=lexicon
.
If you have a clone of the source repository, you can run the tests like so:
pip install -r dev-requirements.txt
spec
API
AliasDict
In all examples, 'myalias'
is the alias and 'realkey'
is the "real",
unaliased key.
alias(from_'myalias', to='realkey')
: Aliasmyalias
torealkey
sod['myalias']
behaves exactly liked['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'))
: Aliasmyalias
to bothrealkey
andotherrealkey
. 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 themyalias
alias; any subsequent reads/writes tomyalias
will behave as normal for a regulardict
.'myalias' in d
(aka__contains__
): Returns True when given an alias, so ifmyalias
is an alias to some other key, dictionary membership tests will behave as ifmyalias
is set.del d['myalias']
(aka__delitem__
): This effectively becomesdel d['realkey']
-- to remove the alias itself, useunalias()
.del d['realkey']
: Deletes the real key/value pair (i.e. it callsdict.__del__
) but doesn't touch any aliases pointing torealkey
.- 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.
- As a result, "dangling" aliases pointing to nonexistent keys will raise
Caveats:
- Because of the single-key/multi-key duality,
AliasDict
is incapable of honoring non-string-type keys when aliasing (it must testisinstance(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 tod['key'] = 'value'
.d.key
(aka__getattr__
): Maps directly tod['key']
.del d.key
(aka__delattr__
): Maps directly todel 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'