Skip to main content

GitHub PyPI Conda (channel only) GitHub Workflow Status

pickydict - the more picky version of Python dictionary

PickyDict objects will behave just like Python dictionaries, with a few notable exceptions:

  1. PickyDict has a force_lower_case attribute.
    If set to True (default) then dictionary keys will all be treated as lower-case.

  2. PickyDict can contain two additional dictionaries named key_replacements and key_regex_replacements with mappings to enforce translating specific key words.

Installation

PickyDict can simply be installed with:

pip install pickydict

It has no further dependencies and should run fine with Python >= 3.6 (probably also lower versions, but that is not tested).

Code examples

from pickydict import PickyDict

# per default force_lower_case is set to True:
my_dict = PickyDict({"A": 1, "B": 2})
print(my_dict)  # => {'a': 1, 'b': 2}

# now also using a replacements dictionary
my_dict = PickyDict({"A": 1, "B": 2},
                    key_replacements={"a": "abc", "b": "bcd", "c": "cde"})
print(my_dict)  # => {'abc': 1, 'bcd': 2}

# When adding a value using an undesired key, the key will automatically be fixed
my_dict["c"] = 100
print(my_dict)  # => {'abc': 1, 'bcd': 2, 'cde': 100}

# Trying to add a value using an undesired key while the proper key already exists,
# will raise an exception.
my_dict["b"] = 5  # => ValueError: Key 'b' will be interpreted as 'bcd'...

It is also possible to add a dictionary with regex expression to replace parts of key strings. This is done using the key_regex_replacements attribute. In the following example the dictionary will replace all spaces in keys with underscores.

Important to note is that regex based replacements will be carried out before the more specific key_replacements. This is to reduce the number of possible variations and make things simpler for the user.

Example:

from pickydict import PickyDict

my_dict = PickyDict({"First Name": "Peter", "Last Name": "Petersson"},
                    key_replacements={"last_name": "surname"},
                    key_regex_replacements={r"\s": "_"})
print(my_dict)  # => {'first_name': 'Peter', 'surname': 'Petersson'}

Whenever the pickyness is updated, no matter if the force_lower_case, key_replacements, or key_regex_replacements, the entire dictionary will be updated accordingly.

Example:

from pickydict import PickyDict

my_dict = PickyDict({"First Name": "Peter", "Last Name": "Petersson"})
print(my_dict)  # => {'first name': 'Peter', 'last name': 'Petersson'}

my_dict.set_pickyness(key_replacements={"last_name": "surname"},
                      key_regex_replacements={r"\s": "_"})
print(my_dict)  # => {'first_name': 'Peter', 'surname': 'Petersson'}

Handling of key duplicates

PickyDict converts key names as described above. This can obviously lead to cases of having key duplicates. This is handled in two different ways. When passing a dictionary to PickyDict in the beginning, only the entries for the desired keys will be kept.

Example:

from pickydict import PickyDict

my_dict = PickyDict({"My Name": "Peter", "name": "Peter Petersson"},
                      key_replacements={"my_name": "name"},
                      key_regex_replacements={r"\s": "_"})
print(my_dict)  # => {"name": "Peter Petersson"}

Later adding values using an improper key, however, will raise an exception when it leads to a duplicate.

Example:

from pickydict import PickyDict

my_dict = PickyDict({"first_name": "Peter Petersson"},
                      key_regex_replacements={r"\s": "_"})
my_dict["First Name"] = Peter P. Petersson  # => ValueError:Key 'First name' will be interpreted as 'first_name'

For the rest it's just a dict

All other operation should work as you are used to from Python dictionaries.

Metadata

Release files for pickydict 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pickydict 0.5.0
File Size Uploaded
pickydict-0.5.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pickydict 0.5.0
File Interpreter ABI Platform
pickydict-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / pickydict-0.5.0.tar.gz

Download URL pickydict-0.5.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fc66944dc5782c1078ffe41cf4dd8fdf6702f9d63c5373748403c12ea2da8cc6
BLAKE2b-256 checksum
How to use checksums
6c20ec9aa324363cc10a9f2cf37bda9a469f1f725dcce4e25b8c37adbdcff303
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / pickydict-0.5.0-py3-none-any.whl

Download URL pickydict-0.5.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6614d2aacbe07d3d4b0b6e7c9e9834eae7b8c0127bad4b1127b6fdcebd5cd65
BLAKE2b-256 checksum
How to use checksums
02ad40ba18d3a88ccce689687a5b6b246f7c651823c48d2e3051d9cd53afafe5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page