Case insensitive derivable dictionary
You can install the newest version of pydicti from PyPI:
pip install pydicti
Alternatively, you can just take the file pydicti.py and redistribute it with your application.
Objects of type dicti are dictionaries that feature case insensitive item access:
>>> d = dicti(Hello='foo', world='bar') >>> d['heLLO'] 'foo' >>> 'WOrld' in d True
Internally however, the keys retain their original case:
>>> sorted(d.keys()) ['Hello', 'world']
>>> odicti(zip('abc', range(3))) Dicti(OrderedDict([('a', 0), ('b', 1), ('c', 2)]))
With build_dicti you can create custom case insensitive dictionaries. This function is what is used to create the pydicti.dicti and pydicti.odicti types. Note that calling build_dicti several times with the same argument will result in identical types:
>>> build_dicti(dict) is dicti True >>> build_dicti(OrderedDict) is odicti True
build_dicti uses subclassing to inherit the semantics of the given base dictionary type:
>>> issubclass(odicti, OrderedDict) True
The function Dicti is convenient for creating case insensitive copies of dictionary instances:
>>> o = OrderedDict(zip('abcdefg', range(7))) >>> oi = Dicti(o) >>> type(oi) is odicti True
The subclassing approach allows to plug your dictionary instance into places where typechecking with isinstance is used, like in the json module:
>>> import json >>> d == json.loads(json.dumps(d), object_hook=dicti) True
Above python26 you can use json.loads(s, object_pairs_hook=odicti) to deserialize ordered dictionaries.
The equality comparison tries preserves the semantics of the base type as well as reflexitivity. This has impact on the transitivity of the comparison operator:
>>> i = dicti(oi) >>> roi = odicti(reversed(list(oi.items()))) >>> roi == i and i == oi True >>> oi != roi and roi != oi # NOT transitive! True >>> oi == i and i == oi # reflexive True
The coercion rules in python allow this to work pretty well when performing comparisons between types that are subclasses of each other. Be careful otherwise, however.
Copyright © 2013 Thomas Gläßle <firstname.lastname@example.org>
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.
This program is free software. It comes without any warranty, to the extent permitted by applicable law.