Skip to main content

th

Codecov PyPI PyPI - Downloads Python Version

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.

Release files for th 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for th 0.4.1
File Size Uploaded
th-0.4.1.tar.gz 13.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for th 0.4.1
File Interpreter ABI Platform
th-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.3 kB

Release files / th-0.4.1.tar.gz

Download URL th-0.4.1.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5a0d99523cd98213e01e18395dc08155fcf81cc72e43be7e58b14a57b14ca5a9
BLAKE2b-256 checksum
How to use checksums
105748f5b321ca37e6973d3635fdb602a792d9ede7da2c7d0c46ff6d04e48e0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / th-0.4.1-py3-none-any.whl

Download URL th-0.4.1-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
79beca528001f79687d5dafd01caa4186a22fb5cefd78af354b0a48d9b034002
BLAKE2b-256 checksum
How to use checksums
2f29969d96eeb98abf22915ce59079ac77e0e7d1cdcf5750aa0b46e4b057233f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

1 release file

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