-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
65 lines (47 loc) · 1.51 KB
/
helpers.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
from typing import Dict, List
def pretty_json(d: Dict):
s = json.dumps(d, sort_keys=True, indent=4, separators=(',', ': '))
return s
def print_pretty_json(d: Dict):
s = pretty_json(d)
print(s)
def recursive_get_keys(d: Dict, skip_keys: List = None) -> List:
"""
d = {'z': {'y': {1: ''}}, 'a': 1, 'b': {'c': 3, 'd': []}}
recursive_get_keys(d)
=> returns [1, 'b', 'y', 'z', 'd', 'c', 'a']
:param d: Dict to extract keys from
:param skip_keys:
:return: List of all keys in the dictionary and inner dictionaries
"""
if not skip_keys:
skip_keys = []
the_keys = set()
for key, val in d.items():
if key not in skip_keys:
the_keys.add(key)
if isinstance(val, dict):
more_keys = recursive_get_keys(val)
for k in more_keys:
the_keys.add(k)
return list(the_keys)
class LazyIterator:
"""
An iterator that will evaluate the items only when the iteration starts
"""
def __init__(self, get_items):
self._items = None
self._count = None
self._next_index = -1
self._get_items = get_items
def __iter__(self):
return self
def __next__(self):
self._next_index += 1
if self._items is None:
self._items = self._get_items()
self._count = len(self._items)
if self._next_index < self._count:
return self._items[self._next_index]
raise StopIteration