Skip to main content

Library that allows deep extraction of layered data structures (like JSON).

Project description

Magic Dot

https://img.shields.io/pypi/v/magic_dot.svg https://img.shields.io/pypi/dm/magic_dot.svg https://github.com/bonafideduck/magic_dot/workflows/Sanity/badge.svg Documentation Status

Library that allows deep extraction of layered data structures (like JSON).

Introduction

Magic Dot encapsulates data to allow the versatile extraction of its contents. It is easier to use than setdefault or try: except that typical extraction from a structured like JSON. Consider the following simplified JSON snipppet curl https://api.github.com/events:

import json
data = json.loads("""
  [
    {
      "type": "PushEvent",
      "payload": {
        "commits": [
          {
            "author": {
              "name": "Bubba"
  }}]}}]
""")

magic_dot can retrieve the first name of the first commit, with a default of “nobody” if any part of that chain is missing with the the following:

from magic_dot import MagicDot, NOT_FOUND
name = MagicDot(data)[0].payload.commits[0].author.name.get("nobody")

Since the incoming JSON can’t be trusted, without magic_dot, you have to verify that each layer is there. This can be done with a try: except, nearly as efficiently, but it is more verbose.

try:
  name = data[0]['payload']['commits'][0]['author']['name']
except (IndexError, KeyError):
  name = "nobody"

Other features, like pluck, selective exceptions, attribute support, and iteration can lead to cleaner code.

Features

Forgiving NOT_FOUND Handling

Manipulations of the MagicDot structure will raise no exceptions when one of the attributes or keys are not found. Instead it delays this until the get() call that extracts the data at the end. When the get() is called, there are three ways of handling missing data:

Default is to return magic_dot.NOT_FOUND

>>> md.nonexistent.get()
magic_dot.NOT_FOUND

You can request a default value for magic_dot.NOT_FOUND

>>> md.nonexistent.get('bubba')
'bubba'

Or you can enable exceptions when referencing the nonexistent data

>>> md.exception().nonexistent
---------------------------------------------------------------------------
NotFound                                  Traceback (most recent call last)

Exceptions are not enabled by default. They can be enabled during creation I.E MagicDot(data, exception=True) and switched on and off with the MagicDot::exception(exception=False) method.

Dict and List Item Handling

When a md[item] is encountered, data will be extracted as follows:

  1. If md.__data[item] exists, that is used.

  2. If md.__data.item attribute exists it is used.

  3. If .exception() is enabled, a NotFound exception is raised.

  4. Otherwise md.NOT_FOUND is assigned to the resulting encapsulated data.

Attribute Handling

When a md.key is supplied data will be extracted as follows:

  1. If md.__data.key attribute exists it is used.

  2. If md.__data[key] item exists, it is used.

  3. If .exception() is enabled, a NotFound exception is raised.

  4. Otherwise md.NOT_FOUND is assigned to the resulting md.__data.

Iteration Support

If the currently encapsulated data is an iterable, MagicDot supports iterating over the contained data with the resulting iteration being a MagicDot wrapper around the iterated data.

>>> from collections import namedtuple
>>> data = [1, {'x': 2}, namedtuple('x', 'x')(3)]
>>> for md in MagicDot(data):
...   print(md.get())
1
{'x': 2}
x(x=3)

By default, if an attempt is made to iterate over NOT_FOUND data, a TypeError will be raised. The iteration code can be changed to instead return an empty list. ::

>> md = MagicDot(1, iter_nf_as_empty=True)
>> for x in md.nonexistent:
..   print(md.get())
(prints nothing)

Other Operators

Currently, there is one additional operator, MagicDot::pluck(), which if the encapsulated data is a list, it will attempt to extract a named attribute or key from the entire list. The returned value is a MagicDot with the new plucked list.

Future Enhancement

Future enhancements will be to support many of the Underscore js array and collection capabilities like compact, reject, and count.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.2.0 (2020-04-30)

  • This contains some modifications that are not backwards compatible.

  • Remove .lists() support.

  • Add .pluck() support.

  • Change exception() to be raised when NOT_FOUND is generated instead of at the .get()

  • Make documentation changes to reflect the above.

0.1.1 (2020-03-19)

  • No significant chagnes. Testing github release automations.

0.1.0 (2020-03-19)

  • First release on PyPI.

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

magic_dot-0.2.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

magic_dot-0.2.0-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file magic_dot-0.2.0.tar.gz.

File metadata

  • Download URL: magic_dot-0.2.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for magic_dot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 485de16ba2a380211d323b043936d93428131827a8f66712590e6a2c6eb4aab6
MD5 2e3a22d9ba09377f301779576a2b6130
BLAKE2b-256 a0d5d2912ed38226f5877d672d11108534cb83bc543d4a28fc193da2a8ed1eb8

See more details on using hashes here.

File details

Details for the file magic_dot-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: magic_dot-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for magic_dot-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2ebc2875269b854132ae7970a44a015a2ddc006353c9092d6fb71ca12f619d5e
MD5 5f00c5d3f9304f2c544e7858147648c5
BLAKE2b-256 c13b7ca8aee71c1761ed0cfb4bffe234f90a602ed9103afb177a0ed06c38e1a9

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