-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergedict.py
113 lines (85 loc) · 3.61 KB
/
mergedict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""mergedict - A Python `dict` with a merge() method."""
# The MIT License
# Copyright (c) 2013 Eduardo Naufel Schettino
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
__version__ = (1, 0, 0)
import sys
import inspect
from functools import singledispatch
class MergeDict(dict):
"""Base class for a dict that implements a merge() method.
What exactly merge() depends on the subclass...
"""
def __init__(self, *args, **kwargs):
super(MergeDict, self).__init__(*args, **kwargs)
# register singlesingle dispatch methods
self.merge_value = singledispatch(self.merge_value)
# python 2 version
if sys.version_info[0] < 3: # pragma: no cover
for name, val in inspect.getmembers(self.__class__):
_type = getattr(val, 'merge_dispatch', None)
if _type:
self.merge_value.register(_type, val.__func__)
return
# python 3 version
for name, val in inspect.getmembers(self.__class__, inspect.isfunction):
_type = getattr(val, 'merge_dispatch', None)
if _type:
self.merge_value.register(_type, val)
def merge(self, *args, **kwargs):
"""merge other dict into self"""
class Sentinel: pass
if args:
if len(args) > 1:
msg = "merge() expected at most 1 arguments, got {}."
raise TypeError(msg.format(len(args)))
other = args[0]
else:
other = kwargs
for key, other_value in other.items():
this_value = self.get(key, Sentinel)
if this_value is Sentinel:
self[key] = other_value
else:
self[key] = self.merge_value(this_value, other_value)
class dispatch:
"""decorator to mark methods as single dispatch functions."""
def __init__(self, _type):
self._type = _type
def __call__(self, func):
func.merge_dispatch = self._type
return func
@staticmethod
def merge_value(this, other):
"""default merge operation, just replace the value"""
return other
class ConfigDict(MergeDict):
"""A MergeDict that on merge() extend/update lists/sets/dicts.
Other values are over-written as in dict.update()
"""
@MergeDict.dispatch(list)
def merge_list(this, other):
this.extend(other)
return this
@MergeDict.dispatch(set)
def merge_set(this, other):
this.update(other)
return this
@MergeDict.dispatch(dict)
def merge_dict(this, other):
this.update(other)
return this