Name | th JSON |
Version |
0.4.1
JSON |
| download |
home_page | https://github.com/tsv1/th |
Summary | Safely access deeply nested values with clear error messages |
upload_time | 2024-09-21 14:35:10 |
maintainer | None |
docs_url | None |
author | Nikita Tsvetkov |
requires_python | >=3.8 |
license | Apache-2.0 |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# `th`
[![Codecov](https://img.shields.io/codecov/c/github/tsv1/th/master.svg?style=flat-square)](https://codecov.io/gh/tsv1/th)
[![PyPI](https://img.shields.io/pypi/v/th.svg?style=flat-square)](https://pypi.python.org/pypi/th/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/th?style=flat-square)](https://pypi.python.org/pypi/th/)
[![Python Version](https://img.shields.io/pypi/pyversions/th.svg?style=flat-square)](https://pypi.python.org/pypi/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:
```sh
pip install th
```
## Usage
### Basic Usage
Suppose you have a complex `response` object and you want to access a nested value:
```python
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:
```python
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:
```python
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:
```python
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/tsv1/th",
"name": "th",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Nikita Tsvetkov",
"author_email": "tsv1@fastmail.com",
"download_url": "https://files.pythonhosted.org/packages/10/57/48f5b321ca37e6973d3635fdb602a792d9ede7da2c7d0c46ff6d04e48e0f/th-0.4.1.tar.gz",
"platform": null,
"description": "# `th`\n\n[![Codecov](https://img.shields.io/codecov/c/github/tsv1/th/master.svg?style=flat-square)](https://codecov.io/gh/tsv1/th)\n[![PyPI](https://img.shields.io/pypi/v/th.svg?style=flat-square)](https://pypi.python.org/pypi/th/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/th?style=flat-square)](https://pypi.python.org/pypi/th/)\n[![Python Version](https://img.shields.io/pypi/pyversions/th.svg?style=flat-square)](https://pypi.python.org/pypi/th/)\n\n## Introduction\n\n`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.\n\nWhen 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.\n\n`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.\n\n## Installation\n\nYou can install `th` using pip:\n\n```sh\npip install th\n```\n\n## Usage\n\n### Basic Usage\n\nSuppose you have a complex `response` object and you want to access a nested value:\n\n```python\nusername = response.body[\"users\"][0][\"name\"]\n```\n\nIf any part of this path doesn't exist or is `None`, you might get a `TypeError` or `AttributeError` with a less-than-helpful message.\n\nWith `th`, you can do:\n\n```python\nfrom th import get, _\n\nusername = get(response, _.body[\"users\"][0][\"name\"])\n```\n\nIf 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.\n\n### Providing a Default Value\n\nIf 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:\n\n```python\nusername = get(response, _.body[\"users\"][0][\"name\"], default=\"Unknown\")\n```\n\nIf one of the parts of the path doesn't exist or is of the wrong type, `username` will be set to \"Unknown\".\n\n### Verbose Mode\n\nIf you need more detailed information about the error, you can enable verbose mode:\n\n```python\nusername = get(response, _.body[\"users\"][0][\"name\"], verbose=True)\n```\n\nSuppose `response.body[\"users\"]` is `None`, you would get an error message like:\n\n```\nth.TypeError: _.body['users'][0]['name']\n ^^^^^^^^^^^^^^^ inappropriate type (NoneType)\nwhere _ is <class 'Response'>:\nResponse({'total': 3, 'users': None})\n```\n\nThis includes additional debug information in the error message, such as the type and value of the object at the point of failure.\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Safely access deeply nested values with clear error messages",
"version": "0.4.1",
"project_urls": {
"Homepage": "https://github.com/tsv1/th"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2f29969d96eeb98abf22915ce59079ac77e0e7d1cdcf5750aa0b46e4b057233f",
"md5": "18be0f578b800be18a1bacd34a87b5da",
"sha256": "79beca528001f79687d5dafd01caa4186a22fb5cefd78af354b0a48d9b034002"
},
"downloads": -1,
"filename": "th-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "18be0f578b800be18a1bacd34a87b5da",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 12521,
"upload_time": "2024-09-21T14:35:09",
"upload_time_iso_8601": "2024-09-21T14:35:09.648415Z",
"url": "https://files.pythonhosted.org/packages/2f/29/969d96eeb98abf22915ce59079ac77e0e7d1cdcf5750aa0b46e4b057233f/th-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "105748f5b321ca37e6973d3635fdb602a792d9ede7da2c7d0c46ff6d04e48e0f",
"md5": "ca219182146379b22c892961df8bf734",
"sha256": "5a0d99523cd98213e01e18395dc08155fcf81cc72e43be7e58b14a57b14ca5a9"
},
"downloads": -1,
"filename": "th-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "ca219182146379b22c892961df8bf734",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 13824,
"upload_time": "2024-09-21T14:35:10",
"upload_time_iso_8601": "2024-09-21T14:35:10.928420Z",
"url": "https://files.pythonhosted.org/packages/10/57/48f5b321ca37e6973d3635fdb602a792d9ede7da2c7d0c46ff6d04e48e0f/th-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-21 14:35:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tsv1",
"github_project": "th",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "th"
}