alias_generators.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Alias generators for converting between different capitalization conventions."""
  2. import re
  3. __all__ = ('to_pascal', 'to_camel', 'to_snake')
  4. # TODO: in V3, change the argument names to be more descriptive
  5. # Generally, don't only convert from snake_case, or name the functions
  6. # more specifically like snake_to_camel.
  7. def to_pascal(snake: str) -> str:
  8. """Convert a snake_case string to PascalCase.
  9. Args:
  10. snake: The string to convert.
  11. Returns:
  12. The PascalCase string.
  13. """
  14. camel = snake.title()
  15. return re.sub('([0-9A-Za-z])_(?=[0-9A-Z])', lambda m: m.group(1), camel)
  16. def to_camel(snake: str) -> str:
  17. """Convert a snake_case string to camelCase.
  18. Args:
  19. snake: The string to convert.
  20. Returns:
  21. The converted camelCase string.
  22. """
  23. # If the string is already in camelCase and does not contain a digit followed
  24. # by a lowercase letter, return it as it is
  25. if re.match('^[a-z]+[A-Za-z0-9]*$', snake) and not re.search(r'\d[a-z]', snake):
  26. return snake
  27. camel = to_pascal(snake)
  28. return re.sub('(^_*[A-Z])', lambda m: m.group(1).lower(), camel)
  29. def to_snake(camel: str) -> str:
  30. """Convert a PascalCase, camelCase, or kebab-case string to snake_case.
  31. Args:
  32. camel: The string to convert.
  33. Returns:
  34. The converted string in snake_case.
  35. """
  36. # Handle the sequence of uppercase letters followed by a lowercase letter
  37. snake = re.sub(r'([A-Z]+)([A-Z][a-z])', lambda m: f'{m.group(1)}_{m.group(2)}', camel)
  38. # Insert an underscore between a lowercase letter and an uppercase letter
  39. snake = re.sub(r'([a-z])([A-Z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
  40. # Insert an underscore between a digit and an uppercase letter
  41. snake = re.sub(r'([0-9])([A-Z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
  42. # Insert an underscore between a lowercase letter and a digit
  43. snake = re.sub(r'([a-z])([0-9])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
  44. # Replace hyphens with underscores to handle kebab-case
  45. snake = snake.replace('-', '_')
  46. return snake.lower()