Giter Site home page Giter Site logo

Recursive dict merge about munch HOT 2 CLOSED

infinidat avatar infinidat commented on July 30, 2024
Recursive dict merge

from munch.

Comments (2)

d1618033 avatar d1618033 commented on July 30, 2024 1

Hey Jason,

Happy to hear you're enjoying this package.

The goal of munch is to be a simple alternative to python's standard dict that supports attribute-style access, without too many additional features besides that.

The merge function idea you suggested sounds really cool, but unfortunately it would be a bit of a feature creep to add it to Munch.

You can always extend the Munch class using inheritance or composition and add this functionality in your own repository if you'd like.

Best of luck,
David

from munch.

zepengchen avatar zepengchen commented on July 30, 2024

Thank you, David,

I use below code to merge 2 dict recursively, then create a new munch object from the new dict object.
def dict_merge(dct, merge_dct):
# https://gist.github.com/angstwad/bf22d1822c38a92ec0a9
""" Recursive dict merge. Inspired by :meth:dict.update(), instead of
updating only top-level keys, dict_merge recurses down into dicts nested
to an arbitrary depth, updating keys. The merge_dct is merged into
dct.
:param dct: dict onto which the merge is executed
:param merge_dct: dct merged into dct
:return: None
"""
for k, v in merge_dct.items():
if (k in dct and isinstance(dct[k], munch.Mapping) and isinstance(merge_dct[k], munch.Mapping)):
dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]

The code doesn't process list(tuple), but it is enough for me now.

Regards,
Jason.

from munch.

Related Issues (20)

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.