Skip to content

Consecutive underscores in camelize/underscore transformations aren't reversible #26

Open
@levic

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions