| Total Complexity | 20 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | |||
| 3 | from six import string_types |
||
| 4 | |||
| 5 | |||
| 6 | def all_keys(d): |
||
| 7 | keys = [] |
||
| 8 | for key, value in d.items(): |
||
| 9 | if key not in keys: |
||
| 10 | keys.append(key) |
||
| 11 | if isinstance(value, dict): |
||
| 12 | keys += all_keys(value) |
||
| 13 | return keys |
||
| 14 | |||
| 15 | |||
| 16 | def check_keys(keys, separator): |
||
| 17 | if not separator: |
||
| 18 | return |
||
| 19 | for key in keys: |
||
| 20 | if key and isinstance(key, string_types) and separator in key: |
||
| 21 | raise ValueError( |
||
| 22 | 'keys should not contain keypath separator ' |
||
| 23 | '\'{}\'.'.format(separator)) |
||
| 24 | |||
| 25 | |||
| 26 | def follow_keys(d, keys): |
||
| 27 | item_keys = keys[:-1] |
||
| 28 | item_key = keys[-1] |
||
| 29 | item_parent = d |
||
| 30 | i = 0 |
||
| 31 | j = len(item_keys) |
||
| 32 | while i < j: |
||
| 33 | key = item_keys[i] |
||
| 34 | try: |
||
| 35 | item_parent = item_parent[key] |
||
| 36 | except KeyError: |
||
| 37 | item_parent = None |
||
| 38 | break |
||
| 39 | i += 1 |
||
| 40 | return (item_parent, item_key, ) |
||
| 41 | |||
| 42 | |||
| 43 | def join_keys(keys, separator): |
||
| 44 | return separator.join(keys) |
||
| 45 | |||
| 46 | |||
| 47 | def split_keys(key, separator): |
||
| 48 | if not separator: |
||
| 49 | return [key] |
||
| 50 | elif isinstance(key, string_types): |
||
| 51 | keypath = key |
||
| 52 | if separator in keypath: |
||
| 53 | keys = list(keypath.split(separator)) |
||
| 54 | return keys |
||
| 55 | else: |
||
| 56 | return [key] |
||
| 57 | elif isinstance(key, (list, tuple, )): |
||
| 58 | keys = [] |
||
| 59 | for key_item in key: |
||
| 60 | keys += split_keys(key_item, separator) |
||
| 61 | return keys |
||
| 62 | else: |
||
| 63 | return [key] |
||
| 64 |