-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
39 lines (27 loc) · 1.13 KB
/
Copy pathbasic_usage.py
File metadata and controls
39 lines (27 loc) · 1.13 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
"""Basic usage: register callables with metadata and retrieve them."""
from micro_reg import DuplicateRegistrationError, Registry
reg = Registry("hardware_tools")
@reg.register(name="read_voltage", type="sensor", unit="V")
def read_voltage() -> float:
"""Return the current voltage reading."""
return 5.0
@reg.register(name="read_temperature", type="sensor", unit="C")
def read_temperature() -> float:
"""Return the current temperature reading."""
return 22.5
@reg.register(name="toggle_led", type="actuator", pin=13)
def toggle_led() -> bool:
"""Toggle the onboard LED and return its new state."""
return True
# Retrieve and call each registered function.
for entry_name in ("read_voltage", "read_temperature", "toggle_led"):
func, meta = reg.get(entry_name)
print(f"{entry_name}: result={func()!r} metadata={meta}")
print()
# Attempting to register the same name twice raises DuplicateRegistrationError.
try:
@reg.register(name="read_voltage")
def duplicate() -> float:
return 0.0
except DuplicateRegistrationError as e:
print(f"Caught expected error — name already taken: {e.name!r}")