2015-03-22 18:21:15 +00:00
|
|
|
.. _basic-usage:
|
|
|
|
|
|
|
|
Basic Usage
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Let's return to the example from the :ref:`intro`::
|
|
|
|
|
2015-12-21 03:05:20 +00:00
|
|
|
>>> from bidict import bidict
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> element_by_symbol = bidict(H='hydrogen')
|
|
|
|
|
2015-11-25 22:52:52 +00:00
|
|
|
As we saw, this behaves just like a dict,
|
2015-12-21 03:05:20 +00:00
|
|
|
but maintains a special
|
2018-01-02 21:49:34 +00:00
|
|
|
:attr:`~bidict.BidirectionalMapping.inv` attribute
|
2015-11-25 22:52:52 +00:00
|
|
|
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
|
squashed changes for 0.13.0
- support Python 3.6, refactor CI/test setup, increase test coverage
- refactor BidirectionalMapping, BidictBase, OrderedBidictBase,
FrozenBidictBase, and subclasses
- move frozenorderedbidict into _frozen and looseorderedbidict into _loose
- register bidict as a virtual subclass of MutableMapping rather than
inheriting from it directly. This makes it clearer that it does not use any
of the concrete generic methods that MutableMapping provides.
- improve performance and flexibility of frozenbidict and
frozenorderedbidict hashing
- docs, including new type-hierarchy.png diagram
- rm unused imap, ifilter, izip_longest from compat, add PYPY
- update to latest versions of dependencies
- restore benchmarking on travis
2017-01-09 15:37:31 +00:00
|
|
|
bidict()
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> 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-12-21 03:05:20 +00:00
|
|
|
|
2016-06-28 04:05:22 +00:00
|
|
|
.. include:: values-hashable.rst.inc
|
2015-12-21 03:05:20 +00:00
|
|
|
|
2017-11-20 03:24:08 +00:00
|
|
|
.. include:: values-unique.rst.inc
|
2015-12-21 03:05:20 +00:00
|
|
|
|
2016-07-03 22:12:58 +00:00
|
|
|
.. include:: order-matters.rst.inc
|
|
|
|
|
2016-06-28 04:05:22 +00:00
|
|
|
.. include:: interop.rst.inc
|
2015-12-21 03:05:20 +00:00
|
|
|
|
2015-03-22 18:21:15 +00:00
|
|
|
|
|
|
|
Hopefully bidict feels right at home
|
|
|
|
among the Python built-ins you already know.
|
2016-06-28 04:05:22 +00:00
|
|
|
Proceed to :ref:`other-bidict-types`
|
|
|
|
for documentation on the remaining bidict flavors.
|