A lab's control software shows each tank's temperature in three units, and operators may type a new value in any of them. An earlier version stored all three numbers and they drifted apart whenever one was updated; it also happily accepted -500 °C.
Write a class Temperature(celsius=0.0) with these attributes, all used without brackets, like plain attributes:
celsius,fahrenheitandkelvincan be read and assigned. Assigning any of them changes the temperature, and the other two follow (F = C * 9 / 5 + 32,K = C + 273.15).- A temperature below absolute zero (
0 K, which is-273.15 °C) raisesValueError, both in the constructor and on assignment, and leaves the temperature unchanged. stateis"ice"below0 °C,"water"from0up to (not including)100 °C, and"steam"from100 °C. It can only be read: assigning to it raisesAttributeError.
The tests use the helper session(ops, start) from the setup: it creates Temperature(start) and runs a list of steps, each ("set", name, value) (an assignment) or ("get", name) (a read), and returns one result per step (None for a successful assignment, the exception's name when one is raised).
Examples
Input: session([("get", "fahrenheit"), ("set", "kelvin", 373.15), ("get", "celsius"),
("get", "state"), ("set", "celsius", -300), ("get", "kelvin")], 20)
Output: [68.0, None, 100.0, 'steam', 'ValueError', 373.15]
Input: session([("set", "state", "ice"), ("get", "state")], -5)
Output: ['AttributeError', 'ice']
Constraints
- Values are numbers; floats are compared with a tolerance of
1e-6. - Exactly
0 Kis allowed.
Goals
- Store one value and compute the others on demand, so they can never go stale
- Validate new values in a property setter
- Make a derived value read-only