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

Changes in version 9.x.x

Escaping a literal dot (.) is no longer (\\.) rather double-dot (..). Escaping a literal dot can still be done with bell (\b)

Changes in version 5.x.x

The Data() constructor only accepts keyword parameters. It no longer accepts a dict, nor does it attempt to clean the input. Replace Data(my_var) with to_data(my_var)

Overview

This library defines a Data class that can serve as a replacement for dict, and acts much like a null-safe dataclass.

See the full documentation for all the features of mo-dots

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 wrap 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

Safe Access

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

>>> a = Data()
>>> a.b == None
True
>>> a.b.c == None
True
>>> a[None] == None
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 is 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-9.582.24095.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

mo_dots-9.582.24095-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file mo-dots-9.582.24095.tar.gz.

File metadata

  • Download URL: mo-dots-9.582.24095.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for mo-dots-9.582.24095.tar.gz
Algorithm Hash digest
SHA256 c7e6c1ce01690891580cbe20922f7845f6915cf7bceff8b4b9f5a2e3d6e5d941
MD5 b2a1971dd1db5c16c826aa3811b9e826
BLAKE2b-256 7125ebaa89ad02c62e58dd46c5eca04b1a73bca680d211fc34f1b05e254daa15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mo_dots-9.582.24095-py3-none-any.whl
Algorithm Hash digest
SHA256 353727b9720ab5ce2187f27dae94b6710d418b12ecf433011722595ed38796a4
MD5 d1a7322c1c00e94526e747fd392b0213
BLAKE2b-256 de69efa4f4a06cc8a1707c689bf446ff79046224732b4114adc52b4efa0e730c

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