Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed python Materials enum invalid keys #214

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()},
)