A very simple specialization of the dictionary that makes it possible to merge two dictionaries provided that they are compatible : if a given key is present in both dictionaries, it must be associated with the same value.
The method is_compatible_with() allows to check this compatibility.
If possible, dictionaries can then be merged with the merge() method or with the '|' operator. Moreover, '|=' is defined as syntactical sugar for the merge() method.
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
# mergeable_dict - dict with merge() method. # # Author: Jerome Lovy class mergeable_dict(dict): """dict with merge() method.""" def is_compatible_with(self, other): for key in self: if key in other and self[key] != other[key]: return False return True def merge(self, other): for key in other: if key in self: if self[key] != other[key]: raise ValueError else: self[key] = other[key] return self def __ior__(self, other): return self.merge(other) def __or__(self, other): result = mergeable_dict(self) for key in other: if key in result: if result[key] != other[key]: raise ValueError else: result[key] = other[key] return result
Semantics : - The merged dictionary has all the keys (union) of the initial dictionaries. - A key that is present in both initial dictionaries must be associated with the same value : trying to merge dictionaries which possess the same key with two different values will raise an exception.