https://github.com/ntessore/namespacedict
Python dictionary with fully evaluated keys
https://github.com/ntessore/namespacedict
data-structures python
Last synced: about 1 month ago
JSON representation
Python dictionary with fully evaluated keys
- Host: GitHub
- URL: https://github.com/ntessore/namespacedict
- Owner: ntessore
- License: mit
- Created: 2021-06-25T22:05:58.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-10-12T08:29:21.000Z (over 4 years ago)
- Last Synced: 2025-12-14T16:58:04.410Z (7 months ago)
- Topics: data-structures, python
- Language: Python
- Homepage: https://pypi.org/project/namespacedict
- Size: 31.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
namespacedict
=============
|tests| |coverage|
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.
.. |tests| image:: https://github.com/ntessore/namespacedict/actions/workflows/test.yml/badge.svg
:target: https://github.com/ntessore/namespacedict/actions/workflows/test.yml
.. |coverage| image:: https://codecov.io/gh/ntessore/namespacedict/branch/main/graph/badge.svg?token=V0OKE8EBSY
:target: https://codecov.io/gh/ntessore/namespacedict