Skip to main content

dict-tape

A collection of Python utilities for manipulating dictionaries

ko-fi

Installation

pip install dict-tape

Usage

from dict_tape import chain_get

Features

Traversal

chain_get

This is intended to help solve problems like this with deeply nested dictionaries:

some_dict.get('a', {})[0].get('b', {}).get('e', 'default')

The problem is that while .get() can help with chains of dictionaries, if you have lists intermixed in there, you can't sanely default things and have to just hope the list has items (since list indexing will throw an exception if the list is empty).

But with chain_get() you can accomplish this in a way that will sanely default:

from dict_tape import chain_get

some_dict = {'a': [{'b': {'e': 'value'}}]}
# Prints 'value'
print(chain_get(some_dict, 'a', 0, 'b', 'e', default='default'))

some_dict = {'a': []}
# Prints 'default'
print(chain_get(some_dict, 'a', 0, 'b', 'e', default='default'))

chain_get() will also do some sanity checking on your types and arguments:

  • Checks to make sure that every new level is a dict, list, or str
  • If the current data being indexed is a list or str, it will check that the arg provided is an int
  • If the current data being indexed is a dict, it will check that the arg provided is an int or str
  • If the resulting data it finds with traversal doesn't match the type provided for default, it will throw an error so you know you got something you apparently didn't expect

Options

  • default: The default value to return if the chain fails at any point.
  • check_arg_types: You can set this to False to disable checking whether arg types are valid for data. If this is disabled, when it would normally throw an error for this problem it will instead return the default value.
  • check_data_types: You can set this to False to disable checking whether the data types are valid. If this is disabled, when it would normally throw an error for this problem it will instead return the default value.

Metadata

Release files for dict-tape 1.3.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 dict-tape 1.3.0
File Size Uploaded Provenance Actions
dict_tape-1.3.0.tar.gz 17.7 kB Without provenance Details for dict_tape-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dict-tape 1.3.0
File Interpreter ABI Platform Actions
dict_tape-1.3.0-py3-none-any.whl Python 3 none any Details for dict_tape-1.3.0-py3-none-any.whl

Total release size: 33.5 kB

Release files / dict_tape-1.3.0.tar.gz

Download URL dict_tape-1.3.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3562e55576d80bde655549ac91afd06239a4a8a2bae68f78f8740eba6488edea
BLAKE2b-256 checksum
How to use checksums
d0e05be0fc7f37c53e2e3738b360ca609de09ddbc81c6bb826ae9615380f5ee3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Release files / dict_tape-1.3.0-py3-none-any.whl

Download URL dict_tape-1.3.0-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0167d52a7bf239d85273cd5ab046a526883b30fd1d55fed8189486ab8e474db
BLAKE2b-256 checksum
How to use checksums
d7d1d451c54bf49f8240cef01255769742966825edc41ed680af3fe1eee38070
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.0

Security for dict-tape 1.3.0

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.

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