From d3bf505514d36c6a90ede4d2b543272454dcba11 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Tue, 25 Jun 2024 11:14:45 +0100 Subject: [PATCH 1/4] created new method which sanitises the material names so that they can be accessed as enum values --- .../ladybugtools_toolkit/external_comfort/material.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py index b075df4b..d8fc8b8e 100644 --- a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py +++ b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py @@ -2,6 +2,7 @@ """ # pylint: disable=E0401 +import re from difflib import get_close_matches from pathlib import Path from enum import Enum @@ -178,8 +179,15 @@ def get_material(material_identifier: str) -> _EnergyMaterialOpaqueBase: f"Unknown material name: '{material_identifier}'. Did you mean {possible_materials}" ) from exc +def get_identifier(material_identifier: str) -> str: + keep_characters = r"[^A-Za-z0-9_]" + + if any([material_identifier.startswith(str(num)) for num in range(10)]): + material_identifier = f"_{material_identifier}" + + return re.sub(keep_characters, "_", material_identifier).replace("__", "_").rstrip() Materials = Enum( "Materials", - {sanitise_string(material.identifier): material for material in materials()}, + {get_identifier(material.identifier): material for material in materials()}, ) From 1b0fbd24d92e4e937515669a87693cb7a881e7d5 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Tue, 25 Jun 2024 12:45:38 +0100 Subject: [PATCH 2/4] prepended value is now Thickness_, allowing the value to be visible when searching with tab --- .../src/ladybugtools_toolkit/external_comfort/material.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py index d8fc8b8e..b2dc84b3 100644 --- a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py +++ b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py @@ -182,8 +182,9 @@ def get_material(material_identifier: str) -> _EnergyMaterialOpaqueBase: def get_identifier(material_identifier: str) -> str: keep_characters = r"[^A-Za-z0-9_]" + # prepend `Thickness_` to identifiers which start with a number as the number references the thickness if any([material_identifier.startswith(str(num)) for num in range(10)]): - material_identifier = f"_{material_identifier}" + material_identifier = f"Thickness_{material_identifier}" return re.sub(keep_characters, "_", material_identifier).replace("__", "_").rstrip() From 1a3244c46aa7a25833b0338adb80c3ad5cebe0fd Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Tue, 25 Jun 2024 13:11:50 +0100 Subject: [PATCH 3/4] Apply suggestions from code review will commit regex change locally as it should go after substitution Co-authored-by: James Ramsden --- .../src/ladybugtools_toolkit/external_comfort/material.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py index b2dc84b3..120e5440 100644 --- a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py +++ b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py @@ -182,9 +182,9 @@ def get_material(material_identifier: str) -> _EnergyMaterialOpaqueBase: def get_identifier(material_identifier: str) -> str: keep_characters = r"[^A-Za-z0-9_]" - # prepend `Thickness_` to identifiers which start with a number as the number references the thickness + # prepend `Material_` to identifiers which start with a number if any([material_identifier.startswith(str(num)) for num in range(10)]): - material_identifier = f"Thickness_{material_identifier}" + material_identifier = f"Material_{material_identifier}" return re.sub(keep_characters, "_", material_identifier).replace("__", "_").rstrip() From 170939e44997f7920b5ce20034c4801ed167d988 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Tue, 25 Jun 2024 13:15:05 +0100 Subject: [PATCH 4/4] changes based on comments by @jamesramsden-bh --- .../external_comfort/material.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py index 120e5440..30fe35af 100644 --- a/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py +++ b/LadybugTools_Engine/Python/src/ladybugtools_toolkit/external_comfort/material.py @@ -180,13 +180,23 @@ def get_material(material_identifier: str) -> _EnergyMaterialOpaqueBase: ) from exc def get_identifier(material_identifier: str) -> str: + """Get the enum-valid identifier for a given material identifier. + + Args: + material_identifier (str): The identifier to sanitise. + + Returns: + str: The sanitised material identifier. + """ keep_characters = r"[^A-Za-z0-9_]" + material_identifier = re.sub(keep_characters, "_", material_identifier).replace("__", "_").rstrip() + # prepend `Material_` to identifiers which start with a number - if any([material_identifier.startswith(str(num)) for num in range(10)]): + if re.match("^\d", material_identifier): material_identifier = f"Material_{material_identifier}" - return re.sub(keep_characters, "_", material_identifier).replace("__", "_").rstrip() + return material_identifier Materials = Enum( "Materials",