This package provides a NamespaceDict mapping which fully evaluates keys using Python syntax. For example, it can perform nested lookups, attribute lookups, and tuple splicing:
>>> from namespacedict import NamespaceDict >>> >>> # create a new namespace >>> ns = NamespaceDict() >>> >>> # nested lookup from a list >>> ns['x'] = [1, 2, 3] >>> ns['x[1]'] 2 >>> # set a docstring attribute >>> ns['y'] = lambda x: x >>> ns['y.__doc__'] = 'my function' >>> ns['y.__doc__'] 'my function' >>> >>> # set three items from a tuple >>> ns['a, b, c'] = 'A', 'B', 'C' >>> ns['b'] 'B'
Keys are parsed in a safe way using Python’s AST library. It is thus possible to create complex dictionary queries that work as expected:
>>> ns['one'] = 1 >>> ns['two'] = 2 >>> ns['x[0:two]'] = 5, 4 >>> ns['x[::-one]'] [3, 4, 5]
The NamespaceDict type can also be used as an adapter for other mappings, by passing the underlying data structure on initialisation:
>>> # create a numpy array with named columns
>>> import numpy as np
>>> a = np.empty(5, dtype=[('col1', int), ('col2', int), ('col3', int)])
>>>
>>> # use NamespaceDict to access array
>>> ns = NamespaceDict(a)
>>>
>>> # access named columns through namespace
>>> ns['col1, col2, col3'] = 1, 2, 3
>>> ns['col2']
array([2, 2, 2, 2, 2])
The mapping can be retrieved using the .data attribute.
Metadata
Release files for namespacedict 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| namespacedict-0.3.0.tar.gz | 3.8 kB | Details |
Release files / namespacedict-0.3.0.tar.gz
| Download URL | namespacedict-0.3.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
70d8bde0672a090b4fdf291cab58a362d6d1cad8f001915471d94b55073442af
|
|
BLAKE2b-256 checksum How to use checksums |
db8a500afef50f0a4d11fd119c724788c66827a38ca60560b9e45e8c2c1c83f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5
|