From 3d6ed8863464a3d6f08d9406108ae853aed1aabf Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Thu, 8 Jun 2023 17:30:33 +0100 Subject: [PATCH] Moved out search_split_description --- epc_data/cleaning/Floor.py | 36 +++++++++++++++++++++++++-- epc_data/cleaning/Roof.py | 24 ++++-------------- epc_data/cleaning/cleaning_utils.py | 14 +++++++++++ epc_data/tests/test_clean_roof.py | 6 ----- epc_data/tests/test_cleaning_utils.py | 10 +++++++- 5 files changed, 62 insertions(+), 28 deletions(-) diff --git a/epc_data/cleaning/Floor.py b/epc_data/cleaning/Floor.py index e05892fa..67386290 100644 --- a/epc_data/cleaning/Floor.py +++ b/epc_data/cleaning/Floor.py @@ -1,4 +1,5 @@ from typing import Dict, Union +from epc_data.cleaning.cleaning_utils import extract_thermal_transmittence, search_split_description class CleanFloor: @@ -13,16 +14,47 @@ class CleanFloor: """ description_lower = self.description.lower().strip() + assumed = "assumed" in description_lower + to_unheated_space = "to unheated space" in description_lower if "another dwelling below" in description_lower: return self._make_clean_output( has_dwelling_below=True, + thermal_transmittence=None, + thermal_transmittence_unit=None, + to_unheated_space=to_unheated_space, + assumed=assumed ) - raise Exception("EKJH") + thermal_transmittence, thermal_transmittence_unit = None, None + if "insulated" in description_lower: + bh + elif "thermal transmittance" in description_lower: + thermal_transmittence, thermal_transmittence_unit = extract_thermal_transmittence(description_lower) + + return self._make_clean_output( + has_dwelling_below=False, + thermal_transmittence=thermal_transmittence, + thermal_transmittence_unit=thermal_transmittence_unit, + to_unheated_space=to_unheated_space, + assumed=assumed + ) + + @classmethod + def _find_insulation_thickness(cls, description_lower, to_unheated_space): + + if to_unheated_space: + desc_split = description_lower.split("to unheated space,")[-1].strip().split("(assumed)")[0].strip() + return search_split_description(desc_split) @staticmethod - def _make_clean_output(has_dwelling_below): + def _make_clean_output( + has_dwelling_below, thermal_transmittence, thermal_transmittence_unit, to_unheated_space, assumed + ) -> Dict[str, Union[str, bool, int, None]]: return { "has_dwelling_below": has_dwelling_below, + "thermal_transmittence": thermal_transmittence, + "thermal_transmittence_unit": thermal_transmittence_unit, + "to_unheated_space": to_unheated_space, + "assumed": assumed } diff --git a/epc_data/cleaning/Roof.py b/epc_data/cleaning/Roof.py index 8f2144c9..da620a01 100644 --- a/epc_data/cleaning/Roof.py +++ b/epc_data/cleaning/Roof.py @@ -1,5 +1,5 @@ from typing import Dict, Union, Optional -from epc_data.cleaning.cleaning_utils import extract_thermal_transmittence +from epc_data.cleaning.cleaning_utils import extract_thermal_transmittence, search_split_description class CleanRoof: @@ -126,22 +126,8 @@ class CleanRoof: } @staticmethod - def _search_split_description(desc: str) -> str: - """ - Searches roof descriptions and looks for key words, determining a description about the roof's insulation. - - :param desc: Description to be searched. - :return: Result of the search. - """ - if desc == "insulated": - return "average" - if desc == "limited": - return "below average" - raise NotImplementedError("Handle me") - - @classmethod def _find_insulation_thickness( - cls, description_lower: str, is_pitched: bool, is_roof_room: bool, is_flat: bool + description_lower: str, is_pitched: bool, is_roof_room: bool, is_flat: bool ) -> Union[int, str, None]: """ Finds insulation thickness in the description. @@ -168,7 +154,7 @@ class CleanRoof: if "invalid input" in description_lower: return None desc = description_lower.split("pitched,")[-1].strip().split(" ")[0] - return cls._search_split_description(desc) + return search_split_description(desc) if is_roof_room: desc_split_lookup = { @@ -182,11 +168,11 @@ class CleanRoof: return res desc = desc_split.split(" ")[0] - return cls._search_split_description(desc) + return search_split_description(desc) if is_flat: # Just search for specific phrases desc = description_lower.split("flat,")[-1].lstrip().split(" ")[0] - return cls._search_split_description(desc) + return search_split_description(desc) return None diff --git a/epc_data/cleaning/cleaning_utils.py b/epc_data/cleaning/cleaning_utils.py index d7196836..ec600bd4 100644 --- a/epc_data/cleaning/cleaning_utils.py +++ b/epc_data/cleaning/cleaning_utils.py @@ -27,3 +27,17 @@ def extract_thermal_transmittence(description_lower: str) -> Tuple[Union[float, unit = None return u_value, unit + + +def search_split_description(desc: str) -> str: + """ + Searches split descriptions and looks for key words, determining a description about the roof's/floor's insulation. + + :param desc: Description to be searched. + :return: Result of the search. + """ + if desc == "insulated": + return "average" + if desc == "limited": + return "below average" + raise NotImplementedError("Handle me") diff --git a/epc_data/tests/test_clean_roof.py b/epc_data/tests/test_clean_roof.py index 83be314d..e802dc75 100644 --- a/epc_data/tests/test_clean_roof.py +++ b/epc_data/tests/test_clean_roof.py @@ -32,12 +32,6 @@ class TestEpcClean: self.cleaner._init_empty_cleaned_obj() assert all([len(values) == 0 for values in self.cleaner.cleaned.values()]) - def test__search_split_roof_description(self): - assert CleanRoof._search_split_description("insulated") == "average" - assert CleanRoof._search_split_description("limited") == "below average" - with pytest.raises(NotImplementedError): - CleanRoof._search_split_description("unknown") - def test__find_insulation_thickness(self): assert CleanRoof._find_insulation_thickness("no insulation", False, False, False) == 0 diff --git a/epc_data/tests/test_cleaning_utils.py b/epc_data/tests/test_cleaning_utils.py index d5b77916..dd9e4444 100644 --- a/epc_data/tests/test_cleaning_utils.py +++ b/epc_data/tests/test_cleaning_utils.py @@ -1,6 +1,14 @@ -from epc_data.cleaning.cleaning_utils import extract_thermal_transmittence +import pytest +from epc_data.cleaning.cleaning_utils import extract_thermal_transmittence, search_split_description def test__extract_thermal_transmittence(): description = "U-value of 2.3 w/m-¦k" assert extract_thermal_transmittence(description) == (2.3, "w/m-¦k") + + +def test__search_split_roof_description(): + assert search_split_description("insulated") == "average" + assert search_split_description("limited") == "below average" + with pytest.raises(NotImplementedError): + search_split_description("unknown")