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.
You can install th
using pip:
pip install th
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.
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".
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.