Skip to main content

Add a method to dictionaries to get nested values

Project description

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:

>>> 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:

>>> 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:

>>> 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

# 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:

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynsted-1.0.8.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

pynsted-1.0.8-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file pynsted-1.0.8.tar.gz.

File metadata

  • Download URL: pynsted-1.0.8.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 FreeBSD/14.1-RELEASE-p2

File hashes

Hashes for pynsted-1.0.8.tar.gz
Algorithm Hash digest
SHA256 64e043c66b606a2f2e41919c25808d9a4fbc5bc49c82a0501e888f2c7135ae2a
MD5 6914602624ddc8ec6cb08e0bdbca0d6f
BLAKE2b-256 4c1c88fee7c5ea0cb32c0adddd81fef62e802f3985609836eb83fde7985d65c1

See more details on using hashes here.

File details

Details for the file pynsted-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: pynsted-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 FreeBSD/14.1-RELEASE-p2

File hashes

Hashes for pynsted-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 ddd0cb5e1be93cd4930b5c97b6becaf6f26e7649958f503371539184d49ff536
MD5 22eebab7b320e2c803e844f743f9d551
BLAKE2b-256 8fec86281913231ba265b7319d859d31ca3c6025e3ec13650456b084197b811b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page