Skip to main content

More Dots! Dot-access to Python dicts like Javascript

Project description

More Dots!

Branch Status
master Build Status
dev Build Status

Overview

This library defines a Data class that can serve as a replacement for dict, with additional features.

>>> from mo_dots import wrap, Data

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

Easy Definition

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

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

>>> wrap({'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 = wrap({"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 == {}
>>> a.b == None
True
>>> a.b.c == None
True
>>> a[None] == None
True

Path assignment

No need to make intermediate dicts

>>> a = Data()
a == {}
>>> 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]}}

Transforming JSON Data in Python

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-2.45.19103.tar.gz (14.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: mo-dots-2.45.19103.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.14

File hashes

Hashes for mo-dots-2.45.19103.tar.gz
Algorithm Hash digest
SHA256 5912d299932ae785152d841c8a3693e7847cd27155bc70ac98bd5f27bae24273
MD5 5850d6cb0efc2d1cbbe260c332d70bcd
BLAKE2b-256 df8e6f5da0c7a7e0f703750f69c57a9add2e6efeaca748a25d9fa1baaa79ac05

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