Mercurial > hgrepos > Python > libs > ConfigMix
diff configmix/__init__.py @ 10:58af59d5af40
A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Wed, 09 Mar 2016 11:19:20 +0100 |
| parents | 6835a5663008 |
| children | aecb36d4025f |
line wrap: on
line diff
--- a/configmix/__init__.py Wed Mar 09 11:09:33 2016 +0100 +++ b/configmix/__init__.py Wed Mar 09 11:19:20 2016 +0100 @@ -6,11 +6,6 @@ See LICENSE for details. """ -# -# NOTE: ONLY STANDARDLIB-IMPORTS IN THIS MODULE. -# ITS JUST FOR BOOTSTRAPPING WITH PYTHON2 and PYTHON3. -# NO `future`, `six`, ... -# from __future__ import division, print_function, absolute_import @@ -18,6 +13,9 @@ __version__ = "0.0.dev0" +import copy + + __all__ = [] @@ -54,3 +52,18 @@ else: user[k] = merge(user[k], v) return user + + +def safe_merge(user, default): + """A more safe version of `merge()` that makes shallow copies of + the container objects. + + """ + user = copy.copy(user) + if isinstance(user, dict) and isinstance(default, dict): + for k, v in default.items(): + if k not in user: + user[k] = copy.copy(v) + else: + user[k] = merge(user[k], v) + return user
