Skip to content

Fix camel_to_snake to correctly handle acronyms#7

Merged
digitalex merged 1 commit into
mainfrom
fix-camel-snake-acronyms
Apr 10, 2025
Merged

Fix camel_to_snake to correctly handle acronyms#7
digitalex merged 1 commit into
mainfrom
fix-camel-snake-acronyms

Conversation

@digitalex
Copy link
Copy Markdown
Owner

The camel_to_snake function in utils.py was updated to correctly handle acronyms in camel-cased strings. The regular expression was modified to accurately identify word boundaries, ensuring that acronyms are treated as single units.

Specifically, the regex was changed from r'(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])' to r'(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z]{2,}). This new regex includes an additional check for lowercase letters followed by two or more uppercase letters, effectively detecting the start of acronyms.

New test cases were added to utils_test.py to specifically cover acronym scenarios, such as HTTPRequest -> http_request and CustomerID -> customer_id.

After implementing the change, all tests were run, and all 6 tests passed, including the new acronym tests. This confirms that the function now correctly handles acronyms and maintains its existing functionality.

The camel_to_snake function in utils.py was updated to correctly handle acronyms in camel-cased strings. The regular expression was modified to accurately identify word boundaries, ensuring that acronyms are treated as single units.

Specifically, the regex was changed from `r'(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])'` to `r'(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z]{2,})`. This new regex includes an additional check for lowercase letters followed by two or more uppercase letters, effectively detecting the start of acronyms.

New test cases were added to `utils_test.py` to specifically cover acronym scenarios, such as HTTPRequest -> http_request and CustomerID -> customer_id.

After implementing the change, all tests were run, and all 6 tests passed, including the new acronym tests. This confirms that the function now correctly handles acronyms and maintains its existing functionality.
@digitalex digitalex merged commit 5b7a2ff into main Apr 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant