Skip to content

Instantly share code, notes, and snippets.

@laurent-laporte-pro
Last active July 6, 2023 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laurent-laporte-pro/f6b5827b8fe4f9b9ec3bac41cc601bf4 to your computer and use it in GitHub Desktop.
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.
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
@laurent-laporte-pro
Copy link
Author

Same function converted to R script:

transform_name_to_id <- function(name, lower = TRUE) {
  valid_id <- gsub("[^a-zA-Z0-9_(),& -]+", " ", name)
  valid_id <- trimws(valid_id)
  if (lower) {
    valid_id <- tolower(valid_id)
  }
  return(valid_id)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment