Skip to main content

No project description provided

Project description

addict-tracking-changes

Introduction

Originally, this repository was a fork of addict by Mats Julian Olsen. Overtime, it has substatially diverged in functionality and codebase that it seemed to make sense to breakout as its own repository.

The original addict: provides an alternative and succient interface to manipulate a dictionary. This is especially useful when dealing with heavily nested dictionaries. As example (taken from https://github.com/mewwts/addict) a dictionary created using standard python dictionary interface looks as follows:

body = {
    'query': {
        'filtered': {
            'query': {
                'match': {'description': 'addictive'}
            },
            'filter': {
                'term': {'created_by': 'Mats'}
            }
        }
    }
}`

can be summarized to following three lines:

body = Dict()
body.query.filtered.query.match.description = 'addictive'
body.query.filtered.filter.term.created_by = 'Mats'

This repo builds on original addict and adds contraptions to track key additions in the dictionary. This features comes in quite handy in building reactive webapps where one has to respond to all the changes made on the browser. Addict-tracking-changes is the underpinning data-structure in ofjustpy: a python based webframework build from justpy

The functions relevant to tracking changed history are: get_changed_history and clear_changed_history. The get_changed_history returns an iterator of XPath style paths like /a/b/c/e (see Demo example).

Usage and examples

Installation

This project is not on PyPI. Its a simple package with no third party dependency. Simply clone from github and include the source directory in your PYTHONPATH.

Demo example

from addict import Dict
body = Dict()
body.query.filtered.query.match.description = 'addictive'
body.query.filtered.filter.term.created_by = 'Mats'

for changed_path in body.get_changed_history():
    #<work with changed_path>

body.clear_changed_history()

Behaviour when values are instances of container types

addict works as expected when the values of keys are simple data types (such as string, int, float, etc.). However, for container types such as dict, list, tuples, etc. the behaviour is somewhat differs.

  • dicts are treated as opaque object just like simple data types
from addict import Dict

mydict = Dict()
mydict.a.b.c = {'kk': 1}
mydict.a.b.e = {'dd': 1}

for _ in mydict.get_changed_history():
    print(_) 

will print

/a/b/c
/a/b/e

and not

/a/b/cc/kk
/a/b/e/dd
  • lists are seen as container, i.e., get_changed_history will report path for each element of the list
from addict import Dict

mydict = Dict()

mydict.a.b = [1, [1]]
mydict.a.c = [2, [2, [3]]]

get_changed_history will report following paths:

/a/b/0,
/a/b/1/0,
/a/c/0,
/a/c/1/0,
/a/c/1/1/"
  • tuple tuple behave same as dict

  • sets sets behave same as dict

Known bugs and Caveats

  1. Only tracks field additions. Deletions and updates are not tracked.
  2. freeze doesn't guards against deletions
  3. building dict from another dict as shown in following expression wont' work
cjs_cfg = Dict(x, track_changes=True)

instead use

cjs_cfg = Dict(track_changes = True)
with open("cjs_cfg.pickle", "rb") as fh:
    x = pickle.load(fh)
for _ in oj.dictWalker(x):
    oj.dnew(cjs_cfg, _[0], _[1])

EndNotes

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

addict_tracking_changes-0.1.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

addict_tracking_changes-0.1.0-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file addict_tracking_changes-0.1.0.tar.gz.

File metadata

File hashes

Hashes for addict_tracking_changes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f4a2df62abaeacd9254cd7e242e455a57e900e69e60e6a76de375bdedce1dfa0
MD5 5546cc3a4c3f4c0ec8ddb5417147d59f
BLAKE2b-256 82bdf1cf01815a0c105b68d128bbae08ce111df0248361cc81edbb8e6f408c45

See more details on using hashes here.

File details

Details for the file addict_tracking_changes-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for addict_tracking_changes-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 349362b81059de1382be1994058ebe2e81fe5c7c8fecab3ad1cacb87e700ec8e
MD5 f0f333fb20bdd2912f8e24d2063ab7b0
BLAKE2b-256 ef3b14033a4b146de4b410b59a9a9b474bd2a781be1190e4e4b1b69fe6017b28

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