Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Added alternative strings for some jatelaji. Fixed parsing empty strings, when data is missing.  Fixed a case where SUM(astiamaara) is a decimal, with "," instead of ".". In reality this is likely a typo in the data.
  • Loading branch information
Mtk112 committed Nov 15, 2023
1 parent 5cfedf1 commit 380e010
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions jkrimporter/providers/lahti/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import re
from datetime import date
from dateutil.parser import parse as date_parser
from enum import Enum
from typing import Dict, Optional, Union

Expand All @@ -19,11 +20,11 @@ class Jatelaji(str, Enum):
aluekerays = "Aluekeräys"
seka = "Sekajäte"
energia = "Energia"
bio = "Biojäte"
kartonki = "Kartonkipakkaus"
bio = "Bio"
kartonki = "Kartonki"
pahvi = "Pahvi"
metalli = "Metallipakkaus"
lasi = "Lasipakkaus"
metalli = "Metalli"
lasi = "Lasi"
paperi = "Paperi"
muovi = "Muovi"
liete = "Liete"
Expand Down Expand Up @@ -118,6 +119,9 @@ def fix_posti(value: Union[str, int]):
@validator("koko", "paino", pre=True)
def parse_float(value: Union[float, str]):
# If float wasn't parsed, let's parse them
# Return none if empty string was parsed
if value == '':
return None
if type(value) is str:
# we might have . or , as the separator
return float(value.replace(",", "."))
Expand All @@ -127,6 +131,14 @@ def parse_float(value: Union[float, str]):
def parse_jatelaji(value: str):
if value == "Sekaj":
value = "Sekajäte"
if value == "Biojäte":
value = "Bio"
if value == "Kartonkipakkaus":
value = "Kartonki"
if value == "Muovipakkaus":
value = "Muovi"
if value == "Lasipakkaus":
value = "Lasi"
return value.title()

@validator("Pvmalk", "Pvmasti", pre=True)
Expand All @@ -142,8 +154,8 @@ def parse_date(value: Union[date, str]):
"tyhjennysvali", "Voimassaoloviikotalkaen", "Voimassaoloviikotasti", pre=True
)
def fix_na(value: str):
# Many fields may have strings such as #N/A. Parse them to None.
if value == "#N/A":
# Many fields may have strings such as #N/A or empty string. Parse them to None.
if value == "#N/A" or value == '':
return None
return value

Expand All @@ -154,3 +166,12 @@ def parse_kuntatunnus(value: str):
if value:
return int(value)
return None

@validator("astiamaara", pre=True, always=True)
def parse_decimal_separator(cls, value):
if isinstance(value, str):
# There is atleast one case where SUM(astiamaara) is "0,12"
# Not sure what to do here, doesn't make sense that 0.12 containers have been emptied.
# But then again, should this be converted to 0, 1 or 12?
value = float(value.replace(',', '.'))
return value

0 comments on commit 380e010

Please sign in to comment.