Mercurial > personal > weatherlog
view weatherlog/types.py @ 14:c01f9929ae38
Make logger and HTTP writer more general and resilient.
This makes the logger and HTTP writer more general, by removing
any dependency upon the exact data type they are writing.
They can now handle any type of BSON-serializable dict,
and track what they have sent by keeping track of the last *byte*,
not the last timestamp.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Tue, 15 Oct 2019 22:40:24 -0400 |
parents | 885bff085edf |
children | b5625b531d2d |
line wrap: on
line source
"""Basic datatypes for the weather station. """ import datetime import attr import pytz def _utc_now() -> datetime.datetime: """utcnow, but timezone-aware.""" return datetime.datetime.now(tz=pytz.UTC) @attr.s(frozen=True, slots=True) class Reading(object): """A single reading from a temperature/humidity sensor.""" # The timestamp of the reading. sample_time = attr.ib(type=datetime.datetime) # The temperature, in degrees Celsius. temp_c = attr.ib(type=float) # The relative humidity, in percent. rh_pct = attr.ib(type=float) @classmethod def from_now(cls, **kwargs) -> 'Reading': """Creates a new reading taken at the current time.""" return cls(sample_time=_utc_now(), **kwargs) def as_dict(self): return attr.asdict(self, recurse=False)