Fix camel_to_snake to correctly handle acronyms#7
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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])'tor'(?<=[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.pyto 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.