Skip to main content

Returns a list of commands/delta to go from one tree of objects to another

Project description

Take 2 sets of containers and provide a (deep) delta between them


At the time this module was started, no modules existed that had this functionality. After poking the difflib module there was code that could be reused to achive part of what i wanted. functionality above and beyond this was trivial to implement (diffing unsorted items)

There are a number of libraries out there that provide diffing functionality however a quick review of these has indicated they are mainly for display output and do not provide an easy way for a program to diff 2 things and then react based ont he results (ie they require you to split lots of strings to get at the information you need)


Objdiff uses difflib built into python for lists and tuples (basically sorted things) and implements its own comparison code for dictonaries. User types are detected via the type and instance comparison and are treated as dictonaries (ie unsorted item => value mappings)

What does this look like?

>>> import objdiff
>>> a = {'a': 1, 'b':[1,2,3], 'c':None}
>>> b = {'a': 1, 'b':[1,4], 'c':'hello'}
>>> objdiff.obj_diff(a, b)
<generator object obj_diff at 0xb6a3da80>

We return an iterator and make use of yield from so you can process large trees of objects efficiently and incremental

>>> list(objdiff.obj_diff(a, b))
[('!', ['c'], (None, 'hello')), ('!', ['b'], ([1, 2, 3], [1, 4]))]

Expanding out the generator we get back a bunch of tuples containing the command value, key path and before and after value

>>> c = {'a':{1: None, 2: 2, 3: 3}, 'b': None}
>>> d = {'a':{1: 1, 2: 2}, 'b': {'1':{}, '2':{'2':2}}}
>>> list(objdiff.obj_diff(c, d))
[('-', ['a', 3], (3, Deleted)), ('!', ['a', 1], (None, 1)), ('!', ['b'], (None, {'1': {}, '2': {'2': 2}}))]

Note in the above how you get a full list of keys to the destined object after the command value

Project details

Download files

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

Files for objdiff, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size (16.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page