Convert a snake_case string to PascalCase. Args: snake (str): The snake_case string to be converted. Returns: str: The converted PascalCase string.
(snake: str)
| 334 | |
| 335 | |
| 336 | def to_pascal(snake: str) -> str: |
| 337 | """Convert a snake_case string to PascalCase. |
| 338 | |
| 339 | Args: |
| 340 | snake (str): The snake_case string to be converted. |
| 341 | |
| 342 | Returns: |
| 343 | str: The converted PascalCase string. |
| 344 | """ |
| 345 | # Check if the string is already in PascalCase |
| 346 | if re.match(r'^[A-Z][a-zA-Z0-9]*([A-Z][a-zA-Z0-9]*)*$', snake): |
| 347 | return snake |
| 348 | # Remove leading and trailing underscores |
| 349 | snake = snake.strip('_') |
| 350 | # Replace multiple underscores with a single one |
| 351 | snake = re.sub('_+', '_', snake) |
| 352 | # Convert to PascalCase |
| 353 | return re.sub( |
| 354 | '_([0-9A-Za-z])', |
| 355 | lambda m: m.group(1).upper(), |
| 356 | snake.title(), |
| 357 | ) |
| 358 | |
| 359 | |
| 360 | def get_pydantic_major_version() -> int: |
no test coverage detected