add via_ip.estimate_location using binary search
This commit is contained in:
parent
7aed18042d
commit
0a48393589
5 changed files with 84 additions and 24 deletions
|
@ -4,7 +4,12 @@
|
|||
from typing import Iterator
|
||||
|
||||
from my.core.source import import_source
|
||||
from my.location.fallback.common import estimate_from, FallbackLocation, DateExact
|
||||
from my.location.fallback.common import (
|
||||
estimate_from,
|
||||
FallbackLocation,
|
||||
DateExact,
|
||||
LocationEstimator,
|
||||
)
|
||||
|
||||
|
||||
# can comment/uncomment sources here to enable/disable them
|
||||
|
@ -12,8 +17,15 @@ def fallback_locations() -> Iterator[FallbackLocation]:
|
|||
yield from _ip_fallback_locations()
|
||||
|
||||
|
||||
def fallback_estimators() -> Iterator[LocationEstimator]:
|
||||
# can comment/uncomment estimators here to enable/disable them
|
||||
# the order of the estimators determines priority if location accuries are equal/unavailable
|
||||
yield _ip_estimate
|
||||
yield _home_estimate
|
||||
|
||||
|
||||
def estimate_location(dt: DateExact) -> FallbackLocation:
|
||||
loc = estimate_from(dt, estimators=(_home_estimate,))
|
||||
loc = estimate_from(dt, estimators=list(fallback_estimators()))
|
||||
if loc is None:
|
||||
raise ValueError("Could not estimate location")
|
||||
return loc
|
||||
|
@ -21,9 +33,16 @@ def estimate_location(dt: DateExact) -> FallbackLocation:
|
|||
|
||||
@import_source(module_name="my.location.fallback.via_home")
|
||||
def _home_estimate(dt: DateExact) -> Iterator[FallbackLocation]:
|
||||
from my.location.fallback.via_home import estimate_location as via_home
|
||||
from my.location.fallback.via_home import estimate_location as via_home_estimate
|
||||
|
||||
yield from via_home(dt)
|
||||
yield from via_home_estimate(dt)
|
||||
|
||||
|
||||
@import_source(module_name="my.location.fallback.via_ip")
|
||||
def _ip_estimate(dt: DateExact) -> Iterator[FallbackLocation]:
|
||||
from my.location.fallback.via_ip import estimate_location as via_ip_estimate
|
||||
|
||||
yield from via_ip_estimate(dt)
|
||||
|
||||
|
||||
@import_source(module_name="my.location.fallback.via_ip")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue