sanitize#

scikitplot.cython.sanitize(name)[source]#

Convert an arbitrary string into a valid Python module name.

Parameters:
namestr

Input string (path-like strings allowed).

Returns:
str

Sanitized module-like identifier. The returned string is guaranteed to be a non-empty, valid Python identifier consisting only of ASCII alphanumerics and underscores.

Raises:
TypeError

If name is not a str.

Parameters:

name (str)

Return type:

str

Notes

  • Non-alphanumeric characters (including /, -, ., spaces) are replaced with underscores.

  • If the first character of the result would be a digit, a leading underscore is prepended so that the output is always a valid identifier.

  • An empty input string returns "_" (the minimal valid identifier).

Examples

>>> sanitize("hello-world")
'hello_world'
>>> sanitize("123abc")
'_123abc'
>>> sanitize("")
'_'
>>> sanitize("a/b/c")
'a_b_c'