Recursive Namespace. An extension of SimpleNamespace. Enhance get/set and parse from JSON
Project description
RecursiveNamespace
Description
RecursiveNamespace is an extension of Python's SimpleNamespace that provides enhanced functionality for working with nested namespaces and dictionaries. This package allows easy access and manipulation of deeply nested data structures in an intuitive and Pythonic way.
Installation
To install RecursiveNamespaceV2 from PyPI use the following command.
pip install RecursiveNamespaceV2
If you want to use the github clone, use the following.
git clone https://github.com/pasxd245/RecursiveNamespaceV2.git
cd RecursiveNamespaceV2
python -m venv .venv # to setup a virtual env.
pip install -r .\requirements.txt
Usage
The RecursiveNamespace class can be used in the same way as Python's SimpleNamespace class, but in a recursive fashion. The RecursiveNamespace class can be instantiated with a dictionary or keyword arguments. The RecursiveNamespace class also provides a to_dict() method that returns a dictionary representation of the namespace.
Basic Usage
One of the best use cases of this module is converting dict into a recursive namespace, and back to dict.
Another usage is to convert a dictionary to a recursive namespace.
from recursivenamespace import RNS # or RecursiveNamespace
data = {
'name': 'John',
'age': 30,
'address': {
'street': '123 Main St',
'city': 'Anytown'
},
'friends': ['Jane', 'Tom']
}
rn = RNS(data)
print(type(rn)) # <class 'recursivenamespace.main.recursivenamespace'>
print(rn) # RNS(name=John, age=30, address=RNS(street=123 Main St, city=Anytown))
print(rn.name) # John
print(rn.address.city) # Anytown
print(rn.friends[1]) # Tom, yes it does recognize iterables
# convert back to dictionary
data2 = rn.to_dict()
print(type(data2)) # <class 'dict'>
print(data2 == data) # True
print(data2['address']['city']) # Anytown
print(data2['friends'][1]) # Tom
You can use the key or namespace interchangeably
print(rn.friends[1] is rn['friends'][1]) # True
You can also use it with YAML.
import yaml
from recursivenamespace import RNS
datatext = """
name: John
age: 30
address:
street: 123 Main St
city: Anytown
friends:
- Jane
- Tom
"""
data = yaml.safe_load(datatext)
rn = RNS(data)
print(rn) # RNS(name=John, age=30, address=RNS(street=123 Main St, city=Anytown))
# convert back to YAML
data_yaml = yaml.dump(rn.to_dict())
Let's see other use cases. You can make a nested rns.
from recursivenamespace import RNS
results = RNS(
params=rns(
alpha=1.0,
beta=2.0,
),
metrics=rns(
accuracy=98.79,
f1=97.62
)
)
Access elements as dictionary keys or namespace attributes.
print(results.params.alpha is results.params['alpha']) # True
print(results['metrics'].accuracy is results.metrics['accuracy']) # True
Convert only the metrics to dictionary.
metrics_dict = results.metrics.to_dict()
print(metrics_dict) # {'accuracy': 98.79, 'f1': 97.62}
Or convert all to a nested dictionary.
from pprint import pprint
output_dict = results.to_dict()
pprint(output_dict)
# {'metrics': {'accuracy': 98.79, 'f1': 97.62},
# 'params': {'alpha': 1.0, 'beta': 2.0}}
Flatten the dictionary using a separator for keys.
flat_dict = results.to_dict(flatten_sep='_')
pprint(flat_dict)
# {'metrics_accuracy': 98.79,
# 'metrics_f1': 97.62,
# 'params_alpha': 1.0,
# 'params_beta': 2.0}
Add more fields on the fly.
results.experiment_name = 'experiment_name'
results.params.dataset_version = 'dataset_version'
results.params.gamma = 0.35
The character '-' in a key will be converted to '_'
results.params['some-key'] = 'some-value'
print(results.params.some_key) # some-value
print(results.params['some-key'] is results.params.some_key) # True
print(results.params['some-key'] is results.params['some_key']) # True
Testing
To run tests, navigate to the project's root directory and execute:
pytest -s
# or with coverage:
coverage run -m pytest
The test_recursive_namespace.py file contains tests for the RecursiveNamespace class.
Contributing
Contributions to the RecursiveNamespace project are welcome! Please ensure that any pull requests include tests covering new features or fixes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
You should copy the actual content from examlpes scripts (founde under ./examples/ directory) and paste it into the respective sections of the README. This provides users with immediate examples of how to use your package. The Testing section explains how to run the unit tests, encouraging users to check that everything is working correctly.
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 recursivenamespacev2-0.0.2.tar.gz.
File metadata
- Download URL: recursivenamespacev2-0.0.2.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05e9f8636c0af66fcaf7190f29068f4c94dec61d9c482f30405be6795eecf357
|
|
| MD5 |
1dbd3c3d967fe944535bdace4e96ff0e
|
|
| BLAKE2b-256 |
68d998010aecef5e22df2e78f4723d21192f1a5e401a7c70bbe75a94d79581dc
|
Provenance
The following attestation bundles were made for recursivenamespacev2-0.0.2.tar.gz:
Publisher:
publish.yml on pasxd245/RecursiveNamespaceV2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
recursivenamespacev2-0.0.2.tar.gz -
Subject digest:
05e9f8636c0af66fcaf7190f29068f4c94dec61d9c482f30405be6795eecf357 - Sigstore transparency entry: 169284318
- Sigstore integration time:
-
Permalink:
pasxd245/RecursiveNamespaceV2@f6711949034eae17300470d8d9d689811f3eba92 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/pasxd245
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f6711949034eae17300470d8d9d689811f3eba92 -
Trigger Event:
release
-
Statement type:
File details
Details for the file recursivenamespacev2-0.0.2-py3-none-any.whl.
File metadata
- Download URL: recursivenamespacev2-0.0.2-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
588fbb608f859785bfc66c8316205f7734734866daf40d08061a029f33431d45
|
|
| MD5 |
28b6806f167befd08af22b09a44e4ee3
|
|
| BLAKE2b-256 |
c9403b29b57231f05be3c898d437f8e3f08c599e45fb921e1123f47f0ddb2294
|
Provenance
The following attestation bundles were made for recursivenamespacev2-0.0.2-py3-none-any.whl:
Publisher:
publish.yml on pasxd245/RecursiveNamespaceV2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
recursivenamespacev2-0.0.2-py3-none-any.whl -
Subject digest:
588fbb608f859785bfc66c8316205f7734734866daf40d08061a029f33431d45 - Sigstore transparency entry: 169284321
- Sigstore integration time:
-
Permalink:
pasxd245/RecursiveNamespaceV2@f6711949034eae17300470d8d9d689811f3eba92 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/pasxd245
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f6711949034eae17300470d8d9d689811f3eba92 -
Trigger Event:
release
-
Statement type: