Skip to main content

Transform a Python dictionary into an object

Project description

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.

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

objns-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

objns-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file objns-1.0.0.tar.gz.

File metadata

  • Download URL: objns-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for objns-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8c5515e17964f49c3af385a8f053b0ca06a97cdf499dea671b1689109964da35
MD5 3f50d7c7dbf5a27830665a62e4636d01
BLAKE2b-256 13bafd3462b5cf4df2f15e6c7e703537ff0fdf0ba5fbd9eb181718874e268d2d

See more details on using hashes here.

File details

Details for the file objns-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: objns-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for objns-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9355a29f02330d225e69e87e57a846afea72abe487f10a6b0a600ea12a66fd29
MD5 e47b18e1fce90ef3582b0b8ac904003a
BLAKE2b-256 6c0ac14f7c8ccbd5cb0bbc6cdaab1b9749e8b905f683a6e71f65881ed89beffe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page