A lightweight library for counting keys in dictionaries.
Project description
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.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dict_counter-0.1.2.tar.gz.
File metadata
- Download URL: dict_counter-0.1.2.tar.gz
- Upload date:
- Size: 23.3 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6dff437d87c6629114bfc7fd95540be81a9f67a606f48094b5a753d40a9af7d
|
|
| MD5 |
86212d8fecea62b48e6ed1b185b32033
|
|
| BLAKE2b-256 |
949d737151c48c7f80c53241f63e76aaf639234548ed3d9a4f9c3cc5aedb1d83
|
File details
Details for the file dict_counter-0.1.2-py3-none-any.whl.
File metadata
- Download URL: dict_counter-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b82c46df451f4e0387517d60f6a1e714b1f5f860c11bc8efb012b401973b9642
|
|
| MD5 |
5c287965b46fb53a83274fbd5ebca4dc
|
|
| BLAKE2b-256 |
d29ec6f2cf6b56a6b36f9f70f9b1d21fb16341efc9940477f0dfb3729790e72d
|