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
pylintput# pylint: disable=unused-importin the line right before the import and# pylint: disable=unused-importin the line right after, orpylint: disable=unused-importas an end comment into the import line - for
ruffputnoqa: F401as an end comment into the import line - for
pyright: put# pyright: reportUnusedImport=falsein the line right before the import line and# pyright: reportUnusedImport=falsein the line right after, orpyright: ignoreas 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-- runspytestand demands 100% test coverageqa-- runstesttarget and then runs a bunch of lintersformat-- formats the code withblackandisort
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64e043c66b606a2f2e41919c25808d9a4fbc5bc49c82a0501e888f2c7135ae2a
|
|
| MD5 |
6914602624ddc8ec6cb08e0bdbca0d6f
|
|
| BLAKE2b-256 |
4c1c88fee7c5ea0cb32c0adddd81fef62e802f3985609836eb83fde7985d65c1
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddd0cb5e1be93cd4930b5c97b6becaf6f26e7649958f503371539184d49ff536
|
|
| MD5 |
22eebab7b320e2c803e844f743f9d551
|
|
| BLAKE2b-256 |
8fec86281913231ba265b7319d859d31ca3c6025e3ec13650456b084197b811b
|