From 7d5aa34a6e2378d82199f26652b8f5d5682b4cfd Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Wed, 28 Dec 2022 16:16:31 -0800 Subject: [PATCH] Add code --- pota_search.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 2 files changed, 93 insertions(+) create mode 100644 pota_search.py create mode 100644 requirements.txt diff --git a/pota_search.py b/pota_search.py new file mode 100644 index 0000000..d517dc3 --- /dev/null +++ b/pota_search.py @@ -0,0 +1,91 @@ +import csv +import sys +import dataclasses +import thefuzz.fuzz, thefuzz.process +import typing +import itertools + + +@dataclasses.dataclass +class Park: + reference: str + name: str + location: typing.List[str] + latitude: float + longitude: float + grid: str + attempts: int + activations: int + qsos: int + + +@dataclasses.dataclass +class ParkDatabase: + parks: typing.List[Park] + + def __post_init__(self): + self.parks_dict = {park.reference: park for park in self.parks} + + def search_name(self, string): + return [ + self.parks_dict[i[0].split("|")[0]] + for i in thefuzz.process.extract( + string, + [park.reference + "|" + park.name for park in self.parks], + scorer=thefuzz.fuzz.partial_ratio, + ) + ] + + def filter_location(self, location): + location = location.upper() + return ParkDatabase( + [ + park + for park in self.parks + if (location in park.location) + or ( + location + in [ + park_location.split("-")[0] + for park_location in park.location + ] + ) + ] + ) + + def get_locations(self): + return _uniq(itertools.chain(*[park.location for park in self.parks])) + + def get_expanded_locations(self): + return _uniq(_expand_locations(self.get_locations())) + + +def _uniq(data): + return sorted(set(data)) + + +def _expand_location(code): + return code, code.split("-")[0] + + +def _expand_locations(codes): + return itertools.chain(*[_expand_location(code) for code in codes]) + + +def load_csv(f): + return ParkDatabase( + [ + Park( + park["reference"], + park["name"], + park["locationDesc"].split(","), + float(park["latitude"]), + float(park["longitude"]), + park["grid"], + int(park["attempts"]), + int(park["activations"]), + int(park["qsos"]), + ) + for park in csv.DictReader(f) + ] + ) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..045fcf3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +thefuzz[speedup] +blessed