2018-01-02 21:49:34 +00:00
|
|
|
:func:`~bidict.namedbidict`
|
|
|
|
---------------------------
|
2015-03-22 18:21:15 +00:00
|
|
|
|
2017-11-21 04:07:46 +00:00
|
|
|
:func:`bidict.namedbidict`,
|
2015-04-27 22:20:03 +00:00
|
|
|
inspired by :func:`collections.namedtuple`,
|
2015-03-22 18:21:15 +00:00
|
|
|
allows you to easily generate
|
2017-11-21 04:07:46 +00:00
|
|
|
a new bidirectional mapping type
|
2015-03-22 18:21:15 +00:00
|
|
|
with custom attribute-based access to forward and inverse mappings::
|
|
|
|
|
2015-12-21 03:05:20 +00:00
|
|
|
>>> from bidict import namedbidict
|
2017-12-06 19:22:32 +00:00
|
|
|
>>> ElementMap = namedbidict('ElementMap', 'symbol', 'name')
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> noble_gases = ElementMap(He='helium')
|
2017-12-06 19:22:32 +00:00
|
|
|
>>> noble_gases.name_for['He']
|
2015-03-22 18:21:15 +00:00
|
|
|
'helium'
|
|
|
|
>>> noble_gases.symbol_for['helium']
|
|
|
|
'He'
|
2017-12-06 19:22:32 +00:00
|
|
|
>>> noble_gases.name_for['Ne'] = 'neon'
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> del noble_gases.symbol_for['helium']
|
|
|
|
>>> noble_gases
|
|
|
|
ElementMap({'Ne': 'neon'})
|
|
|
|
|
2015-12-25 19:13:02 +00:00
|
|
|
The *base_type* keyword arg,
|
2015-03-22 18:21:15 +00:00
|
|
|
whose default value is :class:`bidict.bidict`,
|
|
|
|
allows overriding the bidict type used as the base class,
|
2017-11-16 20:44:51 +00:00
|
|
|
allowing the creation of e.g. named frozen bidicts::
|
2015-03-22 18:21:15 +00:00
|
|
|
|
2017-11-20 03:24:08 +00:00
|
|
|
>>> from bidict import frozenbidict
|
|
|
|
>>> ElMap = namedbidict('ElMap', 'sym', 'el', base_type=frozenbidict)
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> noble = ElMap(He='helium')
|
2016-06-28 04:05:22 +00:00
|
|
|
>>> hash(noble) is not 'an error'
|
2015-03-22 18:21:15 +00:00
|
|
|
True
|
|
|
|
>>> noble['C'] = 'carbon' # mutation fails
|
|
|
|
Traceback (most recent call last):
|
2015-12-21 03:05:20 +00:00
|
|
|
...
|
|
|
|
TypeError...
|