Workshop 8 moves from plain data structures into object-oriented design. This homework asks you to invent fresh scenarios instead of remixing class examples. Focus on encapsulating behaviour inside classes, keeping state consistent, and composing multiple objects together. Keep the solutions in a single script (for example, homework_8.py) so you can run everything end to end.
- Create a
WeatherSnapshotclass withtemperature,humidity, andobserved_at(adatetime.datetime) attributes. - Implement
__repr__so each instance prints as a compact ISO-style summary (for example,WeatherSnapshot(2025-02-14T08:00, 18°C, 62%)). - Add a method
cool_down(delta=1)that returns a newWeatherSnapshotwith a reduced temperature but identical humidity and timestamp, leaving the original untouched. - Generate four distinct snapshots and iterate over them printing the original and cooled versions to highlight immutability.
- Design a
SmartIrrigatorclass that tracksname,water_reserve, anddaily_limit. - Provide methods
refill(amount),sprinkle(volume), andstatus(); ensuresprinklerejects overuse viaValueErrorand always deducts from the reserve. - Add a computed property
needs_refillthat becomesTruewhen the reserve drops below 20% of capacity. - Demonstrate two devices with different limits, run a short watering routine, and print their status along with whether they need a refill.
- Build a
Greenhouseclass that stores multipleSmartIrrigatorinstances in a dictionary keyed by device name. - Provide
register_device(device)that stores and returns the device,get_device(name)that retrieves a device orNone, andtotal_water_remaining()that sums reserves across all devices. - Implement
busiest_device()returning the irrigator that sprinkled the most cumulative volume (track per-device usage insideSmartIrrigator). ReturnNonewhen there are no devices. - Simulate a day's schedule: register at least three devices, perform varied watering operations, then report the total remaining water and the name of the busiest device.
- Write a helper
log_action(message, *, timestamp=None)that defaults todatetime.datetime.now()and prints a line like2025-01-14T21:15:00 — Sprinkled 3L from HerbGarden. - Integrate the logger into at least two methods from Tasks 2 or 3 so significant actions generate entries (for example, refills and sprinkling events).
- Store each log line in a list in addition to printing it, then display the complete log at the end of your script to confirm ordering.
- Add a
SmartMistersubclass that introduces humidity boosts and customisesstatus(). - Write a small test section that asserts
Greenhouse.total_water_remaining()matches the sum of each stored device after several operations.