MCPcopy Create free account
hub / github.com/ipython/ipython / merge

Method merge

IPython/utils/ipstruct.py:273–390  ·  view source on GitHub ↗

Merge two Structs with customizable conflict resolution. This is similar to :meth:`update`, but much more flexible. First, a dict is made from data+key=value pairs. When merging this dict with the Struct S, the optional dictionary 'conflict' is used to decide what to

(self, __loc_data__=None, __conflict_solve=None, **kw)

Source from the content-addressed store, hash-verified

271 object.__setattr__(self, '_allownew', allow)
272
273 def merge(self, __loc_data__=None, __conflict_solve=None, **kw):
274 """Merge two Structs with customizable conflict resolution.
275
276 This is similar to :meth:`update`, but much more flexible. First, a
277 dict is made from data+key=value pairs. When merging this dict with
278 the Struct S, the optional dictionary 'conflict' is used to decide
279 what to do.
280
281 If conflict is not given, the default behavior is to preserve any keys
282 with their current value (the opposite of the :meth:`update` method's
283 behavior).
284
285 Parameters
286 ----------
287 __loc_data : dict, Struct
288 The data to merge into self
289 __conflict_solve : dict
290 The conflict policy dict. The keys are binary functions used to
291 resolve the conflict and the values are lists of strings naming
292 the keys the conflict resolution function applies to. Instead of
293 a list of strings a space separated string can be used, like
294 'a b c'.
295 kw : dict
296 Additional key, value pairs to merge in
297
298 Notes
299 -----
300
301 The `__conflict_solve` dict is a dictionary of binary functions which will be used to
302 solve key conflicts. Here is an example::
303
304 __conflict_solve = dict(
305 func1=['a','b','c'],
306 func2=['d','e']
307 )
308
309 In this case, the function :func:`func1` will be used to resolve
310 keys 'a', 'b' and 'c' and the function :func:`func2` will be used for
311 keys 'd' and 'e'. This could also be written as::
312
313 __conflict_solve = dict(func1='a b c',func2='d e')
314
315 These functions will be called for each key they apply to with the
316 form::
317
318 func1(self['a'], other['a'])
319
320 The return value is used as the final merged value.
321
322 As a convenience, merge() provides five (the most commonly needed)
323 pre-defined policies: preserve, update, add, add_flip and add_s. The
324 easiest explanation is their implementation::
325
326 preserve = lambda old,new: old
327 update = lambda old,new: new
328 add = lambda old,new: old + new
329 add_flip = lambda old,new: new + old # note change of order!
330 add_s = lambda old,new: old + ' ' + new # only for str!

Callers 3

__iadd__Method · 0.95
__add__Method · 0.80
_run_with_profilerMethod · 0.80

Calls 3

__dict_invertMethod · 0.95
copyMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected