Package: odict
odictDictionary in which theinsertionorder of items is preserved (using an
internal double linked list). In this implementation replacing an existing
item keeps it at its original position.Internal representation: values of the dict.[pred_key,val,succ_key]The sequence of elements uses as a double linked list. Thelinksare dict
keys.self.lhandself.ltare the keys of first and last element
inserted in the odict.MotivationWhen this package was created,collections.OrderedDictnot existed yet.Another problem is thatdictcannot always be inherited from in conjunction
with other base classes. This may result in instance layout conflicts or other
errors. Soodictis written in a way that let you alter the dictionary
base implementation.UsageImport and create ordered dictionary.fromodictimportodictod=odict()Custom odictIt is possible to use_odictbase class to implement an ordered dict not
inheriting fromdicttype. This is useful i.e. when persisting to ZODB.Inheriting fromdictandPersistentat the same time fails. Also,
using a regularlistfor the internal double linked list representation
causes problems, so we define a custom class for it as well.frompersistent.dictimportPersistentDictfrompersistent.listimportPersistentListclasspodict(_odict,PersistentDict):def_dict_impl(self):returnPersistentDictdef_list_factory(self):returnPersistentListPython < 3.7In Python < 3.7 casting to dict will fail. The reason for this can be foundhere. The__init__function of
dict checks whether arg is subclass ofdict, and ignores overwritten__getitem__& co if so. This was fixed and later reverted due to
behavioural problems with pickle:>>>dict(odict([(1,1)])){1: [nil, 1, nil]}Use one of the following ways for type conversion.>>>dict(odict([(1,1)]).items()){1: 1}>>>odict([(1,1)]).as_dict(){1: 1}MiscIn a C reimplementation of this data structure, things could be simplified
(and speed up) a lot if given a value you can at the same time find its key.
With that, you can use normal C pointers.Python VersionsPython 2.7, 3.7+Probably works with other/older versionsContributorsbearophile (Original Author)Robert Niederreiter (Author)Georg BernhardFlorian FriesdorfJens Kleinunder thePython Software Foundation License.Changes1.9.0 (2022-05-15)Addmovebefore,moveafter,movefirstandmovelastfunctions
toodict.
[rnix]Usefirst_keyandlast_keyininsertfirstandinsertlastinstead of reading all keys.
[rnix]1.8.1 (2022-03-17)Do not run test suites for Python 2.6 and Python < 3.7 any more. No changes
to the implementation have been made.
[rnix]Package housekeeping.
[rnix]1.8.0Addnext_keyandprev_keyfunctions.
[rnix]Addfirst_keyandlast_keyproperties.
[rnix]Add_list_factoryon_odictbase class. Can be used to alter
the list implementation used for internal value triples.
[rnix]1.7.0Addswap,insertbefore,insertafter,insertfirstandinsertlastfunctions toodict.
[rnix]1.6.2Use class name instead of ‘odict()’ in__repr__.
[rnix]1.6.1pypi masness.
[rnix]1.6.0Compatible with Python 3 and pypy.
[rnix]1.5.2Fix permission problem in 1.5.1 release, some files were only rw by user.
[jensens, 2016-11-25]1.5.1Implement__copy__and__deepcopy__in order to work with Python 2.7.
[rnix, 2012-10-15]Usetry/exceptinstead ofinin__contains__.
[rnix, 2012-10-15]1.5.0Implementalter_key.
[rnix, 2012-05-18]1.4.4Remove unused error variable.
[rnix, 2011-11-28]Add note on why to check with==and!=against_nil.
[rnix, 2011-11-28]1.4.3Get rid of annoying warning about “global” usage inbench.py.
[jensens, 2011-09-20]1.4.2Morecopytesting.
[rnix, 2010-12-18]Addhas_keyto odict.
[rnix, 2010-12-18]1.4.1Fix release, README.rst was missing, added MANIFEST.in file to include it.
[jensens, 2010-11-29]1.4.0Full test coverage.
[chaoflow, rnix, 2010-08-17]Code cleanup and optimizing.
[chaoflow, rnix, 2010-08-17]1.3.2AccessdictAPI providing class via function_dict_impl()and
provide odict logic as abstract base class_odict.
[rnix, 2010-07-08]1.3.1Add test for bool evaluation.
[rnix, 2010-04-21]1.3.0Fix access toodict.ltandodict.lhproperties. Now it’s possible
to overwrite__setattr__and__getattr__onodictsubclass
without hassle.
[rnix, 2010-04-06]Addsortfunction to odict.
[rnix, 2010-03-03]1.2.6Makeodictserialize and deserialize properly.
[gogo, 2010-01-12]1.2.5Addas_dictfunction. Supports type conto ordinarydict.
[rnix, 2009-12-19]Add benchmark script.
[rnix, 2009-12-19]1.2.4Do not check forkey in selfon__delitem__,KeyErroris raised
properly anyway. Huge Speedup!
[rnix, jensens, 2009-12-18]1.2.3Move tests to seperate file and make egg testable withpython setup.py test.
[rnix, 2009-12-07]improveltandlhproperties to makeodictwork withcopy.deepcopy.
[rnix, 2009-12-07]1.2.2Use try/except instead of__iter__in__setitem__to determine if
value was already set.
[rnix, 2009-07-17]1.2.1Add missing__len__and__contains__functions.
[rnix, 2009-03-17]1.2.0Eggified
[rnix, 2009-03-17]< 1.2http://code.activestate.com/recipes/498195/[bearophile, 2006-10-12]LicensePYTHON SOFTWARE FOUNDATION LICENSE VERSION 21. This LICENSE AGREEMENT is between the Python Software Foundation
(“PSF”), and the Individual or Organization (“Licensee”) accessing and
otherwise using this software (“Python”) in source or binary form and
its associated documentation.2. Subject to the terms and conditions of this License Agreement, PSF
hereby grants Licensee a nonexclusive, royalty-free, world-wide
license to reproduce, analyze, test, perform and/or display publicly,
prepare derivative works, distribute, and otherwise use Python
alone or in any derivative version, provided, however, that PSF’s
License Agreement and PSF’s notice of copyright, i.e., “Copyright (c)
2001, 2002, 2003, 2004, 2005, 2006 Python Software Foundation; All Rights
Reserved” are retained in Python alone or in any derivative version
prepared by Licensee.3. In the event Licensee prepares a derivative work that is based on
or incorporates Python or any part thereof, and wants to make
the derivative work available to others as provided herein, then
Licensee hereby agrees to include in any such work a brief summary of
the changes made to Python.4. PSF is making Python available to Licensee on an “AS IS”
basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT
INFRINGE ANY THIRD PARTY RIGHTS.5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON,
OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.6. This License Agreement will automatically terminate upon a material
breach of its terms and conditions.7. Nothing in this License Agreement shall be deemed to create any
relationship of agency, partnership, or joint venture between PSF and
Licensee. This License Agreement does not grant permission to use PSF
trademarks or trade name in a trademark sense to endorse or promote
products or services of Licensee, or any third party.8. By copying, installing or otherwise using Python, Licensee
agrees to be bound by the terms and conditions of this License
Agreement.