-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2_output_as_table.py
More file actions
76 lines (72 loc) · 1.77 KB
/
example2_output_as_table.py
File metadata and controls
76 lines (72 loc) · 1.77 KB
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
66
67
68
69
70
71
72
73
74
75
76
from tabularjson import stringify, is_homogeneous, StringifyOptions
data = {
"careTakers": [{"id": 1001, "name": "Joe"}, {"id": 1002, "name": "Sarah"}],
"animals": [
{
"animalId": 1,
"name": "Elephant",
"description": "Elephants are the largest living land animals.",
},
{"animalId": 2, "name": "Giraffe"},
],
}
print("Using the default table strategy:")
print(stringify(data, {"indentation": 2}))
# {
# "careTakers": (
# "id", "name"
# 1001, "Joe"
# 1002, "Sarah"
# ),
# "animals": (
# "animalId", "name", "description"
# 1, "Elephant", "Elephants are the largest living land animals."
# 2, "Giraffe",
# )
# }
print(
"Output as table only when the data is homogeneous (all list items have the same keys):"
)
print(stringify(data, {"indentation": 2, "output_as_table": is_homogeneous}))
# {
# "careTakers": (
# "id", "name"
# 1001, "Joe"
# 1002, "Sarah"
# ),
# "animals": [
# {
# "animalId": 1,
# "name": "Elephant",
# "description": "Elephants are the largest living land animals."
# },
# {
# "animalId": 2,
# "name": "Giraffe"
# }
# ]
# }
print("Output as table only when not containing objects having a key 'animalId':")
options: StringifyOptions = {
"indentation": 4,
"output_as_table": lambda tabular_data, path: "animalId" not in tabular_data[0],
}
print(stringify(data, options))
# {
# "careTakers": (
# "id", "name"
# 1001, "Joe"
# 1002, "Sarah"
# ),
# "animals": [
# {
# "animalId": 1,
# "name": "Elephant",
# "description": "Elephants are the largest living land animals."
# },
# {
# "animalId": 2,
# "name": "Giraffe"
# }
# ]
# }