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'})
|
|
|
|
|
|
2018-02-27 13:09:57 +00:00
|
|
|
|
Using the *base_type* keyword arg –
|
|
|
|
|
whose default value is :class:`bidict.bidict` –
|
|
|
|
|
you can override the bidict type used as the base class,
|
|
|
|
|
allowing the creation of e.g. a named frozenbidict type::
|
2015-03-22 18:21:15 +00:00
|
|
|
|
|
2017-11-20 03:24:08 +00:00
|
|
|
|
>>> from bidict import frozenbidict
|
2018-02-27 13:09:57 +00:00
|
|
|
|
>>> ElMap = namedbidict('ElMap', 'symbol', 'name', base_type=frozenbidict)
|
2015-03-22 18:21:15 +00:00
|
|
|
|
>>> noble = ElMap(He='helium')
|
2018-02-27 13:09:57 +00:00
|
|
|
|
>>> noble.symbol_for['helium']
|
|
|
|
|
'He'
|
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):
|
2018-02-27 13:09:57 +00:00
|
|
|
|
...
|
|
|
|
|
TypeError: ...
|