Skip to main content

objns - Transform a Python dictionary into an object

Simple Python utility module that allows to recursively transform any dictionary or dictionary like object into a Python object with dot notation access to its attributes.

This is specially helpful when dealing with large configuration files (from parsing YAML, JSON or TOML) or dealing with a large response from a server.

Install

pip3 install objns

Optionally, install the following package to enable better pretty printing of an object:

pip3 install pprintpp

Usage

Create an object from one or more dictionaries, and set values directly in the constructor:

>>> from objns import Namespace
>>> ns = Namespace(
...     {'one': 100},
...     {'two': 300},
...     {'two': 400, 'three': {'four': 400}},
...     one=200,
... )
>>> ns.two
400
>>> ns.one
200
>>> ns['two']
400

Get and set values recursively:

>>> ns['two'] = 300
>>> ns['two']
300
>>> ns.two = 700
>>> ns['two']
700
>>> ns.two
700
>>> ns.three.four
400
>>> ns['three'].four
400
>>> ns['three']['four']
400

Recursively copy an object:

>>> nscopy = ns.copy()
>>> id(ns) == id(nscopy)
False

Transform back to a dictionary:

>>> asdict = dict(ns)
>>> asdict
{'one': 200, 'two': 400, 'three': {'four': 400}}
>>> type(asdict)
<class 'dict'>

Iterate the object:

>>> for key, value in ns:
...     print(key, value)
...
one 200
two 400
three {'four': 400}

Recursively merge update with other dictionaries:

>>> ns.update({
...     'one': 'override1',
...     'three': {'four': 'override2'},
... })
>>> ns.one
'override1'
>>> ns.three.four
'override2'

Pretty print the data structure:

>>> ns
{'one': 'override1', 'three': {'four': 'override2'}, 'two': 400}
>>> print(str(ns))
{'one': 'override1', 'three': {'four': 'override2'}, 'two': 400}

Preservation of source datatype, such as OrderedDict and other Mapping subclasses:

>>> nso = Namespace(OrderedDict([('one', 100), ('two', 200)]))
>>> nso
OrderedDict([('one', 100), ('two', 200)])
>>> for key, value in nso:
    ...     print(key, value)
    ...
    one 100
    two 200

Repository

https://github.com/kuralabs/objns

Changelog

1.0.0 (2021-05-31)

New

  • Initial release.

License

Copyright (C) 2017-2021 KuraLabs S.R.L

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

Metadata

Release files for objns 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for objns 1.1.0
File Size Uploaded Provenance Actions
objns-1.1.0.tar.gz 7.9 kB Without provenance Details for objns-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for objns 1.1.0
File Interpreter ABI Platform Actions
objns-1.1.0-py3-none-any.whl Python 3 none any Details for objns-1.1.0-py3-none-any.whl

Total release size: 15.8 kB

Release files / objns-1.1.0.tar.gz

Download URL objns-1.1.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1b8d2d78172ab51fa71431d7b12e800cf271a9c79030331d649a2f85aecae1a4
BLAKE2b-256 checksum
How to use checksums
86ea968d3fd0ee5d024758d7108ff1caf4919d5abaf302f0faafb644fec46a32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / objns-1.1.0-py3-none-any.whl

Download URL objns-1.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e781a10d58392c4808a297441dc9f72d59f303bb04cea01f07fa78426082a1ab
BLAKE2b-256 checksum
How to use checksums
3e857c095531ac44ec35cfa5b14a03a7d5ba692287bcbeb786ab3b0baa2cb3e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Security for objns 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page