Wrap dictionaries of data for quick traversal and useful messages when data doesn't match expectations.
Project description
DataWrap
Wrap dictionaries of data for quick traversal and provide informative error messages when data doesn't match expectations.
Working with large JSON/YAML files often means drilling through deeply nested structures. Navigating those structures in Python either leads to additional code to check the shapes and attributes of the data:
with file.open('rt') as fh:
data = yaml.safe_load(fh)
if "sources" in data and isinstance(data["sources"], list):
for source in data["sources"]:
if "tables" in source and isinstance(source["tables"], list) and source["tables"]:
for table in source["tables"]:
print(f"{source['schema']}.{table['name']}")
KeyError: 'name'
Or leads to a best-effort Duck typing that generates non-descriptive KeyError and IndexError exceptions that leave the developer digging through the source data.
with file.open('rt') as fh:
data = yaml.safe_load(fh)
for source in data["sources"]:
for table in source["tables"]:
print(f"{source['schema']}.{table['name']}")
TypeError: 'NoneType' object is not iterable
The DictWrapper class provides a simple wrapper around nested data structures which track path traversal to generate targeted exceptions.
with file.open('rt') as fh:
sources = DictWrapper(yaml.safe_load(fh), filename=str(file))
for source in sources['sources']:
for table in source['tables']:
print(f"{source['schema']}.{table['name']}")
KeyError: '[schema.yml] sources[0].tables[2] is missing key .name'
Usage
Install using pip
pip install datawrap
Wrap any nested dict/list structure with DictWrapper to get clear, contextual error messages when keys or indices are missing.
from dict_wrapper import DictWrapper
import json
# Load JSON or YAML
with open("schema.yml", "rt") as fh:
data = json.load(fh)
# Wrap the root object
data = DictWrapper(data, filename="schema.yml")
# Traverse as normal dicts/lists
for system in data["sources"]:
for table in system["tables"]:
print(table["name"])
Key Features
- Drop-in replacement for normal dict/list access.
- Tracks path context across nested dicts/lists.
- Raises KeyError/IndexError with full path info.
- Use .get(key, default) for safe access without raising.
- Iteration yields wrapped elements, so the path context is preserved inside loops.
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 datawrap-1.0.0.tar.gz.
File metadata
- Download URL: datawrap-1.0.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f22af8b4c9548f819815bfc6d3c6cb3a86e040a9c420c3cabe13931defda853
|
|
| MD5 |
526c086c0977c6d5fe0f3d82bc9208d7
|
|
| BLAKE2b-256 |
c7ba99d66aba7d7ff5b24757b35f3eeb2d5f58d8a1cd18b58d2ef8baa852abf9
|
File details
Details for the file datawrap-1.0.0-py3-none-any.whl.
File metadata
- Download URL: datawrap-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de1d3d45e2a5dde87c12e3a0e5dd5ddbc4b0f1d110fb02c01917f2424178a20a
|
|
| MD5 |
9d16c79cfb449652442143232febe5e4
|
|
| BLAKE2b-256 |
b6db62d8cd1b60728ef54cda580c67cc931c9017c781334bea0c11211b6cd1fd
|