Skip to content

Commit

Permalink
refactor: type hints for lbsn bases (use aliases)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sieboldianus committed Jan 20, 2020
1 parent 40634b0 commit 4ad00d1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
3 changes: 2 additions & 1 deletion lbsntransform/output/hll/hll_bases.py
Expand Up @@ -61,6 +61,7 @@ class structures defined here, thus it is important that order of keys,
import inspect
import sys
from collections import OrderedDict, namedtuple
from typing import List

from lbsnstructure import lbsnstructure_pb2 as lbsn
from lbsntransform.tools.helper_functions import HelperFunctions as HF
Expand Down Expand Up @@ -232,7 +233,7 @@ def get_metric_keys(self):
"""
return list(self.metrics.keys())

def get_sql_header(self) -> str:
def get_sql_header(self) -> List[str]:
"""Get joined header for hll upsert sql
Concat column names for key, attrs and metrics, e.g.:
latitude, longitude, latlng_geom, user_hll, post_hll, date_hll, utl_hll
Expand Down
4 changes: 2 additions & 2 deletions lbsntransform/output/hll/hll_functions.py
Expand Up @@ -5,7 +5,7 @@
"""

import datetime as dt
from typing import Dict, Generator, List, Set, Tuple, Union
from typing import Dict, Generator, List, Set, Tuple, Union, Optional

from lbsnstructure import lbsnstructure_pb2 as lbsn
from lbsntransform.tools.helper_functions import HelperFunctions as HF
Expand Down Expand Up @@ -198,7 +198,7 @@ def hll_concat(record_attr: List[Union[str, int]]) -> str:
return ":".join(map(str, record_attr))

@staticmethod
def merge_dates_post(record: lbsn.Post = None) -> dt:
def merge_dates_post(record: lbsn.Post = None) -> Optional[dt.datetime]:
"""Merge post_publish and post_created attributes"""
post_create_date = HF.null_check_datetime(
record.post_create_date)
Expand Down
32 changes: 12 additions & 20 deletions lbsntransform/output/hll/shared_structure_proto_hlldb.py
Expand Up @@ -14,6 +14,14 @@

from .hll_functions import HLLFunctions as HLF

HllBases = Union[
spatial.LatLngBase, spatial.PlaceBase,
spatial.CityBase, spatial.CountryBase,
temporal.DateBase, temporal.MonthBase,
temporal.YearBase, topical.TermBase,
topical.EmojiBase, topical.TermLatLngBase,
topical.EmojiLatLngBase]


class ProtoHLLMapping():
""" Methods to map ProtoBuf structure to PG HLL SQL structure."""
Expand All @@ -25,16 +33,8 @@ def __init__(self, include_lbsn_bases: List[str] = None):

@staticmethod
def update_hll_dicts(
batched_hll_bases: Dict[Tuple[str, str], Dict[str, Union[
spatial.LatLngBase, spatial.PlaceBase,
spatial.CityBase, spatial.CountryBase,
temporal.DateBase, temporal.MonthBase,
temporal.YearBase, topical.TermBase]]],
hll_base_metrics: List[Union[
spatial.LatLngBase, spatial.PlaceBase,
spatial.CityBase, spatial.CountryBase,
temporal.DateBase, temporal.MonthBase,
temporal.YearBase, topical.TermBase]]):
batched_hll_bases: Dict[Tuple[str, str], Dict[str, HllBases]],
hll_base_metrics: List[HllBases]):
"""Update batched hll bases with new list of bases, merged metrics
batched_hll_bases:
Expand Down Expand Up @@ -77,16 +77,8 @@ def extract_hll_base_metrics(self, record, record_type) -> Optional[

@staticmethod
def update_bases_metrics(
bases: List[Union[
spatial.LatLngBase, spatial.PlaceBase,
spatial.CityBase, spatial.CountryBase,
temporal.DateBase, temporal.MonthBase,
temporal.YearBase, topical.TermBase]],
metrics: hll.HllMetrics) -> Iterator[Union[
spatial.LatLngBase, spatial.PlaceBase,
spatial.CityBase, spatial.CountryBase,
temporal.DateBase, temporal.MonthBase,
temporal.YearBase, topical.TermBase]]:
bases: List[HllBases],
metrics: hll.HllMetrics) -> Iterator[HllBases]:
"""Adds/updates metrics to hll_bases"""
# iterate Namedtuple field names and values
for key, value in zip(metrics._fields, metrics):
Expand Down

0 comments on commit 4ad00d1

Please sign in to comment.