-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.py
More file actions
66 lines (55 loc) · 2.18 KB
/
Copy pathcode.py
File metadata and controls
66 lines (55 loc) · 2.18 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
# ---------------------------------------------------------------------
# DefaultMunch and DefaultFactoryMunch: graceful handling of missing keys.
# ---------------------------------------------------------------------
from munch import Munch, DefaultMunch, DefaultFactoryMunch
heading("DefaultMunch: a sentinel for missing attributes")
note(
"A regular Munch raises AttributeError for unknown keys. "
"DefaultMunch instead returns a value of your choosing, which is "
"perfect for optional configuration fields."
)
# A common pattern: use a sentinel object so you can tell "missing" apart
# from a legitimate None value.
MISSING = object()
config = DefaultMunch.fromDict(
{
"host": "localhost",
"port": 5432,
"database": {"name": "shop", "user": "ada"},
},
MISSING,
)
note(f"config.host → <strong>{config.host}</strong>")
note(f"config.database.user → <strong>{config.database.user}</strong>")
note(f"config.database.password is MISSING → {config.database.password is MISSING}")
note(f"config.tls_cert is MISSING → {config.tls_cert is MISSING}")
heading("DefaultFactoryMunch: build values on demand")
note(
"When you'd rather create a fresh value for each missing key (think "
"collections.defaultdict), reach for DefaultFactoryMunch. Below we "
"tally word frequencies in a tiny corpus."
)
word_counts = DefaultFactoryMunch(int)
poem = (
"the sea the sky the gull "
"the wind the wave the gull"
)
for word in poem.split():
word_counts[word] += 1
# Attribute-style access works on accumulated keys.
note(f"word_counts.the → <strong>{word_counts.the}</strong>")
note(f"word_counts.gull → <strong>{word_counts.gull}</strong>")
note(f"Unseen word starts at zero: word_counts.dolphin = {word_counts.dolphin}")
display(dict(word_counts), append=True)
# A factory can be any zero-argument callable. Here, lists for grouping.
inbox = DefaultFactoryMunch(list)
messages = [
("ada", "lunch?"),
("grace", "shipped the build"),
("ada", "see you at 1"),
("linus", "patch attached"),
]
for sender, body in messages:
inbox[sender].append(body)
note("Messages grouped by sender:")
display(dict(inbox), append=True)