From a5feff3472df06dedce92b402000145964328033 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 13 Jun 2023 12:22:43 +0100 Subject: [PATCH] started refactoring utility functions --- epc_data/attributes/FloorAttributes.py | 15 +++++---- epc_data/attributes/RoofAttributes.py | 15 ++------- epc_data/attributes/WallAttributes.py | 10 ++---- epc_data/attributes/attribute_utils.py | 42 ++++++++++++++------------ 4 files changed, 33 insertions(+), 49 deletions(-) diff --git a/epc_data/attributes/FloorAttributes.py b/epc_data/attributes/FloorAttributes.py index 0c855692..04be5e6a 100644 --- a/epc_data/attributes/FloorAttributes.py +++ b/epc_data/attributes/FloorAttributes.py @@ -3,17 +3,16 @@ from epc_data.attributes.attribute_utils import extract_thermal_transmittence, s class FloorAttributes: - INSULATION_TERMS = ["insulation", "insulated"] - FLOOR_TYPES = ["suspended", "solid"] - EXPOSURE_TYPES = ["to unheated space", "to external air"] - DWELLING_BELOW_TYPES = ["another dwelling below", "other premises below"] + DWELLING_BELOW = ["another dwelling below", "other premises below"] + FLOOR_TYPES = ["assumed", "to unheated space", "to external air", "suspended", "solid"] def __init__(self, description: str): - if not description or not any(term in description.lower() for term in - self.FLOOR_TYPES + self.INSULATION_TERMS + self.EXPOSURE_TYPES + - self.DWELLING_BELOW_TYPES): + self.description: str = description.lower() + + if not description or not any( + rt in self.description for rt in self.FLOOR_TYPES + self.DWELLING_BELOW + ["average thermal transmittance"] + ): raise ValueError('Invalid description') - self.description = description.lower().strip() def process(self) -> Dict[str, Union[str, bool, int, None]]: description = self.description diff --git a/epc_data/attributes/RoofAttributes.py b/epc_data/attributes/RoofAttributes.py index 2626e688..eec0434d 100644 --- a/epc_data/attributes/RoofAttributes.py +++ b/epc_data/attributes/RoofAttributes.py @@ -1,6 +1,6 @@ import re from typing import Dict, Union -from epc_data.attributes.attribute_utils import extract_component_types +from epc_data.attributes.attribute_utils import extract_component_types, extract_thermal_transmittence class RoofAttributes: @@ -21,22 +21,11 @@ class RoofAttributes: def process(self) -> Dict[str, Union[float, str, bool, None]]: - if not self.description: - raise ValueError("Description is empty") - result: Dict[str, Union[float, str, bool, None]] = {} description = self.description # thermal transmittance - match = re.search(r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)", description) - if match: - result['thermal_transmittance'] = float(match.group(1)) - result['thermal_transmittance_unit'] = match.group(2) - # Remove the match from the description - description = re.sub(r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)", "", description) - else: - result['thermal_transmittance'] = None - result['thermal_transmittance_unit'] = None + result, description = extract_thermal_transmittence(result, description) # roof type result, description = extract_component_types(result, description, list_of_components=self.ROOF_TYPES) diff --git a/epc_data/attributes/WallAttributes.py b/epc_data/attributes/WallAttributes.py index 39d0ca25..cd0f2e08 100644 --- a/epc_data/attributes/WallAttributes.py +++ b/epc_data/attributes/WallAttributes.py @@ -1,6 +1,6 @@ import re from typing import Dict, Union -from epc_data.attributes.attribute_utils import extract_component_types +from epc_data.attributes.attribute_utils import extract_component_types, extract_thermal_transmittence class WallAttributes: @@ -18,13 +18,7 @@ class WallAttributes: description = self.description.lower() # thermal transmittance - it can be negative which is errneous however we'll still pull it out - match = re.search(r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)", description) - if match: - result['thermal_transmittance'] = float(match.group(1)) - result['thermal_transmittance_unit'] = match.group(2) - else: - result['thermal_transmittance'] = None - result['thermal_transmittance_unit'] = None + result, description = extract_thermal_transmittence(result, description) # wall type result, description = extract_component_types(result, description, list_of_components=self.WALL_TYPES) diff --git a/epc_data/attributes/attribute_utils.py b/epc_data/attributes/attribute_utils.py index 4b51ff65..0aa6d6ac 100644 --- a/epc_data/attributes/attribute_utils.py +++ b/epc_data/attributes/attribute_utils.py @@ -1,32 +1,32 @@ import re -from typing import Tuple, Union +from typing import Tuple, Union, Dict -THERMAL_TRANSMITTANCE_U_VALUE_REGEX = re.compile(r"(\d+\.\d+)") -THERMAL_TRANSMITTANCE_UNIT_REGEX = re.compile(r"(w/m-¦k)") +THERMAL_TRANSMITTENCE_STR = r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)" +THERMAL_TRANSMITTANCE_REGEX = re.compile(THERMAL_TRANSMITTENCE_STR) -def extract_thermal_transmittence(description_lower: str) -> Tuple[Union[float, None], Union[str, None]]: +def extract_thermal_transmittence(result: dict, description: str) -> Tuple[ + Dict[str, Union[None, str, float]], str +]: """ Extracts thermal transmittance from the description. - :param description_lower: Lowercase description. - :return: Tuple containing U-value and unit. + :param result: Dictionary to store the result in. + :param description: Lowercase description. + :return: Tuple of r """ - # Find U-value - u_value = re.search(THERMAL_TRANSMITTANCE_U_VALUE_REGEX, description_lower) - if u_value is not None: - u_value = float(u_value.group(1)) + # thermal transmittance + match = THERMAL_TRANSMITTANCE_REGEX.search(description) + if match: + result['thermal_transmittance'] = float(match.group(1)) + result['thermal_transmittance_unit'] = match.group(2) + # Remove the match from the description + description = re.sub(THERMAL_TRANSMITTENCE_STR, "", description) else: - u_value = None + result['thermal_transmittance'] = None + result['thermal_transmittance_unit'] = None - # Find unit - unit = re.search(THERMAL_TRANSMITTANCE_UNIT_REGEX, description_lower) - if unit is not None: - unit = unit.group(1) - else: - unit = None - - return u_value, unit + return result, description def search_split_description(desc: str) -> str: @@ -51,7 +51,9 @@ def search_split_description(desc: str) -> str: raise NotImplementedError("Handle me") -def extract_component_types(result: dict, description: str, list_of_components: list) -> list: +def extract_component_types(result: dict, description: str, list_of_components: list) -> Tuple[ + Dict[str, Union[None, str, float]], str +]: """ Extracts component types from the description. :param result: