Git Product home page Git Product logo

dictdiffer's Introduction

Dictdiffer

Dictdiffer is a helper module that helps you to diff and patch dictionaries.

dictdiffer.diff

Compares two dictionary object and returns a diff result.

dictdiffer.patch

Applies the diff result to the old dictionary.

dictdiffer.swap

Swaps the diff result to revert changes.

dictdiffer.revert

A shortcut function that swaps and patches the diff result to destination dictionary.

Installation

$ pip install dictdiffer

Examples

from dictdiffer import diff, patch, swap, revert

first = {
    "title": "hello",
    "fork_count": 20,
    "stargazers": ["/users/20", "/users/30"],
    "settings": {
        "assignees": [100, 101, 201],
    }
}


second = {
    "title": "hellooo",
    "fork_count": 20,
    "stargazers": ["/users/20", "/users/30", "/users/40"],
    "settings": {
        "assignees": [100, 101, 202],
    }
}

result = diff(first, second)

assert list(result) == [
    ('push', 'settings.assignees', [202]),
    ('pull', 'settings.assignees', [201]),
    ('push', 'stargazers', ['/users/40']),
    ('change', 'title', ('hello', 'hellooo'))]

Now we can apply the diff result with patch method.

result = diff(first, second)
patched = patch(result, first)

assert patched == second

Also we can swap the diff result with swap method.

result = diff(first, second)
swapped = swap(result)

assert list(swapped) == [
    ('pull', 'settings.assignees', [202]),
    ('push', 'settings.assignees', [201]),
    ('pull', 'stargazers', ['/users/40']),
    ('change', 'title', ('hellooo', 'hello'))]

Let's revert the last changes.

reverted = revert(result, patched)
assert reverted == first

Why I built it?

I decided to make revision history mechanism in dbpatterns.com for documents. Documents are storing in MongoDB, so I was needed a dictionary differ that diffs two dictionary and reverts back changes.

Contribution & Tests

After your changes, you could run the tests to ensure everything is operating correctly.

$ source run_tests.sh
Ran 15 tests in 0.002s

Name         Stmts   Miss  Cover   Missing
------------------------------------------
dictdiffer      74      0   100%
------------------------------------------
TOTAL           74      0   100%

dictdiffer's People

Contributors

brianr avatar jwpeddle avatar

Watchers

 avatar

Forkers

vmfarms

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.