| Total Complexity | 20 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | |||
| 3 | from six import string_types |
||
| 4 | |||
| 5 | import copy |
||
| 6 | import json |
||
| 7 | |||
| 8 | |||
| 9 | def clean(d, strings=True, dicts=True, lists=True): |
||
| 10 | keys = list(d.keys()) |
||
| 11 | for key in keys: |
||
| 12 | value = d.get(key, None) |
||
| 13 | if not value: |
||
| 14 | if value is None or \ |
||
| 15 | strings and isinstance(value, string_types) or \ |
||
| 16 | dicts and isinstance(value, dict) or \ |
||
| 17 | lists and isinstance(value, (list, tuple, )): |
||
| 18 | del d[key] |
||
| 19 | |||
| 20 | |||
| 21 | def clone(d): |
||
| 22 | return copy.deepcopy(d) |
||
| 23 | |||
| 24 | |||
| 25 | def dump(data): |
||
| 26 | def encoder(obj): |
||
| 27 | json_types = (bool, dict, float, int, list, tuple, ) + string_types |
||
| 28 | if not isinstance(obj, json_types): |
||
| 29 | return str(obj) |
||
| 30 | return json.dumps(data, indent=4, sort_keys=True, default=encoder) |
||
| 31 | |||
| 32 | |||
| 33 | def filter(d, predicate): |
||
| 34 | f = {} |
||
| 35 | keys = d.keys() |
||
| 36 | for key in keys: |
||
| 37 | val = d.get(key, None) |
||
| 38 | if predicate(key, val): |
||
| 39 | f[key] = val |
||
| 40 | return f |
||
| 41 | |||
| 42 | |||
| 43 | def merge(d, other): |
||
| 44 | for key, value in other.copy().items(): |
||
| 45 | src = d.get(key, None) |
||
| 46 | if isinstance(src, dict) and isinstance(value, dict): |
||
| 47 | merge(src, value) |
||
| 48 | else: |
||
| 49 | d[key] = value |
||
| 50 | return d |
||
| 51 |