nocasedict
raw JSON → 2.2.0 verified Mon Apr 27 auth: no python
A case-insensitive ordered dictionary for Python. Version 2.2.0, supports Python >=3.9. Released under Apache 2.0. Maintained with irregular releases.
pip install nocasedict Common errors
error ImportError: No module named 'nocasedict' ↓
cause Library not installed or installed in a different Python environment.
fix
Run: pip install nocasedict
error AttributeError: module 'nocasedict' has no attribute 'NocaseDict' ↓
cause Incorrect import: trying to import from the module but the class is not found due to wrong import statement.
fix
Use: from nocasedict import NocaseDict
error TypeError: 'NocaseDict' object does not support item assignment ↓
cause Accidentally passed a frozen instance or tried to use mappingproxy-like behavior.
fix
Create a new NocaseDict from the frozen one: NocaseDict(frozen_dict)
Warnings
breaking Version 2.0.0 dropped support for Python 2.7 and 3.5. Also changes how keys are stored internally; no more separate lowercase key store. Bulk import may break if using internal attributes. ↓
fix Upgrade to 2.x only if using Python >=3.6. Code relying on __dict__ or internal key storage will break.
deprecated The 'NocaseDict.fromkeys' class method is deprecated since 2.2.0 and will be removed in a future release. ↓
fix Use dict.fromkeys and convert: NocaseDict(dict.fromkeys(keys, value)).
gotcha Equality comparison with regular dict is case-sensitive. NocaseDict('a':1) == {'a':1} is True, but NocaseDict('A':1) == {'a':1} is False. ↓
fix Be aware that case-insensitivity only applies within NocaseDict itself, not when comparing to dict.
gotcha Inheritance from NocaseDict may cause unexpected behavior because it overrides __setitem__ and __getitem__ for case-insensitivity. Custom subclasses must carefully call super(). ↓
fix Ensure you override all relevant methods and call super().__setitem__ with the intended key.
Imports
- NocaseDict wrong
import nocasedict.NocaseDictcorrectfrom nocasedict import NocaseDict
Quickstart
from nocasedict import NocaseDict
d = NocaseDict({'KEY': 'value'})
print(d['key']) # 'value'
print(d['KEY']) # 'value'
d['NewKey'] = 1
print('newkey' in d) # True