Giter Site home page Giter Site logo

mergedict's Introduction

mergedict - A Python dict with a merge() method

https://travis-ci.org/schettino72/mergedict.png?branch=master https://coveralls.io/repos/schettino72/mergedict/badge.png

A MergeDict is a dict with a merge() method. merge() is like dict.update()...

from mergedict import MergeDict

d1 = MergeDict({'a': 1, 'b': 'one'})
d1.merge({'a':2, 'c': [2]})

assert d1 == {'a': 2, 'c': [2], 'b': 'one'}

A MergeDict can be subclassed to create custom "merge" operations based on the type of an item value.

from mergedict import MergeDict

class SumDict(MergeDict):
      @MergeDict.dispatch(int)
      def merge_int(this, other):
          return this + other

d2 = SumDict({'a': 1, 'b': 'one'})
d2.merge({'a':2, 'b': 'two'})

assert d2 == {'a': 3, 'b': 'two'}

mergedict module comes with a ConfigDict that will extend/update lists/sets/dicts.

from mergedict import ConfigDict

d3 = ConfigDict({'a': 1, 'my_list': [1, 2]})
d3.merge({'a':2, 'my_list': [3, 4]})

assert d3 == {'a': 2, 'my_list': [1, 2, 3, 4]}

Project Details

license

The MIT License Copyright (c) 2013 Eduardo Naufel Schettino

see LICENSE file

developers / contributors

  • Eduardo Naufel Schettino โ€” main author
  • Sebastian Pipping โ€” build system fixes

install

$ pip install mergedict

or download and:

$ python setup.py install

tests

To run the tests:

$ py.test

mergedict's People

Contributors

hartwork avatar schettino72 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

psaux

mergedict's Issues

Cannot install from PyPI

When I do pip install mergedict I have:

  Downloading mergedict-0.1.1.tar.gz
  Running setup.py (path:/home/spinus/.virtualenvs/sphinx/build/mergedict/setup.py) egg_info for package mergedict
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/home/spinus/.virtualenvs/sphinx/build/mergedict/setup.py", line 10, in <module>
        os.path.join(os.path.dirname(__file__), 'README.rst'), 'r', 'utf8',
      File "/home/spinus/.virtualenvs/sphinx/lib/python2.7/codecs.py", line 878, in open
        file = __builtin__.open(filename, mode, buffering)
    IOError: [Errno 2] No such file or directory: '/home/spinus/.virtualenvs/sphinx/build/mergedict/README.rst'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/home/spinus/.virtualenvs/sphinx/build/mergedict/setup.py", line 10, in <module>

    os.path.join(os.path.dirname(__file__), 'README.rst'), 'r', 'utf8',

  File "/home/spinus/.virtualenvs/sphinx/lib/python2.7/codecs.py", line 878, in open

    file = __builtin__.open(filename, mode, buffering)

IOError: [Errno 2] No such file or directory: '/home/spinus/.virtualenvs/sphinx/build/mergedict/README.rst'

----------------------------------------
Cleaning up...

does not merge nested arrays

import json
from mergedict import ConfigDict

ds1 = ConfigDict({
  'a': 1,
  'b': [1,2,3],
  'c': {
    'd': [3,4]
  }
})
ds1.merge({
  'a': 2,
  'b': [4,5,6],
  'c': {
    'd': [1,2]
  }
})
print(json.dumps(ds1, indent=2))

gives the output

{
  "a": 2,
  "c": {
    "d": [
      1,
      2
    ]
  },
  "b": [
    1,
    2,
    3,
    4,
    5,
    6
  ]
}

Outer array in merged. But the inner one (pointed by 'd') is overwritten - is this the expected behavior?

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.