Typed and Tested Case Insensitive Dictionary which was inspired by Raymond Hettinger
Project description
caseless-dictionary
A simple, fast, typed, and tested implementation for a python3.6+ case-insensitive and attribute case-insensitive
dictionaries. This class extends and maintains the original functionality of the builtin dict
while providing extra
features.
Key Features:
- Easy: If you don't care about the case of the key in a dictionary then this implementation is easy to use since it
acts just like a
dict
obj. - Attribute Access:
CaselessAttrDict
allows attribute-style access to dictionary items, providing an alternative, often more readable way to access dictionary items. - Great Developer Experience: Being fully typed makes it great for editor support.
- Fully Tested: Our test suit fully tests the functionality to ensure that
CaselessDict
runs as expected. - There is More!!!:
- ModifiableItemsDict: CaselessDict is built on top of
the
ModifiableItemsDict
, which is a library that enables the user to modify the key or/and value ofdict
type object at runtime.
- ModifiableItemsDict: CaselessDict is built on top of
the
Installation
pip install caseless-dictionary
Caseless Dictionaries
Class Name | Description | Example |
---|---|---|
CaselessDict | A dictionary where keys that are strings are case-folded. | CaselessDict({" HeLLO WoRLD ": 1}) # Output: {'hello world': 1} |
CaseFoldCaselessDict | A dictionary where keys that are strings are case-folded. | CaseFoldCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'hello world': 1} |
LowerCaselessDict | A dictionary where keys that are strings are in lower case. | LowerCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'hello world': 1} |
UpperCaselessDict | A dictionary where keys that are strings are in upper case. | UpperCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'HELLO WORLD': 1} |
TitleCaselessDict | A dictionary where keys that are strings are in title case. | TitleCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'Hello World': 1} |
SnakeCaselessDict | A dictionary where keys that are strings are in snake case. | SnakeCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'hello_world': 1} |
KebabCaselessDict | A dictionary where keys that are strings are in kebab case. | KebabCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'hello-world': 1} |
ConstantCaselessDict | A dictionary where keys that are strings are in constant case. | ConstantCaselessDict({" HeLLO WoRLD ": 1}) # Output: {'HELLO_WORLD': 1} |
Caseless Attribute Dictionaries
Class Name | Description | Example |
---|---|---|
SnakeCaselessAttrDict | A dictionary where keys that are strings are in snake case and can be accessed using attribute notation. | SnakeCaselessAttrDict({" HeLLO WoRLD ": 1}).hello_world # Output: 1 |
ConstantCaselessAttrDict | A dictionary where keys that are strings are in constant case and can be accessed using attribute notation. | ConstantCaselessAttrDict({" HeLLO WoRLD ": 1}).HELLO_WORLD # Output: 1 |
Basic CaselessDict Example
from caseless_dictionary import CaselessDict
# Create a CaselessDict
caseless_dict = CaselessDict({" HeLLO WoRLD ": 1, 2: "two"})
print(caseless_dict) # Output: {'hello world': 1, 2: 'two'}
# Accessing the value using different cases
print(caseless_dict[" hello world "]) # Output: 1
print(caseless_dict[" HELLO WORLD "]) # Output: 1
# Accessing non string value
print(caseless_dict[2]) # Output: two
Caseless Dictionary with Key as Str Only
from caseless_dictionary import CaselessDict
# Create a CaselessDict with key_is_str_only set to True
CaselessDict.key_is_str_only = True
caseless_dict = CaselessDict({" HeLLO WoRLD ": 1})
# Attempt to set a non-string key
try:
caseless_dict[1] = 2
except TypeError:
print("TypeError raised as expected when key_is_str_only is True")
Basic SnakeCaselessAttrDict Example
from caseless_dictionary import SnakeCaselessAttrDict
# Create a SnakeCaselessAttrDict
snake_caseless_attr_dict = SnakeCaselessAttrDict({" HeLLO WoRLD ": 1, 2: "two"})
print(snake_caseless_attr_dict) # Output: {'hello_world': 1, 2: 'two'}
# Accessing the value using attribute notation
print(snake_caseless_attr_dict.hello_world) # Output: 1
print(snake_caseless_attr_dict.HELLO_WORLD) # Output: 1
# Accessing the value using Keys
print(snake_caseless_attr_dict[" hello_world "]) # Output: 1
print(snake_caseless_attr_dict[" HELLO WORLD "]) # Output: 1
# Accessing non string value
print(snake_caseless_attr_dict[2]) # Output: two
SnakeCaselessAttrDict with Key as Str Only
from caseless_dictionary import SnakeCaselessAttrDict
# Create a SnakeCaselessAttrDict with key_is_str_only set to True
SnakeCaselessAttrDict.key_is_str_only = True
snake_caseless_attr_dict = SnakeCaselessAttrDict({" HeLLO WoRLD ": 1})
# Attempt to set a non-string key
try:
snake_caseless_attr_dict[1] = 2
except TypeError:
print("TypeError raised as expected when key_is_str_only is True")
Acknowledgments
During the class '(Advanced) Python For Engineers III' taught by Raymond Hettinger,
Raymond taught us how to inherit from the builtin Python dict
by creating a Case-insensitive Dict.
I thought this was a wonderful learning experience so I decided to create a project that would build upon what he
taught.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file caseless_dictionary-2.0.1.tar.gz
.
File metadata
- Download URL: caseless_dictionary-2.0.1.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34f31ab1651424fe939aa1d9de03870f4e24b89456e7a936705ef17560d945e5 |
|
MD5 | ebfe3f4fc1456edb4d2f4f8823af708c |
|
BLAKE2b-256 | 49215df24c479d875e29a450a4d39c9152d4e4a3d50f96f52a2700f824ffefb1 |
File details
Details for the file caseless_dictionary-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: caseless_dictionary-2.0.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca962b27d48289b5533c31f5bd2f65677a4ffc00ab7934cd2ee969380e38516e |
|
MD5 | b64055689c352c7ad5dfc5854555b602 |
|
BLAKE2b-256 | b25d61721b5011ed53fdb7e86146a01135cdc1a68df67319fcc969c5c157fc41 |