Fix NPE in geocache.py, Add save on fail
This commit is contained in:
parent
5792bc12b8
commit
5089eb4b10
@ -22,29 +22,35 @@ class Geocache:
|
|||||||
if os.path.isfile(self.JSON_FILE):
|
if os.path.isfile(self.JSON_FILE):
|
||||||
with open(self.JSON_FILE, 'r') as rf:
|
with open(self.JSON_FILE, 'r') as rf:
|
||||||
self.__geo_cache.update(json.load(rf))
|
self.__geo_cache.update(json.load(rf))
|
||||||
|
print(f'Geocache loaded from {self.JSON_FILE}')
|
||||||
|
|
||||||
def __save_geo_cache(self) -> None:
|
def __save_geo_cache(self) -> None:
|
||||||
with open(self.JSON_FILE, 'w') as wf:
|
with open(self.JSON_FILE, 'w') as wf:
|
||||||
json.dump(self.__geo_cache, wf)
|
json.dump(self.__geo_cache, wf)
|
||||||
print('Geocache saved')
|
print(f'Geocache saved to {self.JSON_FILE}')
|
||||||
|
|
||||||
def update_geo_cache(self, cities: List[str]) -> None:
|
def update_geo_cache(self, cities: List[str]) -> None:
|
||||||
is_changed: bool = False
|
is_changed: bool = False
|
||||||
for city in cities:
|
try:
|
||||||
if Utils.is_empty_str(city):
|
for city in cities:
|
||||||
continue
|
if Utils.is_empty_str(city):
|
||||||
result: () = self.__geo_cache.get(city)
|
continue
|
||||||
if result is not None:
|
result: () = self.__geo_cache.get(city)
|
||||||
continue
|
if result is not None:
|
||||||
print(f'{len(self.__geo_cache.keys())}/{len(cities)} - Try to load geocode for {city}')
|
continue
|
||||||
location: Point = self.__geocode(city)
|
print(f'{len(self.__geo_cache.keys())} - Try to load geocode for {city}')
|
||||||
result: () = (location.latitude, location.longitude)
|
location: Point = self.__geocode(city)
|
||||||
self.__geo_cache[city] = result
|
if location is None:
|
||||||
is_changed = True
|
self.__geo_cache[city] = ''
|
||||||
if len(self.__geo_cache.keys()) % 50 == 0:
|
else:
|
||||||
|
result: [] = [location.latitude, location.longitude]
|
||||||
|
self.__geo_cache[city] = result
|
||||||
|
is_changed = True
|
||||||
|
if len(self.__geo_cache.keys()) % 50 == 0:
|
||||||
|
self.__save_geo_cache()
|
||||||
|
finally:
|
||||||
|
if is_changed:
|
||||||
self.__save_geo_cache()
|
self.__save_geo_cache()
|
||||||
if is_changed:
|
|
||||||
self.__save_geo_cache()
|
|
||||||
|
|
||||||
def get_location(self, city: str) -> ():
|
def get_location(self, city: str) -> ():
|
||||||
return self.__geo_cache.get(city)
|
return self.__geo_cache.get(city)
|
||||||
|
Loading…
Reference in New Issue
Block a user