aboutsummaryrefslogtreecommitdiffstats
path: root/hommaexceli_py/data_parser.py
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-02-14 17:01:19 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-02-14 17:01:19 +0200
commit54978eb6b515afd1da68642de07e482b09acf9f3 (patch)
treeec310c4a2135be709a7f647e8df9073b5898cd1e /hommaexceli_py/data_parser.py
parentfbdf6c694b5a92a663eac78230a85a47964a84a1 (diff)
Update last done in Sheets
Diffstat (limited to 'hommaexceli_py/data_parser.py')
-rw-r--r--hommaexceli_py/data_parser.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/hommaexceli_py/data_parser.py b/hommaexceli_py/data_parser.py
new file mode 100644
index 0000000..e8ca33b
--- /dev/null
+++ b/hommaexceli_py/data_parser.py
@@ -0,0 +1,63 @@
+from dataclasses import dataclass
+import datetime
+import re
+from dataclass_defs import ProcessedRow, SheetsRow
+
+INTERVAL_ABBREVS = {
+ "pv": datetime.timedelta(days=1),
+ "vk": datetime.timedelta(days=7),
+ "kk": datetime.timedelta(days=30),
+ "v": datetime.timedelta(days=365),
+}
+
+
+def _calculate_interval(interval_str: str):
+ match = re.search(r"(\d+)(\w+)", interval_str)
+ number = int(match[1])
+ abbrev = match[2]
+
+ try:
+ interval = INTERVAL_ABBREVS[abbrev]
+ except KeyError:
+ raise Exception(f'"{interval_str}" is an invalid interval string!')
+
+ total_interval = datetime.timedelta(seconds=interval.total_seconds() * number)
+ return total_interval
+
+
+def _timestamp_to_date(timestamp: str) -> datetime.datetime:
+ return datetime.datetime.strptime(timestamp, "%Y-%m-%d")
+
+
+def _date_to_timestamp(date: datetime.datetime) -> str:
+ return datetime.datetime.strftime(date, "%Y-%m-%d")
+
+
+def _process_row(row: SheetsRow):
+ interval = _calculate_interval(row.interval)
+ last_done_date = _timestamp_to_date(row.last_done)
+ next_date = last_done_date + interval
+ next_date_str = _date_to_timestamp(next_date)
+
+ return ProcessedRow(
+ name=row.name,
+ next_datestamp=next_date_str,
+ next_date=next_date,
+ last_datestamp=row.last_done,
+ last_date=last_done_date,
+ interval=row.interval,
+ )
+
+
+def process_rows(rows: list[SheetsRow]) -> list[ProcessedRow]:
+ return list(map(_process_row, rows))
+
+
+def _is_this_week(processed_row: ProcessedRow):
+ next_date = processed_row.next_date
+ now = datetime.datetime.now()
+ return (next_date - now) < datetime.timedelta(weeks=1)
+
+
+def filter_only_this_week(rows: list[ProcessedRow]) -> list[ProcessedRow]:
+ return list(filter(_is_this_week, rows))