Last active
July 6, 2023 08:56
-
-
Save laurent-laporte-pro/f6b5827b8fe4f9b9ec3bac41cc601bf4 to your computer and use it in GitHub Desktop.
Transform a name into an identifier by replacing consecutive invalid characters by a single white space, and then whitespaces are striped from both ends.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# Invalid chars was taken from Antares Simulator (C++). | |
_sub_invalid_chars = re.compile(r"[^a-zA-Z0-9_(),& -]+").sub | |
def transform_name_to_id(name: str, lower: bool = True) -> str: | |
""" | |
Transform a name into an identifier by replacing consecutive | |
invalid characters by a single white space, and then whitespaces | |
are striped from both ends. | |
Valid characters are `[a-zA-Z0-9_(),& -]` (including space). | |
Args: | |
name: The name to convert. | |
lower: The flag used to turn the identifier in lower case. | |
""" | |
valid_id = _sub_invalid_chars(" ", name).strip() | |
return valid_id.lower() if lower else valid_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same function converted to R script: