-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature.py
More file actions
86 lines (61 loc) · 1.99 KB
/
temperature.py
File metadata and controls
86 lines (61 loc) · 1.99 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
"""
Example: Designing a class from scratch with AgentReadableMixin.
Shows how __agent_notes__() accumulates through inheritance:
Sensor provides base rules, CalibratedSensor adds calibration-specific rules.
Run this file to see both outputs:
python examples/temperature.py
"""
import os
from agent_readable import AgentReadableMixin, agent_help
class Sensor(AgentReadableMixin):
"""
Reads a value from a hardware sensor.
Agent usage:
Run ``agent_help(Sensor)`` before using this class in generated code.
"""
def __init__(self, pin: int, *, unit: str = "C"):
self.pin = pin
self.unit = unit
def read(self) -> float:
"""Read the current sensor value."""
return 0.0
def calibrate(self, offset: float):
"""Apply a calibration offset."""
self._offset = offset
@classmethod
def __agent_notes__(cls) -> str:
return """
## Do
- Call `calibrate()` once during setup, before `read()`.
- Handle negative values — sensors may report below zero.
## Do not
- Do not call `read()` before `calibrate()` on first use.
"""
class CalibratedSensor(Sensor):
"""
A sensor with factory calibration applied.
Agent usage:
Run ``agent_help(CalibratedSensor)`` before using this class in generated code.
"""
def reset(self):
"""Reset to factory calibration."""
@classmethod
def __agent_notes__(cls) -> str:
return """
## Do
- Call `reset()` if readings drift unexpectedly.
## Do not
- Do not call `calibrate()` — use `reset()` instead. Factory calibration
is pre-applied and `calibrate()` would double-adjust.
"""
if __name__ == "__main__":
print("=== help(CalibratedSensor) — verbose, not agent-friendly ===")
print()
os.environ["PAGER"] = "cat"
help(CalibratedSensor) # noqa: S2201
print()
print("=" * 72)
print()
print("=== agent_help(CalibratedSensor) — notes from both classes ===")
print()
print(agent_help(CalibratedSensor))