-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
114 lines (94 loc) · 4.06 KB
/
Copy pathexamples.py
File metadata and controls
114 lines (94 loc) · 4.06 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Runnable examples for the baseh Python package.
Run from python/: PYTHONPATH=src python3 examples/examples.py
"""
from baseh import (
Baseh,
BasehError,
baseh_expandable_v1,
baseh_medium_v1,
decode,
encode,
)
def show(label, fn):
try:
print(f"{label} -> {fn()}")
except BasehError as e:
print(f"{label} -> raises BasehError [{e.code}]: {e}")
# 1. Expandable mode: shown here as the new default. Codes start at 4
# characters and grow automatically as ids climb; shorter codes keep
# decoding forever.
print("== expandable ==")
expandable = Baseh(baseh_expandable_v1())
show("encode(123456789)", lambda: expandable.encode(123456789))
# 4 characters at this namespace size; grows as ids climb
show(
"decode(...) round trip",
lambda: expandable.decode(expandable.encode(123456789)).id,
)
show('decode lowercase', lambda: expandable.decode(expandable.encode(42).lower()).id)
# 2. Zero configuration: package-level encode/decode on the default
# expandable v1 tier; decode returns the full DecodeResult.
print("== zero config ==")
show("encode(123456789)", lambda: encode(123456789))
show("decode(...) round trip", lambda: decode(encode(123456789)).id)
show("decode lowercase", lambda: decode(encode(42).lower()).id)
# 3. A frozen preset: load baseh-medium-v1 and use the full codec.
print("== preset ==")
medium = Baseh(baseh_medium_v1())
show("encode(123456789)", lambda: medium.encode(123456789))
show('decode("C8XP-8J49").id', lambda: medium.decode("C8XP-8J49").id)
show('decode("UORY-PDCA").id (typed aliases)', lambda: medium.decode("UORY-PDCA").id)
show("encode(813) (blocked word)", lambda: medium.encode(813))
show('decode("CC8G-AZ2X") (checksum typo)', lambda: medium.decode("CC8G-AZ2X"))
show("capacity", lambda: medium.capacity())
# Assisted correction: a user heard a spoken C as a G and typed it wrong.
def corrected_demo():
result = medium.decode(
"GC8G-AZ2V", try_correction=True, confusion_profile="heavy"
)
return f"Identifier: {result.id}, corrected to {result.canonical_code}"
show('decode("GC8G-AZ2V") (heard C as G)', corrected_demo)
# 4. Customized: load a preset, extend the body and regroup the output.
print("== customized ==")
custom = baseh_medium_v1()
custom["profileId"] = "orders-v1"
custom["bodyLength"] = 7
custom["grouping"] = [4, 5]
orders = Baseh(custom)
show("encode(123456789)", lambda: orders.encode(123456789))
show("decode(...) round trip", lambda: orders.decode(orders.encode(123456789)).id)
show('decode("ZC8V-REMJ2") (bad check)', lambda: orders.decode("ZC8V-REMJ2"))
show("capacity", lambda: orders.capacity())
# 5. Customized expandable: start longer, hyphenate only at 8+ characters.
# Profiles carry "mode" ("expandable" or "fixed"); expandable adds
# "minLength" (default 4) and "separatorMinLength" (the shipped tier
# uses 6). A custom bodyAlphabet has any 0/O silently removed.
print("== customized expandable ==")
custom_exp = baseh_expandable_v1()
custom_exp["profileId"] = "tickets-v1"
custom_exp["mode"] = "expandable" # already set by the helper; shown for clarity
custom_exp["minLength"] = 5
custom_exp["separatorMinLength"] = 8
tickets = Baseh(custom_exp)
show("encode(123456789)", lambda: tickets.encode(123456789))
# 5+ characters, no hyphen until codes reach 8 characters
show(
"decode(...) round trip",
lambda: tickets.decode(tickets.encode(123456789)).id,
)
# 6. A view helper: one shared codec built at import time, records rendered
# as codes at the edge. Register baseh_code as a template filter in Django
# ({{ order.id|baseh_code }}); here it is exercised framework-free with a
# plain class. The matching decode-side pattern is in docs/cookbook.md
# ("Framework view helpers").
print("== view helper ==")
codec = Baseh(baseh_expandable_v1())
def baseh_code(record):
return codec.encode(record.id)
class Order:
def __init__(self, id):
self.id = id
order = Order(123456)
print(f"{{{{ order.id|baseh_code }}}} -> {baseh_code(order)}")
show("decode round trip", lambda: codec.decode(baseh_code(order)).id)
show("decode (bogus code)", lambda: codec.decode("ZZZZ-ZZZZ").id)