2018-01-02 21:49:34 +00:00
|
|
|
:class:`~bidict.OrderedBidict`
|
|
|
|
------------------------------
|
2015-12-25 19:13:02 +00:00
|
|
|
|
|
|
|
For those times when your one-to-one mapping must also support
|
|
|
|
remembering the order in which items were inserted
|
2016-06-28 04:05:22 +00:00
|
|
|
(à la :class:`collections.OrderedDict`),
|
2017-11-20 03:24:08 +00:00
|
|
|
:class:`bidict.OrderedBidict` has got your back::
|
2015-12-25 19:13:02 +00:00
|
|
|
|
2017-11-16 20:44:51 +00:00
|
|
|
>>> from bidict import OrderedBidict
|
2017-11-20 03:24:08 +00:00
|
|
|
>>> element_by_symbol = OrderedBidict([
|
|
|
|
... ('H', 'hydrogen'), ('He', 'helium'), ('Li', 'lithium')])
|
2018-02-27 13:09:57 +00:00
|
|
|
|
2015-12-25 19:13:02 +00:00
|
|
|
>>> element_by_symbol.inv
|
2017-11-16 20:44:51 +00:00
|
|
|
OrderedBidict([('hydrogen', 'H'), ('helium', 'He'), ('lithium', 'Li')])
|
2018-02-27 13:09:57 +00:00
|
|
|
|
2015-12-25 19:13:02 +00:00
|
|
|
>>> first, second, third = element_by_symbol.values()
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> first, second, third
|
|
|
|
('hydrogen', 'helium', 'lithium')
|
|
|
|
|
|
|
|
>>> # Insert an additional item and verify it now comes last:
|
|
|
|
>>> element_by_symbol['Be'] = 'beryllium'
|
|
|
|
>>> last_item = list(element_by_symbol.items())[-1]
|
|
|
|
>>> last_item
|
|
|
|
('Be', 'beryllium')
|
2015-12-25 19:13:02 +00:00
|
|
|
|
2018-01-02 21:49:34 +00:00
|
|
|
The additional methods of :class:`~collections.OrderedDict` are supported too::
|
2015-12-25 19:13:02 +00:00
|
|
|
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> element_by_symbol.popitem(last=True) # Remove the last inserted item
|
2015-12-25 19:13:02 +00:00
|
|
|
('Be', 'beryllium')
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> element_by_symbol.popitem(last=False) # Remove the first inserted item
|
2015-12-25 19:13:02 +00:00
|
|
|
('H', 'hydrogen')
|
2018-02-27 13:09:57 +00:00
|
|
|
|
|
|
|
>>> # Re-adding hydrogen after it's been removed moves it to the last item:
|
2015-12-25 19:13:02 +00:00
|
|
|
>>> element_by_symbol['H'] = 'hydrogen'
|
2016-06-28 04:05:22 +00:00
|
|
|
>>> element_by_symbol
|
2017-11-16 20:44:51 +00:00
|
|
|
OrderedBidict([('He', 'helium'), ('Li', 'lithium'), ('H', 'hydrogen')])
|
2018-02-26 23:29:56 +00:00
|
|
|
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> # But there's also a `move_to_end` method just for this purpose:
|
|
|
|
>>> element_by_symbol.move_to_end('Li')
|
2016-06-28 04:05:22 +00:00
|
|
|
>>> element_by_symbol
|
2017-11-16 20:44:51 +00:00
|
|
|
OrderedBidict([('He', 'helium'), ('H', 'hydrogen'), ('Li', 'lithium')])
|
2018-02-27 13:09:57 +00:00
|
|
|
|
|
|
|
>>> element_by_symbol.move_to_end('H', last=False) # move to front
|
2016-06-28 04:05:22 +00:00
|
|
|
>>> element_by_symbol
|
2017-11-16 20:44:51 +00:00
|
|
|
OrderedBidict([('H', 'hydrogen'), ('He', 'helium'), ('Li', 'lithium')])
|
2015-12-25 19:13:02 +00:00
|
|
|
|
2018-01-02 21:49:34 +00:00
|
|
|
As with :class:`~collections.OrderedDict`,
|
2018-02-27 13:09:57 +00:00
|
|
|
updating an existing item preserves its position in the order::
|
2015-12-25 19:13:02 +00:00
|
|
|
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> element_by_symbol['He'] = 'updated in place!'
|
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
|
|
|
>>> element_by_symbol
|
2018-02-27 13:09:57 +00:00
|
|
|
OrderedBidict([('H', 'hydrogen'), ('He', 'updated in place!'), ('Li', 'lithium')])
|
2016-06-28 04:05:22 +00:00
|
|
|
|
2016-07-03 21:25:31 +00:00
|
|
|
When setting an item whose key duplicates that of an existing item
|
2018-02-27 13:09:57 +00:00
|
|
|
and whose value duplicates that of a *different* existing item,
|
|
|
|
the existing item whose *value* is duplicated will be dropped,
|
|
|
|
and the existing item whose *key* is duplicated
|
2016-07-03 21:25:31 +00:00
|
|
|
will have its value overwritten in place::
|
|
|
|
|
2017-11-16 20:44:51 +00:00
|
|
|
>>> o = OrderedBidict([(1, 2), (3, 4), (5, 6), (7, 8)])
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> o.forceput(3, 8) # item with duplicated value (7, 8) is dropped...
|
|
|
|
>>> o # and the item with duplicated key (3, 4) is updated in place:
|
2017-11-16 20:44:51 +00:00
|
|
|
OrderedBidict([(1, 2), (3, 8), (5, 6)])
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> # (3, 8) took the place of (3, 4), not (7, 8)
|
|
|
|
|
|
|
|
>>> o = OrderedBidict([(1, 2), (3, 4), (5, 6), (7, 8)]) # as before
|
|
|
|
>>> o.forceput(5, 2) # another example
|
2016-07-03 21:25:31 +00:00
|
|
|
>>> o
|
2017-11-16 20:44:51 +00:00
|
|
|
OrderedBidict([(3, 4), (5, 2), (7, 8)])
|
2018-02-27 13:09:57 +00:00
|
|
|
>>> # (5, 2) took the place of (5, 6), not (1, 2)
|
2016-07-03 21:25:31 +00:00
|
|
|
|
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
|
|
|
|
2018-01-03 00:24:44 +00:00
|
|
|
.. _eq-order-insensitive:
|
|
|
|
|
|
|
|
:meth:`~bidict.FrozenOrderedBidict.__eq__` is order-insensitive
|
|
|
|
###############################################################
|
|
|
|
|
2018-02-27 13:09:57 +00:00
|
|
|
To ensure that equality of bidicts is transitive
|
|
|
|
(enabling conformance to the
|
|
|
|
`Liskov substitution principle <https://en.wikipedia.org/wiki/Liskov_substitution_principle>`_),
|
2017-11-21 03:27:26 +00:00
|
|
|
equality tests between an ordered bidict and other
|
2018-01-02 21:49:34 +00:00
|
|
|
:class:`~collections.abc.Mapping`\s
|
2017-11-21 03:27:26 +00:00
|
|
|
are always order-insensitive::
|
|
|
|
|
|
|
|
>>> from bidict import bidict
|
|
|
|
>>> b = bidict([('one', 1), ('two', 2)])
|
2017-11-16 20:44:51 +00:00
|
|
|
>>> o1 = OrderedBidict([('one', 1), ('two', 2)])
|
|
|
|
>>> o2 = OrderedBidict([('two', 2), ('one', 1)])
|
2017-11-21 03:27:26 +00:00
|
|
|
>>> b == o1
|
|
|
|
True
|
|
|
|
>>> b == o2
|
|
|
|
True
|
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
|
|
|
>>> o1 == o2
|
2017-11-21 03:27:26 +00:00
|
|
|
True
|
|
|
|
|
|
|
|
For order-sensitive equality tests, use
|
2018-01-02 21:49:34 +00:00
|
|
|
:meth:`~bidict.FrozenOrderedBidict.equals_order_sensitive`::
|
2017-11-21 03:27:26 +00:00
|
|
|
|
|
|
|
>>> o1.equals_order_sensitive(o2)
|
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
|
|
|
False
|
2017-11-21 03:27:26 +00:00
|
|
|
>>> from collections import OrderedDict
|
|
|
|
>>> od = OrderedDict(o2)
|
|
|
|
>>> o1.equals_order_sensitive(od)
|
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
|
|
|
False
|
|
|
|
|
2017-11-21 03:27:26 +00:00
|
|
|
Note that this differs from the behavior of
|
2018-02-26 23:29:56 +00:00
|
|
|
:class:`collections.OrderedDict`\'s ``__eq__()``,
|
2018-02-19 02:40:01 +00:00
|
|
|
by recommendation of Raymond Hettinger (the author) himself
|
2018-02-26 23:29:56 +00:00
|
|
|
(who said that making OrderedDict's ``__eq__()``
|
|
|
|
intransitive was a mistake).
|
2016-07-03 21:25:31 +00:00
|
|
|
|
2018-01-02 21:49:34 +00:00
|
|
|
:class:`~bidict.OrderedBidict` also comes in a frozen flavor.
|
|
|
|
See the :class:`~bidict.FrozenOrderedBidict`
|
2017-11-20 03:24:08 +00:00
|
|
|
API documentation for more information.
|