started refactoring utility functions

This commit is contained in:
Khalim Conn-Kowlessar 2023-06-13 12:22:43 +01:00
parent b7a4c57aeb
commit a5feff3472
4 changed files with 33 additions and 49 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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: