daveoncode/python-string-utils

snake case check fails for one word strings

amalakar opened this issue · 4 comments

# This should return True I believe
>>> string_utils.is_snake_case("user")
False
# looks like correct behavior
>>> string_utils.is_snake_case("user_name")
True

it's by design, specifically I wrote the test:

    def test_string_cannot_be_all_lowercase(self):
        self.assertFalse(is_camel_case('lowercase'))

And in the doc:

A string is considered snake case when:

* it's composed only by lowercase letters ([a-z]), underscores (or provided separator) \
and optionally numbers ([0-9])
* it does not start/end with an underscore (or provided separator)
* it does not start with a number

And on wikipedia (https://en.wikipedia.org/wiki/Snake_case):

Snake case (or snake_case) is the practice of writing compound words or phrases in which the elements are separated with one underscore character (_) and no spaces

So... if a string contains only a word it's definitely not a snake case string. .. however, my assumption about having only lowercased letters is wrong and I have to change this... not sure about the underscore as first or last place instead but maybe I should allow them too :/

for some reason it also fails with a_a, any idea why? if I add another letter at the end, it works: e.g. a_aa.

The correct regex is:

SNAKE_CASE_TEST_RE = re.compile(r'^[a-z][a-z0-9]+(_[a-z0-9]+)*$')

Snake case can clearly be composed of a single word based on its commonly understood usage. The example linux kernel code in the linked wikipedia page includes single word snake case variables. Also nothing in the doc would imply that it can't be a single word:

* it's composed only by lowercase letters ([a-z]), underscores (or provided separator) \
and optionally numbers ([0-9])
* it does not start/end with an underscore (or provided separator)
* it does not start with a number