2015-03-22 18:21:15 +00:00
|
|
|
.. _basic-usage:
|
|
|
|
|
|
|
|
Basic Usage
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Let's return to the example from the :ref:`intro`::
|
|
|
|
|
|
|
|
>>> element_by_symbol = bidict(H='hydrogen')
|
|
|
|
|
2015-11-25 22:52:52 +00:00
|
|
|
As we saw, this behaves just like a dict,
|
|
|
|
but maintains a special ``.inv`` property
|
|
|
|
giving access to inverse mappings::
|
2015-03-22 18:21:15 +00:00
|
|
|
|
2015-11-25 22:52:52 +00:00
|
|
|
>>> element_by_symbol.inv['helium'] = 'He'
|
|
|
|
>>> del element_by_symbol.inv['hydrogen']
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> element_by_symbol
|
|
|
|
bidict({'He': 'helium'})
|
|
|
|
|
|
|
|
The rest of the
|
|
|
|
:class:`collections.abc.MutableMapping` ABC
|
|
|
|
is also supported::
|
|
|
|
|
|
|
|
>>> 'C' in element_by_symbol
|
|
|
|
False
|
|
|
|
>>> element_by_symbol.get('C', 'carbon')
|
|
|
|
'carbon'
|
|
|
|
>>> element_by_symbol.pop('He')
|
|
|
|
'helium'
|
|
|
|
>>> element_by_symbol
|
|
|
|
bidict({})
|
|
|
|
>>> element_by_symbol.update(Hg='mercury')
|
|
|
|
>>> element_by_symbol
|
|
|
|
bidict({'Hg': 'mercury'})
|
2015-11-25 22:52:52 +00:00
|
|
|
>>> 'mercury' in element_by_symbol.inv
|
2015-03-22 18:21:15 +00:00
|
|
|
True
|
2015-11-25 22:52:52 +00:00
|
|
|
>>> element_by_symbol.inv.pop('mercury')
|
2015-03-22 18:21:15 +00:00
|
|
|
'Hg'
|
|
|
|
|
|
|
|
Because inverse mappings are maintained alongside forward mappings,
|
|
|
|
referencing a bidict's inverse
|
|
|
|
is always a constant-time operation.
|
2015-04-27 22:20:03 +00:00
|
|
|
No need to go through the entire collection.
|
2015-03-22 18:21:15 +00:00
|
|
|
|
|
|
|
One last thing, bidicts interoperate well with other types of maps.
|
|
|
|
For example, they support (efficient) polymorphic equality testing::
|
|
|
|
|
|
|
|
>>> bidict(a=1) == dict(a=1)
|
|
|
|
True
|
|
|
|
|
|
|
|
And converting back and forth works as expected::
|
|
|
|
|
|
|
|
>>> dict(bidict(a=1))
|
|
|
|
{'a': 1}
|
|
|
|
>>> bidict(dict(a=1))
|
|
|
|
bidict({'a': 1})
|
|
|
|
|
|
|
|
Straightforward so far?
|
|
|
|
Hopefully bidict feels right at home
|
|
|
|
among the Python built-ins you already know.
|
|
|
|
|
|
|
|
But read on to make sure you cover some important :ref:`caveats`.
|