Skip to main content

More Dots! Dot-access to Python dicts like Javascript

Project description

More Dots!

PyPI Latest Release Build Status Coverage Status Downloads

Overview

Box your JSON-like data in a Data object to get null-safe dot access. This library is a replacement for dict that is more consistent and easier to use.

See full documentation for all the features of mo-dots

See change log to read about major changes

Create instances

Define Data using named parameters, just like you would a dict

>>> from mo_dots import Data
>>> Data(b=42, c="hello world")
Data({'b': 42, 'c': 'hello world'})

You can also box an existing dicts so they can be used like Data

>>> from mo_dots import to_data
>>> to_data({'b': 42, 'c': 'hello world'})
Data({'b': 42, 'c': 'hello world'})

Dot Access

Access properties with attribute dots: a.b == a["b"]. You have probably seen this before.

Path Access

Access properties by dot-delimited path.

>>> a = to_data({"b": {"c": 42}})
>>> a["b.c"] == 42
True

Null-Safe Access

If a property does not exist then return Null rather than raising an error.

>>> a = Data()
>>> a.b == Null
True
>>> a.b.c == Null
True
>>> a[None] == Null
True

Path assignment

No need to make intermediate dicts

>>> a = Data()
>>> a["b.c"] = 42   # same as a.b.c = 42
a == {"b": {"c": 42}}

Path accumulation

Use += to add to a property; default zero (0)

>>> a = Data()
a == {}
>>> a.b.c += 1
a == {"b": {"c": 1}}
>>> a.b.c += 42
a == {"b": {"c": 43}}

Use += with a list ([]) to append to a list; default empty list ([])

>>> a = Data()
a == {}
>>> a.b.c += [1]
a == {"b": {"c": [1]}}
>>> a.b.c += [42]
a == {"b": {"c": [1, 42]}}

Serializing to JSON

The standard Python JSON library does not recognize Data as serializable. You may overcome this by providing default=from_data; which converts the data structures in this module into Python primitives of the same.

from mo_dots import from_data, to_data

s = to_data({"a": ["b", 1]})
result = json.dumps(s, default=from_data)  

Alternatively, you may consider mo-json which has a function value2json that converts a larger number of data structures into JSON.

Summary

This library is the basis for a data transformation algebra: We want a succinct way of transforming data in Python. We want operations on data to result in yet more data. We do not want data operations to raise exceptions. This library also solves Python's lack of consistency (lack of closure) under the dot (.) and slice [::] operators when operating on data objects.

Full documentation

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mo_dots-10.647.24166.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

mo_dots-10.647.24166-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file mo_dots-10.647.24166.tar.gz.

File metadata

  • Download URL: mo_dots-10.647.24166.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for mo_dots-10.647.24166.tar.gz
Algorithm Hash digest
SHA256 9180a5983712f2fa754d3236d6126e9d0614b70ab897fc382f95d9b811c4fd70
MD5 4019cb9dc4820f6784de4c6d58d6c3be
BLAKE2b-256 767912745e95179e6857bae5b7942773d6812d3615557f7e27f681a94863086d

See more details on using hashes here.

File details

Details for the file mo_dots-10.647.24166-py3-none-any.whl.

File metadata

File hashes

Hashes for mo_dots-10.647.24166-py3-none-any.whl
Algorithm Hash digest
SHA256 7365cf5d0711bd4ce680d9b32f377d1156b88010e2c7905cc67b8ab40dae5d65
MD5 e2327db078559f34b4425355fc69bdc5
BLAKE2b-256 a991b4af976397b00cdcd2330e8ca1517d0e9eede73703f26b6773501a090cdc

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