tz -> zone, update iter_tzs depends on, misc changes

This commit is contained in:
Sean Breckenridge 2022-04-26 12:14:37 -07:00
parent 8ab67d4d26
commit 62dd6712f3
5 changed files with 22 additions and 6 deletions

View file

@ -160,7 +160,7 @@ For an extensive/complex example, you can check out ~@seanbreckenridge~'s [[http
- you might trust your original timezone, or it might just be UTC, and you want to use something more reasonable - you might trust your original timezone, or it might just be UTC, and you want to use something more reasonable
#+begin_src python #+begin_src python
Policy = Literal[ TzPolicy = Literal[
'keep' , # if datetime is tz aware, just preserve it 'keep' , # if datetime is tz aware, just preserve it
'convert', # if datetime is tz aware, convert to provider's tz 'convert', # if datetime is tz aware, convert to provider's tz
'throw' , # if datetime is tz aware, throw exception 'throw' , # if datetime is tz aware, throw exception

View file

@ -11,11 +11,12 @@ REQUIRES = ["git+https://github.com/seanbreckenridge/ipgeocache"]
from typing import Iterator from typing import Iterator
from my.core.common import Stats from my.core.common import Stats, warn_if_empty
from .common import IP from .common import IP
@warn_if_empty
def ips() -> Iterator[IP]: def ips() -> Iterator[IP]:
yield from () yield from ()

View file

@ -24,7 +24,7 @@ class IP(NamedTuple):
return ipgeocache.get(self.addr) return ipgeocache.get(self.addr)
@property @property
def tz(self) -> str: def tzname(self) -> str:
tz: str = self.ipgeocache()["timezone"] tz: str = self.ipgeocache()["timezone"]
return tz return tz

View file

@ -10,8 +10,8 @@ LatLon = Tuple[float, float]
# TODO: add timezone to this? can use timezonefinder in tz provider instead though # TODO: add timezone to this? can use timezonefinder in tz provider instead though
class Location(NamedTuple): class Location(NamedTuple):
lon: float
lat: float lat: float
lon: float
dt: datetime dt: datetime
accuracy: Optional[float] accuracy: Optional[float]
elevation: Optional[float] elevation: Optional[float]

View file

@ -106,8 +106,23 @@ def most_common(lst: List[DayWithZone]) -> DayWithZone:
return res return res
# refresh _iter_tzs once per day -- don't think a better depends_on is possible dynamically def _iter_tz_depends_on() -> str:
@mcachew(logger=logger, depends_on=lambda: str(date.today())) """
Since you might get new data which specifies a new timezone sometime
in the day, this causes _iter_tzs to refresh every 6 hours, like:
2022-04-26_00
2022-04-26_06
2022-04-26_12
2022-04-26_18
"""
day = str(date.today())
hr = datetime.now().hour
hr_truncated = hr // 6 * 6
return "{}_{}".format(day, hr_truncated)
# refresh _iter_tzs every 6 hours -- don't think a better depends_on is possible dynamically
@mcachew(logger=logger, depends_on=_iter_tz_depends_on)
def _iter_tzs() -> Iterator[DayWithZone]: def _iter_tzs() -> Iterator[DayWithZone]:
# since we have no control over what order the locations are returned, # since we have no control over what order the locations are returned,
# we need to sort them first before we can do a groupby # we need to sort them first before we can do a groupby