-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.py
More file actions
60 lines (50 loc) · 2 KB
/
Copy pathcode.py
File metadata and controls
60 lines (50 loc) · 2 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
# ---------------------------------------------------------------------
# Munch + JSON: a comfortable way to work with API-shaped data.
# ---------------------------------------------------------------------
from munch import Munch, munchify, unmunchify
import json
heading("Parsing JSON into a Munch")
note(
"Imagine this JSON came back from a weather API. We parse it as "
"usual, then munchify so we can navigate the response with dots "
"instead of a thicket of brackets and quotes."
)
api_response_text = """
{
"location": {"city": "Reykjavik", "country": "IS"},
"current": {"temperature_c": 4.2, "wind_kph": 22.0, "condition": "cloudy"},
"forecast": [
{"day": "Mon", "high_c": 5, "low_c": -1},
{"day": "Tue", "high_c": 6, "low_c": 0},
{"day": "Wed", "high_c": 3, "low_c": -2}
]
}
"""
raw = json.loads(api_response_text)
weather = munchify(raw)
note(f"weather.location.city → <strong>{weather.location.city}</strong>")
note(
f"weather.current.temperature_c → "
f"<strong>{weather.current.temperature_c} °C</strong>"
)
# Iterate over a list of nested Munches just like a list of dicts.
forecast_lines = [
f"{day.day}: {day.low_c}° to {day.high_c}°"
for day in weather.forecast
]
note("Three-day forecast: " + " · ".join(forecast_lines))
heading("Serializing back out with toJSON()")
note(
"Every Munch has a toJSON() helper. It produces a JSON string just "
"like json.dumps would, so a Munch can travel through any JSON-aware "
"boundary unchanged."
)
# Mutate via dot access, then dump.
weather.current.condition = "snow"
weather.forecast.append(munchify({"day": "Thu", "high_c": 2, "low_c": -3}))
dumped = weather.toJSON()
note("Round-tripped JSON (truncated):")
display(HTML(f"<pre>{dumped[:200]}...</pre>"), append=True)
# And of course, it's still a dict, so json.dumps works directly too.
also_dumped = json.dumps(weather, indent=2)
note(f"json.dumps and toJSON agree: {json.loads(dumped) == json.loads(also_dumped)}")