Skip to content

Commit

Permalink
Fixed python Materials enum invalid keys (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesramsden-bh authored Jun 25, 2024
2 parents c3feac7 + 170939e commit cc8f242
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""

# pylint: disable=E0401
import re
from difflib import get_close_matches
from pathlib import Path
from enum import Enum
Expand Down Expand Up @@ -178,8 +179,26 @@ 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:
"""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 re.match("^\d", material_identifier):
material_identifier = f"Material_{material_identifier}"

return material_identifier

Materials = Enum(
"Materials",
{sanitise_string(material.identifier): material for material in materials()},
{get_identifier(material.identifier): material for material in materials()},
)

0 comments on commit cc8f242

Please sign in to comment.