This package and data-structure implementation originally comes from the TQEC library: frozendefaultdict.py.
Introduction
This package introduces a new data-structure that is mix of frozendict introduced in Python 3.15 (PEP 814) and collections.defaultdict, with small changes to the API to make it more usable.
Install
You can install frozendefaultdict by using pip
python -m pip install frozendefaultdict
or any other Python package manager able to install from PyPI.
API
The frozendefaultdict class implements the same API as frozendict (see PEP 814) up to a few changes that are noted below.
Notable differences with frozendict
The constructor of frozendefaultdict has a keyword-only argument default_value that does not exist in the frozendict class.
The frozendict constructors using **kwargs (frozendict(**kwargs) and frozendict(collection, **kwargs)) are not supported for frozendefaultdict.
frozendict instances can be compared to dict. frozendefaultdict instances cannot be compared to dict (the comparison will always return False), and also cannot be compared to defaultdict due to the fact that defaultdict has a default_factory where frozendefaultdict has a default_value.
frozendefaultdict has the following additional properties and methods:
has_default_valuethat isTruewhen thefrozendefaultdictinstance has a default value.default_valuethat returns the default value of the instance orNone.map_keysthat returns a newfrozendefaultdictinstance with new keys but the same values.map_valuesthat returns a newfrozendefaultdictinstance with new values (and default value) but the same keys.map_keys_if_presentthat returns a newfrozendefaultdictinstance with new keys and (potentially a subset of) the same values.
Notable differences with defaultdict
The frozendefaultdict class takes a default value as argument instead of a callable that returns the default value. This is mainly to make frozendefaultdict hashable and comparable with other frozendefaultdict instances without relying on function identity (id()) which breaks as soon as lambdas are used.
If callables were used, the class would have no reliable way to hash or compare the provided callable (using the id() of the callable is very brittle, see below, and using the callable code from inspect would also break with a and c in the exemple below).
For example, with Python 3.10, the following code
a = lambda: 4
b = lambda: 4
c = lambda: 2 + 2
print(id(a))
print(id(b))
print(id(c))
outputs 3 different numbers, which makes the id() function a bad implementation for hash().
Examples
from frozendefaultdict import frozendefaultdict
fdd = frozendefaultdict({0: 1, 4: 2}, default_value=98)
print(fdd)
# frozendefaultdict({0: 1, 4: 2}, default_value=98)
fdd2 = fdd | {0: 3}
print(fdd2)
# frozendefaultdict({0: 3, 4: 2}, default_value=98)
fdd3 = frozendefaultdict[int, int]({}, default_value=3)
fdd4 = fdd | fdd3
print(fdd4)
# frozendefaultdict({0: 1, 4: 2}, default_value=3)
print(fdd[0])
# 1
print(fdd[-1])
# 98
print(fdd.has_default_value)
# True
print(fdd.default_value)
# 98
fdd5 = fdd.map_keys(lambda i: i + 5)
print(fdd5)
# frozendefaultdict({5: 1, 9: 2}, default_value=98)
fdd6 = fdd.map_values(lambda i: i - 5)
print(fdd6)
# frozendefaultdict({0: -4, 4: -3}, default_value=93)
fdd7 = fdd.map_keys_if_present({0: 4, 923874: 9023784})
print(fdd7)
# frozendefaultdict({4: 1}, default_value=98)
Usage of LLMs
A LLM has been used at the very beginning of the project to generate some tests. Since then the tests have been extensively modified (some were added, some were removed, some existings tests were modified).
Everything else (including the documentation and the actual package code) has been crafted with love and passion by a human and no LLM has been involved in their development.
Metadata
Release files for frozendefaultdict 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| frozendefaultdict-0.1.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| frozendefaultdict-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.9 kB
Release files / frozendefaultdict-0.1.0.tar.gz
| Download URL | frozendefaultdict-0.1.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
16ea8dfc1e913d285cd9ffb58175e23a0b2cb7d547a1231a5b74a3fe69c5f988
|
|
BLAKE2b-256 checksum How to use checksums |
51c22999ba00fb632b7d6424e0ba4811ca0133a61b4ee1351f638bf2a2ff481a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / frozendefaultdict-0.1.0-py3-none-any.whl
| Download URL | frozendefaultdict-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
737fce0876db450bf6c69ce9606542f8071231b7d3a1085730cd5f26d3a81726
|
|
BLAKE2b-256 checksum How to use checksums |
00080fa6028b2395aa8d19904b0573c7e285f0a86fa2704df2684c6d08f365c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|