Safely access deeply nested values with clear error messages
Project description
th
Introduction
th
is a Python library that provides a convenient way to access nested attributes and items in complex data structures, with clear and informative error messages when something goes wrong.
When working with deeply nested data structures like JSON responses or complex objects, accessing nested attributes or items can lead to confusing and uninformative error messages when a part of the path doesn't exist or is of the wrong type.
th
simplifies this by allowing you to specify the path using a special path holder object _
, and provides detailed error messages indicating exactly where the problem occurred.
Installation
You can install th
using pip:
pip install th
Usage
Basic Usage
Suppose you have a complex response
object and you want to access a nested value:
username = response.body["users"][0]["name"]
If any part of this path doesn't exist or is None
, you might get a TypeError
or AttributeError
with a less-than-helpful message.
With th
, you can do:
from th import get, _
username = get(response, _.body["users"][0]["name"])
If everything goes well, username
will be set to the desired value. If there's an error, th
will raise an informative exception showing exactly where the problem occurred in the path.
Providing a Default Value
If you want to provide a default value in case the path doesn't exist or is of the wrong type, you can use the default
parameter:
username = get(response, _.body["users"][0]["name"], default="Unknown")
If one of the parts of the path doesn't exist or is of the wrong type, username
will be set to "Unknown".
Verbose Mode
If you need more detailed information about the error, you can enable verbose mode:
username = get(response, _.body["users"][0]["name"], verbose=True)
Suppose response.body["users"]
is None
, you would get an error message like:
th.TypeError: _.body['users'][0]['name']
^^^^^^^^^^^^^^^ inappropriate type (NoneType)
where _ is <class 'Response'>:
Response({'total': 3, 'users': None})
This includes additional debug information in the error message, such as the type and value of the object at the point of failure.
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
File details
Details for the file th-0.4.1.tar.gz
.
File metadata
- Download URL: th-0.4.1.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a0d99523cd98213e01e18395dc08155fcf81cc72e43be7e58b14a57b14ca5a9 |
|
MD5 | ca219182146379b22c892961df8bf734 |
|
BLAKE2b-256 | 105748f5b321ca37e6973d3635fdb602a792d9ede7da2c7d0c46ff6d04e48e0f |
File details
Details for the file th-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: th-0.4.1-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79beca528001f79687d5dafd01caa4186a22fb5cefd78af354b0a48d9b034002 |
|
MD5 | 18be0f578b800be18a1bacd34a87b5da |
|
BLAKE2b-256 | 2f29969d96eeb98abf22915ce59079ac77e0e7d1cdcf5750aa0b46e4b057233f |