From 35e5bec0abad15765d63a3003c33b135125ae608 Mon Sep 17 00:00:00 2001 From: spch-GL Date: Tue, 4 Jun 2024 17:12:22 +0200 Subject: [PATCH] corrections de syntaxe --- comptages/core/report.py | 127 +++++++--------- comptages/core/statistics.py | 198 ++++++++++++------------- comptages/report/yearly_report_bike.py | 89 +++++------ 3 files changed, 192 insertions(+), 222 deletions(-) diff --git a/comptages/core/report.py b/comptages/core/report.py index e80be55..772eeb5 100644 --- a/comptages/core/report.py +++ b/comptages/core/report.py @@ -1,11 +1,9 @@ import os from datetime import date, datetime, timedelta -from typing import Generator +from typing import Generator, Optional +from qgis.core import Qgis, QgsMessageLog -from datetime import timedelta, datetime -from typing import Optional from openpyxl import load_workbook, Workbook -from qgis.core import Qgis, QgsMessageLog from comptages.core import statistics from comptages.datamodel import models @@ -75,7 +73,6 @@ def _prepare_default_reports( ) QgsMessageLog.logMessage(f"{datetime.now()} - Preparing reports: Adding workbook {i+j*mondays_qty} ({output})", "Comptages", Qgis.Info) progress = int(100 / mondays_qty / sections_qty * (i+j*mondays_qty)) - #print(f" j:{j}, i:{i}, progress:{progress}") callback_progress(progress) workbook = load_workbook(filename=template_path) @@ -86,7 +83,7 @@ def _prepare_default_reports( _remove_useless_sheets(count, workbook) workbook.save(filename=output) - + print(f"{datetime.now()}: _prepare_default_reports: ended, count: {count}") @@ -133,6 +130,7 @@ def _prepare_yearly_report( print(f"{datetime.now()}: _prepare_yearly_report: ended, sections_ids: {sections_ids}") + def _mondays_of_count(count: models.Count) -> Generator[date, None, None]: """Generator that return the Mondays of the count""" @@ -243,11 +241,11 @@ def _data_count_yearly( def _data_day( - count: models.Count, - section: models.Section, - monday, - workbook: Workbook - ): + count: models.Count, + section: models.Section, + monday, + workbook: Workbook +): ws = workbook["Data_day"] # Monthly coefficients @@ -358,19 +356,18 @@ def _data_day( def _data_day_yearly( - count: models.Count, - section: models.Section, - year: int, - workbook: Workbook - ): + count: models.Count, + section: models.Section, + year: int, + workbook: Workbook +): ws = workbook["Data_day"] # Total (section) row_offset = 69 col_offset = 2 - df = statistics.get_time_data_yearly( - year, + year, section, exclude_trash=True, ) @@ -388,8 +385,8 @@ def _data_day_yearly( row_offset = 95 col_offset = 2 df = statistics.get_light_numbers_yearly( - section, - start=datetime(year, 1, 1), + section, + start=datetime(year, 1, 1), end=datetime(year + 1, 1, 1), exclude_trash=True, ) @@ -409,10 +406,9 @@ def _data_day_yearly( # Direction 1 row_offset = 5 col_offset = 2 - df = statistics.get_time_data_yearly( - year, - section, + year, + section, direction=1, exclude_trash=True, ) @@ -430,9 +426,9 @@ def _data_day_yearly( row_offset = 31 col_offset = 2 df = statistics.get_light_numbers_yearly( - section, - start=datetime(year, 1, 1), - end=datetime(year + 1, 1, 1), + section, + start=datetime(year, 1, 1), + end=datetime(year + 1, 1, 1), direction=1, exclude_trash=True, ) @@ -453,10 +449,9 @@ def _data_day_yearly( if len(section.lane_set.all()) == 2: row_offset = 37 col_offset = 2 - df = statistics.get_time_data_yearly( - year, - section, + year, + section, direction=2, exclude_trash=True, ) @@ -498,46 +493,43 @@ def _data_month_yearly( end = datetime(year + 1, 1, 1) # Section + row_offset = 14 + col_offset = 2 df = statistics.get_month_data( - section, - start, + section, + start, end, exclude_trash=True, ) - row_offset = 14 - col_offset = 2 - for col in df.itertuples(): ws.cell(row=row_offset, column=col_offset + col.Index, value=col.tm) # Direction 1 + row_offset = 4 + col_offset = 2 df = statistics.get_month_data( - section, - start, - end, + section, + start, + end, direction=1, exclude_trash=True, ) - row_offset = 4 - col_offset = 2 - for col in df.itertuples(): ws.cell(row=row_offset, column=col_offset + col.Index, value=col.tm) # Direction 2 + row_offset = 9 + col_offset = 2 df = statistics.get_month_data( - section, - start, - end, + section, + start, + end, direction=2, exclude_trash=True, ) - row_offset = 9 - col_offset = 2 - for col in df.itertuples(): ws.cell(row=row_offset, column=col_offset + col.Index, value=col.tm) @@ -621,7 +613,6 @@ def _data_speed( speed_high=range_[1], exclude_trash=True, ) - for row in res: ws.cell(row=row_offset + row[0], column=col_offset + i, value=row[1]) @@ -647,12 +638,11 @@ def _data_speed( # Average speed direction 1 row_offset = 5 col_offset = 19 - df = statistics.get_average_speed_by_hour( - count, - section, - direction=1, - start=monday, + count, + section, + direction=1, + start=monday, end=monday + timedelta(days=7), exclude_trash=True, ) @@ -674,7 +664,6 @@ def _data_speed( speed_high=range_[1], exclude_trash=True, ) - for row in res: ws.cell(row=row_offset + row[0], column=col_offset + i, value=row[1]) @@ -702,12 +691,11 @@ def _data_speed( # Average speed direction 2 row_offset = 33 col_offset = 19 - df = statistics.get_average_speed_by_hour( - count, - section, - direction=2, - start=monday, + count, + section, + direction=2, + start=monday, end=monday + timedelta(days=7), exclude_trash=True, ) @@ -770,7 +758,6 @@ def _data_speed_yearly( speed_high=range_[1], exclude_trash=True, ) - for row in res: ws.cell(row=row_offset + row[0], column=col_offset + i, value=row[1]) @@ -780,11 +767,11 @@ def _data_speed_yearly( col_offset = 16 for i, v in enumerate(characteristic_speeds): df = statistics.get_characteristic_speed_by_hour( - None, - section, - direction=1, - start=start, - end=end, + None, + section, + direction=1, + start=start, + end=end, v=v, exclude_trash=True, ) @@ -796,7 +783,6 @@ def _data_speed_yearly( # Average speed direction 1 row_offset = 5 col_offset = 19 - df = statistics.get_average_speed_by_hour( None, section, @@ -833,11 +819,11 @@ def _data_speed_yearly( col_offset = 16 for i, v in enumerate(characteristic_speeds): df = statistics.get_characteristic_speed_by_hour( - None, - section, - direction=2, - start=start, - end=end, + None, + section, + direction=2, + start=start, + end=end, v=v, exclude_trash=True, ) @@ -851,7 +837,6 @@ def _data_speed_yearly( # Average speed direction 2 row_offset = 33 col_offset = 19 - df = statistics.get_average_speed_by_hour( None, section, diff --git a/comptages/core/statistics.py b/comptages/core/statistics.py index 2898f80..f3686f7 100644 --- a/comptages/core/statistics.py +++ b/comptages/core/statistics.py @@ -1,9 +1,9 @@ from typing import Any -from pandas import DataFrame, cut from functools import reduce from datetime import timedelta, datetime -from pytz import timezone +from pandas import DataFrame, cut +from pytz import timezone from django.db.models import F, CharField, Value, Q, Sum, QuerySet from django.db.models.functions import ExtractHour, Trunc, Concat @@ -13,14 +13,14 @@ def get_time_data( - count, - section, - lane=None, - direction=None, - start=None, - end=None, - exclude_trash=False, - ): + count, + section, + lane=None, + direction=None, + start=None, + end=None, + exclude_trash=False, +): if not start: start = count.start_process_date if not end: @@ -61,17 +61,17 @@ def get_time_data( if not df.empty: df["date"] = df["date"].dt.strftime("%a %d.%m.%Y") df["import_status"].replace({0: "Existant", 1: "Nouveau"}, inplace=True) - + return df def get_time_data_yearly( - year, - section: models.Section, - lane=None, - direction=None, - exclude_trash=False, - ) -> DataFrame: + year, + section: models.Section, + lane=None, + direction=None, + exclude_trash=False, +) -> DataFrame: """Vehicles by hour and day of the week""" start = datetime(year, 1, 1) end = datetime(year + 1, 1, 1) @@ -95,8 +95,6 @@ def get_time_data_yearly( if direction is not None: qs = qs.filter(id_lane__direction=direction) - #print("get_time_data_yearly - qs=", qs) - #print("get_time_data_yearly - qs.count=", qs.count()) if not qs.exists(): print(f"statistics.py : get_time_data_yearly - Nothing found for Year: {year}, Section: {section}, Lane: {lane}, Direction: {direction}.") return None @@ -114,8 +112,6 @@ def get_time_data_yearly( print(f"statistics.py : get_time_data_yearly - Nothing found !!! for Year: {year}. Section: {section}. Lane: {lane}. Direction: {direction}. !!!)") print(f"statistics.py : get_time_data_yearly - qsa.query={str(qs.query)}") - #print("get_time_data_yearly - qsa annot=", qs) - #print("get_time_data_yearly - qsa.count=", qs.count()) df = DataFrame.from_records(qs) if not df.empty: @@ -126,15 +122,15 @@ def get_time_data_yearly( def get_day_data( - count: models.Count, - section=None, - lane=None, - direction=None, - status=None, - exclude_trash=False, - start=None, - end=None, - ) -> tuple[DataFrame, int]: + count: models.Count, + section=None, + lane=None, + direction=None, + status=None, + exclude_trash=False, + start=None, + end=None, +) -> tuple[DataFrame, int]: if not start: start = count.start_process_date if not end: @@ -171,7 +167,7 @@ def get_day_data( .annotate(tj=Sum("times")) .values("date", "tj", "import_status") ) - print(f"statistics.py : get_day_data - qs.query=", str(qs.query)) + print("statistics.py : get_day_data - qs.query=", str(qs.query)) df = DataFrame.from_records(qs) mean = 0 @@ -183,12 +179,12 @@ def get_day_data( def get_category_data( - count: models.Count, - section: models.Section, - status=definitions.IMPORT_STATUS_DEFINITIVE, - start=None, - end=None, - ) -> DataFrame: + count: models.Count, + section: models.Section, + status=definitions.IMPORT_STATUS_DEFINITIVE, + start=None, + end=None, +) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -228,12 +224,12 @@ def get_category_data( def get_speed_data( - count: models.Count, - section: models.Section, - exclude_trash=False, - start=None, - end=None, - ) -> DataFrame: + count: models.Count, + section: models.Section, + exclude_trash=False, + start=None, + end=None, +) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -250,7 +246,7 @@ def get_speed_data( if exclude_trash: qs = qs.exclude(id_category__trash=True) - + print(f"statistics.py : get_speed_data - qs.query={str(qs.query)}") df = DataFrame.from_records(qs.values("speed", "times", "import_status")) @@ -292,14 +288,14 @@ def get_speed_data( def get_light_numbers( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - exclude_trash=False, - ) -> dict: + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + exclude_trash=False, +) -> dict: if not start: start = count.start_process_date if not end: @@ -337,13 +333,13 @@ def get_light_numbers( def get_light_numbers_yearly( - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - exclude_trash=False, - ) -> DataFrame: + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + exclude_trash=False, +) -> DataFrame: qs = models.CountDetail.objects.filter( id_lane__id_section=section, id_category__isnull=False, @@ -371,16 +367,16 @@ def get_light_numbers_yearly( def get_speed_data_by_hour( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - speed_low=0, - speed_high=15, - exclude_trash=False, - ) -> "ValuesQuerySet[models.CountDetail, Any]": + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + speed_low=0, + speed_high=15, + exclude_trash=False, +) -> "ValuesQuerySet[models.CountDetail, Any]": if not start: start = count.start_process_date if not end: @@ -420,15 +416,15 @@ def get_speed_data_by_hour( def get_characteristic_speed_by_hour( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - v=0.15, - exclude_trash=False, - ) -> DataFrame: + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + v=0.15, + exclude_trash=False, +) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -469,15 +465,15 @@ def get_characteristic_speed_by_hour( def get_average_speed_by_hour( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - v=0.15, - exclude_trash=False, - ) -> DataFrame: + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + v=0.15, + exclude_trash=False, +) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -519,14 +515,14 @@ def get_average_speed_by_hour( def get_category_data_by_hour( - count: models.Count, - section: models.Section, - category, - lane=None, - direction=None, - start=None, - end=None, - ) -> "ValuesQuerySet[models.CountDetail, Any]": + count: models.Count, + section: models.Section, + category, + lane=None, + direction=None, + start=None, + end=None, +) -> "ValuesQuerySet[models.CountDetail, Any]": if not start: start = count.start_process_date if not end: @@ -572,11 +568,11 @@ def get_special_periods(first_day, last_day) -> QuerySet[models.SpecialPeriod]: def get_month_data( - section: models.Section, - start, end, - direction=None, - exclude_trash=False, - ) -> DataFrame: + section: models.Section, + start, end, + direction=None, + exclude_trash=False, +) -> DataFrame: qs = models.CountDetail.objects.filter( id_lane__id_section=section, timestamp__gte=start, timestamp__lt=end ) @@ -607,7 +603,7 @@ def get_valid_days(year: int, section: models.Section) -> int: where a day is deemed valid just in case there are at least 14 1-hour blocks between 6pm and 4pm with at least 1 vehicle. """ - tz = timezone("Europe/Zurich") + tz = timezone("Europe/Zurich") start = tz.localize(datetime(year, 1, 1)) end = tz.localize(datetime(year + 1, 1, 1)) iterator = ( diff --git a/comptages/report/yearly_report_bike.py b/comptages/report/yearly_report_bike.py index 547a803..ceecd34 100644 --- a/comptages/report/yearly_report_bike.py +++ b/comptages/report/yearly_report_bike.py @@ -5,21 +5,24 @@ from decimal import Decimal from qgis.core import Qgis, QgsMessageLog -from django.db.models import Sum, F, Avg #, Count, ExpressionWrapper -#from django.db.models.fields import DateField, IntegerField +from openpyxl import load_workbook +from django.db.models import Sum, F, Avg from django.db.models.functions import ( -# Cast, ExtractHour, ExtractIsoWeekDay, ExtractMonth, TruncDate, ) -from openpyxl import load_workbook - -from comptages.core import definitions, utils #, statistics -from comptages.datamodel.models import CountDetail, Section, Lane, ClassCategory, Category -from comptages.datamodel.models import Count as modelCount +from comptages.core import definitions, utils +from comptages.datamodel.models import ( + CountDetail, + Section, + Lane, + ClassCategory, + Category, + Count as modelCount, +) class YearlyReportBike: @@ -74,8 +77,7 @@ def tjms_by_weekday_category(self, direction=None) -> "ValuesQuerySet[CountDetai ) print(f"yearly_report_bike.py: tjms_by_weekday_category - results.query:{str(results.query)}") return results - - + def tjms_by_weekday_hour(self) -> "ValuesQuerySet[CountDetail, dict[str, Any]]": # Get all the count details for section and the year # Doesn't produce correct results because of 2 aggregation @@ -171,7 +173,7 @@ def reducer(acc: dict, val: dict) -> dict: if hour not in acc[day]: acc[day][hour] = {} - + acc[day][hour] = val["runs"] return acc @@ -229,7 +231,6 @@ def tjms_by_weekday_and_month( for item in results: if item["month"] not in builder: builder[item["month"]] = {"month": item["month"]} - #print(f"yearly_report_bike.py : tjms_by_weekday_and_month - builder_month:{builder}") builder[item["month"]][item["week_day"]] = { "days": 1, "runs": item["daily_runs"], @@ -250,7 +251,7 @@ def tjms_by_weekday_and_month( builder[item["month"]][item["week_day"]]["runs"] / builder[item["month"]][item["week_day"]]["days"] ) - + return builder def runs_by_weekday_and_month(self) -> "ValuesQuerySet[CountDetail, dict[str, Any]]": @@ -263,14 +264,14 @@ def runs_by_weekday_and_month(self) -> "ValuesQuerySet[CountDetail, dict[str, An # Group by month, week_day results = ( - qs.annotate(month=ExtractMonth("timestamp"), - week_day=ExtractIsoWeekDay("timestamp")) + qs.annotate(month=ExtractMonth("timestamp"), + week_day=ExtractIsoWeekDay("timestamp")) .values("month", "week_day") .annotate(daily_runs=Sum("times")) .values("month", "week_day", "daily_runs") ) print(f"yearly_report_bike.py : runs_by_weekday_and_month - results.query:{str(results.query)}") - + return results def nb_weekday_by_month(self) -> "ValuesQuerySet[CountDetail, dict[str, Any]]": @@ -287,14 +288,13 @@ def nb_weekday_by_month(self) -> "ValuesQuerySet[CountDetail, dict[str, Any]]": .values("date") .annotate(daily_runs=Sum("times")) .values("date", "daily_runs") - .annotate(month=ExtractMonth("timestamp"), - week_day=ExtractIsoWeekDay("timestamp") - ) + .annotate(month=ExtractMonth("timestamp"), + week_day=ExtractIsoWeekDay("timestamp")) .values("date", "month", "week_day") -# .order_by("date") + # .order_by("date") ) print(f"yearly_report_bike.py : nb_weekday_by_month - results.query:{str(results.query)}") - + def reducer(acc: dict, item) -> dict: if item["month"] not in acc: @@ -324,10 +324,9 @@ def total_runs_by_day(self) -> "ValuesQuerySet[CountDetail, dict[str, Any]]": .values("date") .annotate(daily_runs=Sum("times")) .values("date", "daily_runs") -# .order_by("date") + # .order_by("date") ) print(f"yearly_report_bike.py : total_runs_by_day - results.query:{str(results.query)}") - #print(f"yearly_report_bike.py : total_runs_by_day - results:{results}") return results @@ -346,7 +345,7 @@ def tjms_total_runs_by_day_of_week(self) -> dict[str, Any]: .order_by("week_day") ) print(f"yearly_report_bike.py : tjms_total_runs_by_day_of_week - results.query:{str(results.query)}") - #print(f"yearly_report_bike.py : tjms_total_runs_by_day_of_week - results.query:{results}") + # FIXME # Aggregation via `values()` into `annotate()` all the way to the end result would be more performant. builder = {} @@ -365,7 +364,7 @@ def tjms_total_runs_by_day_of_week(self) -> dict[str, Any]: builder[item["week_day"]]["runs"] / builder[item["week_day"]]["days"] ) - + return builder def total_runs_by_class(self) -> dict[str, Any]: @@ -526,7 +525,7 @@ def count_details_by_various_criteria( # Base QuerySet base_qs = CountDetail.objects.filter( - id_count=count.id, + id_count=count.id, id_category__in=categories_ids, timestamp__year=self.year, ) @@ -549,7 +548,7 @@ def count_details_by_various_criteria( .order_by("-value") ) print(f"yearly_report_bike.py : count_details_by_various_criteria - busy_date.query:{str(busy_date.query)}") - + busiest_date = busy_date.first() least_busy_date = busy_date.last() assert busiest_date @@ -564,7 +563,7 @@ def count_details_by_various_criteria( .annotate(value=Sum("times")) ) print(f"yearly_report_bike.py : count_details_by_various_criteria - busiest_date_row.query:{str(busiest_date_row.query)}") - + least_busy_date_row = ( base_qs.annotate( date=TruncDate("timestamp"), category_name=F("id_category__name") @@ -616,7 +615,7 @@ def count_details_by_various_criteria( .annotate(value=Sum("times")) .order_by("-value") ) - + total_runs_busiest_hour_weekday = busiest_hour.exclude(week_day__gt=5) total_runs_busiest_hour_weekend = busiest_hour.exclude(week_day__lt=6) print(f"yearly_report_bike.py : count_details_by_various_criteria - busiest_weekend_hour.query:{str(total_runs_busiest_hour_weekend.query)}") @@ -661,7 +660,7 @@ def count_details_by_season(self, count_id) -> dict[int, Any]: # Getting data count_details = ( CountDetail.objects.filter( - id_count=count_id, + id_count=count_id, id_category__in=categories_ids, timestamp__year=self.year, ) @@ -681,7 +680,7 @@ def reducer(acc: dict, detail) -> dict: month20 = (date - timedelta(days=20)).month for season, _range in self.seasons.items(): - if month20 in _range : + if month20 in _range: category_name = detail["category_name"] if season not in acc: @@ -789,10 +788,8 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: section_start_dist = render_section_dist(section.start_dist) section_end_dist = render_section_dist(section.end_dist) - ws[ - "B3" - ] = f""" - Poste de comptage : {section.id} + ws["B3"] = f""" + Poste de comptage : {section.id} Axe : {section.owner}:{section.road}{section.way} PR {section.start_pr} + {section_start_dist} m à PR {section.end_pr} + {section_end_dist} m """ @@ -808,8 +805,8 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: ) if not count_detail.exists(): QgsMessageLog.logMessage( - f"{datetime.now()} - Aucun conmptage pour cette année ({self.year}) et cette section ({self.section_id})", - "Comptages", + f"{datetime.now()} - Aucun conmptage pour cette année ({self.year}) et cette section ({self.section_id})", + "Comptages", Qgis.Info ) return @@ -864,8 +861,6 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: print_area = ws["C5:D28"] data = self.total_runs_by_hour_and_direction(directions=(1, 2)) for hour, row in enumerate(print_area, 0): - # if hour == 24: - # hour = 0 for direction, cell in enumerate(row, 1): if hour not in data or direction not in data[hour]: cell.value = 0 @@ -876,8 +871,6 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: print_area = ws["C37:D60"] data = self.total_runs_by_hour_and_direction(directions=(1, 2), weekdays=(6, 7)) for hour, row in enumerate(print_area, 0): - # if hour == 24: - # hour = 0 for direction, cell in enumerate(row, 1): if hour not in data or direction not in data[hour]: cell.value = 0 @@ -888,8 +881,6 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: print_area = ws["B69:H92"] data = self.total_runs_by_hour_one_direction(1) for hour, row in enumerate(print_area, 0): - # if hour == 24: - # hour = 0 for day, cell in enumerate(row, 1): if day not in data or hour not in data[day]: cell.value = 0 @@ -900,8 +891,6 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: print_area = ws["B101:H124"] data = self.total_runs_by_hour_one_direction(2) for hour, row in enumerate(print_area, 0): - # if hour == 24: - # hour = 0 for day, cell in enumerate(row, 1): if day not in data or hour not in data[day]: cell.value = 0 @@ -949,7 +938,7 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: for i, season in enumerate(self.seasons): if saison == season: for j, cat in enumerate(column_names): - if cat in data[saison] : + if cat in data[saison]: ws.cell( column=j + column_offset, row=i + row_offset, @@ -1010,7 +999,7 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: for row in res: row_num = row_offset + category.code col_num = col_offset + row[0] - val=ws.cell(row_num, col_num).value + val = ws.cell(row_num, col_num).value value = ( val + row[1] if isinstance(val, (int, float)) else row[1] ) # Add to previous value because with class convertions multiple categories can converge into a single one @@ -1034,7 +1023,7 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: for row in res: row_num = row_offset + category.code col_num = col_offset + row[0] - val=ws.cell(row_num, col_num).value + val = ws.cell(row_num, col_num).value value = ( val + row[1] if isinstance(val, (int, float)) else row[1] ) # Add to previous value because with class convertions multiple categories can converge into a single one @@ -1058,7 +1047,7 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: for row in res: row_num = row_offset + category.code col_num = col_offset + row[0] - val=ws.cell(row_num, col_num).value + val = ws.cell(row_num, col_num).value value = ( val + row[1] if isinstance(val, (int, float)) else row[1] ) # Add to previous value because with class convertions multiple categories can converge into a single one @@ -1069,6 +1058,6 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str: output = path.join( self.path_to_output_dir, "{}_{}_r.xlsx".format(self.section_id, self.year) ) - + workbook.save(filename=output) print(f"{datetime.now()}: YRB_run - end: Saved report to {output}")