pynsted


Namepynsted JSON
Version 1.0.8 PyPI version JSON
download
home_pagehttps://github.com/pragmatic-fi/pynsted
SummaryAdd a method to dictionaries to get nested values
upload_time2024-07-05 13:44:22
maintainerNone
docs_urlNone
authorThe Pragmatic Solutions
requires_python<3.13,>=3.11
licenseNone
keywords dictionary nested
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pynsted

This is a Python library which in a hackish way adds to Python dictionaries
a method for getting nested values. It can be installed with
`pip install pynsted`.

## Usage

Start by importing the module: `import pynsted`.

Now access nested values in dictionaries by specifing path to them and using
`getn` method:

```python
>>> import pynsted
>>> a = {1: {2: {"foo": "bar"}}}
>>> a.getn([1, 2, "foo"])
'bar'
>>>
```

If at some moment there is no value, corresponding to the next key in path,
`None` is returned:

```python
>>> print(a.getn([1, 3, "foo"]))
None
>>>
```

It is possible to specify the default value, which will be returned instead of
`None` if there is no value at the given path:

```python
>>> a.getn([1, 3, "foo"], "new default")
'new default'
>>>
```

## Linters

There are two problems with this module and linters:

- unused import
- nonexisting `.getn()` method for dictionaries

### Unused import

If the import is really unused (see below why it may not be), do the following:

- for `pylint` put `# pylint: disable=unused-import` in the line right before
  the import and `# pylint: disable=unused-import` in the line right after, or
  `pylint: disable=unused-import` as an end comment into the import line
- for `ruff` put `noqa: F401` as an end comment into the import line
- for `pyright`: put `# pyright: reportUnusedImport=false` in the line right
  before the import line and `# pyright: reportUnusedImport=false` in the line
  right after, or `pyright: ignore` as an end comment into the import line

Notice that in the case of multiple linters combination of end of line comments
in the import line will not work, and you will need to do something like

```python
# pylint: disable=unused-import
# pyright: reportUnusedImport=false
import pynsted  # noqa: F401
# pyright: reportUnusedImport=true
# pylint: enable=unused-import
```

### Nonexisting method

#### Quick and dirty pylint solution

Add inte yoir pylint configuration file something like
`extension-pkg-whitelist=pynsted`. Notice that method is insecure and allows
the authors of `pynsted` to do whatever they want with your computer, and maybe
also kill your cat.

### Using protocol

The benefit of this approach that it will pacify not only `pylint` but
`pyright`, `ruff`, and `mypy`, and will make import used. The idea is that
`pytested` module defines `SupportsGetn` protocol, which claims the presence of
`getn` method. You have to assert that your dictionary is BOTH `dict` and
`SupportsGetn`:

```python
assert isinstance(input_dict, dict)
assert isinstance(input_dict, pynsted.SupportsGetn)
```

## Development

The project uses `poetry` tool (highly recommended!), you should know your way
around. The supplied `Makefile` defines the following targets:

- `test` -- runs `pytest` and demands 100% test coverage
- `qa` -- runs `test` target and then runs a bunch of linters
- `format` -- formats the code with `black` and `isort`

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/pragmatic-fi/pynsted",
    "name": "pynsted",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.13,>=3.11",
    "maintainer_email": null,
    "keywords": "dictionary, nested",
    "author": "The Pragmatic Solutions",
    "author_email": "contact@thepragmatic.fi",
    "download_url": "https://files.pythonhosted.org/packages/4c/1c/88fee7c5ea0cb32c0adddd81fef62e802f3985609836eb83fde7985d65c1/pynsted-1.0.8.tar.gz",
    "platform": null,
    "description": "# pynsted\n\nThis is a Python library which in a hackish way adds to Python dictionaries\na method for getting nested values. It can be installed with\n`pip install pynsted`.\n\n## Usage\n\nStart by importing the module: `import pynsted`.\n\nNow access nested values in dictionaries by specifing path to them and using\n`getn` method:\n\n```python\n>>> import pynsted\n>>> a = {1: {2: {\"foo\": \"bar\"}}}\n>>> a.getn([1, 2, \"foo\"])\n'bar'\n>>>\n```\n\nIf at some moment there is no value, corresponding to the next key in path,\n`None` is returned:\n\n```python\n>>> print(a.getn([1, 3, \"foo\"]))\nNone\n>>>\n```\n\nIt is possible to specify the default value, which will be returned instead of\n`None` if there is no value at the given path:\n\n```python\n>>> a.getn([1, 3, \"foo\"], \"new default\")\n'new default'\n>>>\n```\n\n## Linters\n\nThere are two problems with this module and linters:\n\n- unused import\n- nonexisting `.getn()` method for dictionaries\n\n### Unused import\n\nIf the import is really unused (see below why it may not be), do the following:\n\n- for `pylint` put `# pylint: disable=unused-import` in the line right before\n  the import and `# pylint: disable=unused-import` in the line right after, or\n  `pylint: disable=unused-import` as an end comment into the import line\n- for `ruff` put `noqa: F401` as an end comment into the import line\n- for `pyright`: put `# pyright: reportUnusedImport=false` in the line right\n  before the import line and `# pyright: reportUnusedImport=false` in the line\n  right after, or `pyright: ignore` as an end comment into the import line\n\nNotice that in the case of multiple linters combination of end of line comments\nin the import line will not work, and you will need to do something like\n\n```python\n# pylint: disable=unused-import\n# pyright: reportUnusedImport=false\nimport pynsted  # noqa: F401\n# pyright: reportUnusedImport=true\n# pylint: enable=unused-import\n```\n\n### Nonexisting method\n\n#### Quick and dirty pylint solution\n\nAdd inte yoir pylint configuration file something like\n`extension-pkg-whitelist=pynsted`. Notice that method is insecure and allows\nthe authors of `pynsted` to do whatever they want with your computer, and maybe\nalso kill your cat.\n\n### Using protocol\n\nThe benefit of this approach that it will pacify not only `pylint` but\n`pyright`, `ruff`, and `mypy`, and will make import used. The idea is that\n`pytested` module defines `SupportsGetn` protocol, which claims the presence of\n`getn` method. You have to assert that your dictionary is BOTH `dict` and\n`SupportsGetn`:\n\n```python\nassert isinstance(input_dict, dict)\nassert isinstance(input_dict, pynsted.SupportsGetn)\n```\n\n## Development\n\nThe project uses `poetry` tool (highly recommended!), you should know your way\naround. The supplied `Makefile` defines the following targets:\n\n- `test` -- runs `pytest` and demands 100% test coverage\n- `qa` -- runs `test` target and then runs a bunch of linters\n- `format` -- formats the code with `black` and `isort`\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Add a method to dictionaries to get nested values",
    "version": "1.0.8",
    "project_urls": {
        "Homepage": "https://github.com/pragmatic-fi/pynsted",
        "Repository": "https://github.com/pragmatic-fi/pynsted.git"
    },
    "split_keywords": [
        "dictionary",
        " nested"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8fec86281913231ba265b7319d859d31ca3c6025e3ec13650456b084197b811b",
                "md5": "22eebab7b320e2c803e844f743f9d551",
                "sha256": "ddd0cb5e1be93cd4930b5c97b6becaf6f26e7649958f503371539184d49ff536"
            },
            "downloads": -1,
            "filename": "pynsted-1.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "22eebab7b320e2c803e844f743f9d551",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.13,>=3.11",
            "size": 3127,
            "upload_time": "2024-07-05T13:44:21",
            "upload_time_iso_8601": "2024-07-05T13:44:21.322860Z",
            "url": "https://files.pythonhosted.org/packages/8f/ec/86281913231ba265b7319d859d31ca3c6025e3ec13650456b084197b811b/pynsted-1.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4c1c88fee7c5ea0cb32c0adddd81fef62e802f3985609836eb83fde7985d65c1",
                "md5": "6914602624ddc8ec6cb08e0bdbca0d6f",
                "sha256": "64e043c66b606a2f2e41919c25808d9a4fbc5bc49c82a0501e888f2c7135ae2a"
            },
            "downloads": -1,
            "filename": "pynsted-1.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "6914602624ddc8ec6cb08e0bdbca0d6f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.11",
            "size": 2911,
            "upload_time": "2024-07-05T13:44:22",
            "upload_time_iso_8601": "2024-07-05T13:44:22.806709Z",
            "url": "https://files.pythonhosted.org/packages/4c/1c/88fee7c5ea0cb32c0adddd81fef62e802f3985609836eb83fde7985d65c1/pynsted-1.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-05 13:44:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pragmatic-fi",
    "github_project": "pynsted",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pynsted"
}
        
Elapsed time: 0.26982s