-
Notifications
You must be signed in to change notification settings - Fork 0
/
second_problem.py
56 lines (42 loc) · 1.2 KB
/
second_problem.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
from dataclasses import dataclass
@dataclass
class Person:
first_name: str
last_name: str
father: object = None
def prepare_test_dictionary():
p1 = Person(first_name='name', last_name='last')
p2 = Person(first_name='name', last_name='last', father=p1)
return {
'a': 'aaa',
'b': {
'e': 'bbb',
'f': 'bbb',
'g': {
'h': 'ccc',
'p': p2,
},
},
'c': 'aaa',
'd': 'aaa',
}
def print_factory(value, depth):
if isinstance(value, dict):
return print_dictionary(value, depth+1)
if isinstance(value, Person):
return print_person(value, depth+1)
def print_dictionary(dictionary, depth):
keys = dictionary.keys()
for key in keys:
print(key, depth)
print_factory(dictionary[key], depth=depth)
def print_person(person, depth):
print("first_name", depth)
print("last_name", depth)
print("father", depth)
return print_factory(person.father, depth=depth)
def print_depth(data):
print_factory(data, depth=0)
if __name__ == '__main__':
test_dictionary = prepare_test_dictionary()
print_depth(data=test_dictionary)