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