Skip to content

Commit

Permalink
fix dict creation and simplify iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
elazarg committed Nov 24, 2022
1 parent 4763404 commit f3afb81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from anyway.request_params import RequestParams
from anyway.widgets.suburban_widgets.killed_and_injured_count_per_age_group_widget_utils import (
KilledAndInjuredCountPerAgeGroupWidgetUtils,
AGE_RANGES,
AGE_RANGES, make_group_name,
)
from anyway.widgets.suburban_widgets import killed_and_injured_count_per_age_group_widget_utils

Expand All @@ -32,7 +32,7 @@ def generate_items(self) -> None:
)

partial_processed = add_empty_keys_to_gen_two_level_dict(
raw_data, self.get_age_range_list(), InjurySeverity.codes()
raw_data, [make_group_name(group) for group in AGE_RANGES], InjurySeverity.codes()
)

structured_data_list = []
Expand All @@ -53,8 +53,3 @@ def localize_items(request_params: RequestParams, items: Dict) -> Dict:
"labels_map": gen_entity_labels(InjurySeverity),
}
return items

@staticmethod
def get_age_range_list() -> List[str]:
return [f"{item_min_range}-{item_max_range}" if item_max_range < 120 else f"{item_min_range}+"
for item_min_range, item_max_range in zip(AGE_RANGES, AGE_RANGES[1:])]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import Counter, OrderedDict
from datetime import datetime
from typing import Dict, Optional
from typing import Dict, Optional, Tuple

from flask_sqlalchemy import BaseQuery
from flask_babel import _
Expand All @@ -15,10 +15,17 @@
# RequestParams is not hashable, so we can't use functools.lru_cache
cache_dict = OrderedDict()

UNKNOWN = _("Unknown")
CACHE_MAX_SIZE = 10

AGE_RANGES = [0, 5, 15, 20, 25, 45, 65, 200]
_AGES = [0, 5, 15, 20, 25, 45, 65, 200]
AGE_RANGES = list(zip(_AGES, _AGES[1:]))


def make_group_name(pair: Optional[Tuple[int, int]]) -> str:
if pair is None:
return _("Unknown")
start, end = pair
return f"{start}-{end}" if end < 120 else f"{start}+"


class KilledAndInjuredCountPerAgeGroupWidgetUtils:
Expand Down Expand Up @@ -51,24 +58,24 @@ def filter_and_group_injured_count_per_age_group(

@staticmethod
def parse_query_data(query: BaseQuery) -> Optional[Dict[str, Counter]]:
dict_grouped = {k: Counter() for k in AGE_RANGES + [UNKNOWN]}
dict_grouped = {make_group_name(age): Counter() for age in [*AGE_RANGES, None]}
has_data = False

for row in query:
group_name: str = UNKNOWN
group = None

age_parsed = parse_age_from_range(row.age_group)
if age_parsed:
if age_parsed is not None:
min_age, max_age = age_parsed
# The age groups in the DB are not the same age groups in the widget, so we need to merge some groups.
# Find the right bucket to aggregate the data:
for item_min_range, item_max_range in zip(AGE_RANGES, AGE_RANGES[1:]):
for item_min_range, item_max_range in AGE_RANGES:
if item_min_range <= min_age <= max_age <= item_max_range:
has_data = True
group_name = f"{item_min_range}-{item_max_range}" if item_max_range < 120 else f"{item_min_range}+"
group = (item_min_range, item_max_range)
break

dict_grouped[group_name][row.injury_severity] += row.count
dict_grouped[make_group_name(group)][row.injury_severity] += row.count
if not has_data:
return None
return dict_grouped
Expand Down

0 comments on commit f3afb81

Please sign in to comment.