dict-counter
This is lightweight Python library for counting occurrences of keys within nested dictionaries. It traverses Mapping types to provide a consolidated count of how many times each key and each branch appears across a dataset. I created this with a single purpose: to evaluate documents represented as Python dicts, to find the number of keys (and nested key paths) across a dataset, and determine when the keys are always present sometimes present.
It may have other uses, but I'm not clever or creative enough to think of any.
Usage
The primary interface is the DictCounter class. It behaves similarly to a standard mapping but includes specialized methods for accumulating counts from nested data.
Initializing the Counter
Create an instance with an optional initial dictionary. You can also specify a nested_count_key which defaults to _self. This key is used at each level of the nested structure to track how many times that specific branch was traversed.
Use update to process a single dictionary or update_many for an iterable of dictionaries. The library uses a stack-based depth-first search to traverse the input, which avoids recursion depth limits and ensures performance even with deeply nested data.
from dict_counter import DictCounter
counter = DictCounter()
d1 = {"foo": {"bar": None}}
counter.update(d1)
d2 = {"foo": {"baz": None}}
counter.update(d2)
d3 = {"foo": {"bar": None, "quux": None}}
counter.update(d3)
Accumulation Example
Updating the counter with multiple documents shows how counts are aggregated:
docs = [
{"foo": {"bar": None}},
{"foo": {"baz": None}},
{"foo": {"bar": None, "quux": None}}
]
counter.update_many(docs)
print(counter)
Output:
{
'foo': {
'_self': 3,
'bar': 2,
'baz': 1,
'quux': 1
}
}
As the counter processes data, it increments values for leaf nodes and updates the _self count for dictionary nodes. If you pass a second dictionary with the same structure, the counts will accumulate accordingly.
Accessing Data
DictCounter implements the mapping interface, allowing you to use standard dictionary methods like items(), keys(), and values(), as well as bracket notation for access.
print(counter["user"]["_self"]) # Returns the traversal count for the 'user' key
print(len(counter)) # Returns the number of top-level keys
Testing
The project uses pytest for verification. If you have the source code locally, you can run the suite through your preferred Python test runner.
pytest
License
This project is licensed under the Apache License, Version 2.0.
Metadata
Release files for dict-counter 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| dict_counter-0.1.2.tar.gz | 23.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dict_counter-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.7 kB
Release files / dict_counter-0.1.2.tar.gz
| Download URL | dict_counter-0.1.2.tar.gz |
|---|---|
| Size | 23.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6dff437d87c6629114bfc7fd95540be81a9f67a606f48094b5a753d40a9af7d
|
|
BLAKE2b-256 checksum How to use checksums |
949d737151c48c7f80c53241f63e76aaf639234548ed3d9a4f9c3cc5aedb1d83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / dict_counter-0.1.2-py3-none-any.whl
| Download URL | dict_counter-0.1.2-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b82c46df451f4e0387517d60f6a1e714b1f5f860c11bc8efb012b401973b9642
|
|
BLAKE2b-256 checksum How to use checksums |
d29ec6f2cf6b56a6b36f9f70f9b1d21fb16341efc9940477f0dfb3729790e72d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for dict-counter 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.