Open
Description
Every underscore/camelize cycle drops an underscore in the case of consecutive underscores.
Consider the following snippet:
import inflection
x = 'a____b_c'
for i in range(4):
x = inflection.camelize(x)
print(x)
x = inflection.underscore(x)
print(x)
Output:
A__bC
a__b_c
A_bC
a_b_c
ABC
abc
Abc
abc
I understand that not every transformation is going to be cleanly reversible.
Given that underscores are already word separators a number of python packages use double underscores as higher-level separators though (most notably django's ORM but also eg fixture generators factoryboy and model mommy). Preserving reversibility of consecutive underscores is a useful property.
The tests and docs don't seem cover the behaviour of consecutive underscores -- it seems to be undefined.
Would you accept a patch that changed the behaviour to preserve consecutive underscores? My proposal is:
- For underscores in the middle of a word, camelizing drops one underscore:
camelize('a___b_c') == 'A__bC'
camelize('a__b_c') == 'A_bC'
camelize('a_b_c') == 'ABC'
underscore('A__bC') == 'a___b_c'
underscore('A_bC') == 'a__b_c'
underscore('ABC') == 'a_b_c'
- For underscores at the start of a word, camelizing preserves the number of underscores and doesn't uppercase the first non-underscore letter
camelize('___a') == '___a'
camelize('__a') == '__a'
camelize('_a') == '_a'
underscore('___a') == '___a'
underscore('__a') == '__a'
underscore('_a') == '_a'
Activity