comparison weather_server/types.py @ 21:beb42c835c52

Make weather server handle arbitrary data: - Make logfile record arbitrary BSONs - Make server handlers OK with same - Make location type a normal class rather than attrs; have it handle its own logger. - Bump version number.
author Paul Fisher <paul@pfish.zone>
date Sat, 19 Oct 2019 18:40:48 -0400
parents 52ef21607b31
children b77c8e7d2742
comparison
equal deleted inserted replaced
20:a7fe635d1c88 21:beb42c835c52
4 import math 4 import math
5 import typing as t 5 import typing as t
6 6
7 import attr 7 import attr
8 8
9 from . import common 9
10 T = t.TypeVar('T')
10 11
11 12
12 def c_to_f(c: float) -> float: 13 def c_to_f(c: float) -> float:
13 return c * 9 / 5 + 32 14 return c * 9 / 5 + 32
14 15
49 50
50 @property 51 @property
51 def dew_point_f(self) -> float: 52 def dew_point_f(self) -> float:
52 return c_to_f(self.dew_point_c) 53 return c_to_f(self.dew_point_c)
53 54
54 def as_dict(self) -> t.Dict[str, t.Any]:
55 return attr.asdict(self, recurse=False)
56
57 @classmethod 55 @classmethod
58 def from_now(cls, **kwargs) -> 'Reading': 56 def from_dict(cls: t.Type[T], d: t.Dict[str, t.Any]) -> T:
59 return cls(ingest_time=common.utc_now(), **kwargs) 57 return cls(**{f.name: d[f.name] for f in attr.fields(cls)})
60 58
61 @property 59 @property
62 def _gamma(self) -> float: 60 def _gamma(self) -> float:
63 return ( 61 return (
64 math.log(self.rh_pct / 100) + 62 math.log(self.rh_pct / 100) +