Skip to content

Commit

Permalink
feat(generators): update Python identifiers generators to improve per…
Browse files Browse the repository at this point in the history
…formance
  • Loading branch information
catcombo committed Oct 30, 2024
1 parent fa44013 commit f53dd23
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions spec2sdk/generators/identifiers.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import re
from keyword import iskeyword
from typing import Final, Pattern

from humps import decamelize, pascalize

INVALID_CHARACTERS_PATTERN: Final[Pattern] = re.compile(r"[^0-9a-z]+|^[^a-z]+", flags=re.IGNORECASE)


def make_identifier(name: str) -> str:
"""
Makes valid Python identifier from the string.
Makes valid Python identifier from the string by removing invalid leading characters
and replacing invalid characters with underscore.
"""
# Remove invalid characters
name = re.sub(r"[^0-9a-zA-Z_]", "_", name)

# Remove leading characters until we find a letter
name = re.sub(r"^[^a-zA-Z]+", "", name)

# Replace consecutive duplicates of the underscore
name = re.sub(r"_+", "_", name)
name = "_".join(name_part for name_part in INVALID_CHARACTERS_PATTERN.split(name) if name_part)

# Add underscore to the name if it's a valid Python keyword
if iskeyword(name):
Expand Down

0 comments on commit f53dd23

Please sign in to comment.