Skip to content

Case convert and verify for Python: snake_case, camelCase, kebab-case, and more.

License

Notifications You must be signed in to change notification settings

makukha/caseutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

50 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

caseutil ⇄ 🐍🐫🍒

Case conversion and verification for Python: snake_case, camelCase, kebab-case, etc.

license pypi python versions tests coverage tested with multipython docs status uses docsub mypy uv Ruff openssf best practices

Features

  • Verify and convert between most popular cases
  • Custom separators: 'foo.bar.baz', 'foo/bar/baz'
  • Case detection
  • Command line utility caseutil
  • Pure Python 2.7 to 3.14+
  • No dependencies
  • 100% test coverage

Supported cases

Cases classification Cases classification

Case Verify Convert
snake_case is_snake to_snake
Ada_Case is_ada to_ada
CONST_CASE is_const to_const
camelCase is_camel to_camel
PascalCase is_pascal to_pascal
kebab-case is_kebab to_kebab
Train-Case is_train to_train
COBOL-CASE is_cobol to_cobol
lower case is_lower to_lower
UPPER CASE is_upper to_upper
Title Case is_title to_title
Sentence case is_sentence to_sentence

Installation

$ pip install caseutil

Use cases

Basic usage

>>> from caseutil import is_snake, to_snake

>>> is_snake('Foo bar-baz')
False

>>> to_snake('Foo bar-baz')
'foo_bar_baz'

Cases enum

All supported cases are gathered in Case enum:

class Case(StrEnum):
    ADA = 'ada'
    CAMEL = 'camel'
    COBOL = 'cobol'
    CONST = 'const'
    KEBAB = 'kebab'
    LOWER = 'lower'
    PASCAL = 'pascal'
    SENTENCE = 'sentence'
    SNAKE = 'snake'
    TITLE = 'title'
    TRAIN = 'train'
    UPPER = 'upper'

Arbitrary cases

Use functions is_case() and to_case() to deal with arbitrary supported case:

>>> from caseutil import Case, is_case, to_case

>>> is_case(Case.CAMEL, 'myVarName')
True

>>> to_case(Case.CONST, 'myVarName')
'MY_VAR_NAME'

Detect cases

Use function get_cases() to determine case (or cases, if ambiguous):

>>> from caseutil import get_cases

>>> get_cases('fooBar')
('camel',)

>>> get_cases('My var-name')  # mixed case
()

>>> get_cases('Title')  # matches multiple cases
('ada', 'pascal', 'sentence', 'title', 'train')

Custom separators

Use function words():

>>> from caseutil import words, to_lower

>>> '/'.join(words(to_lower('myVarName')))
'my/var/name'

>>> '.'.join(words('myVarName'))
'my.Var.Name'

Tokenization

Word separators are non-word characters including underscore, and places where text case is changed from lower to upper. Digits are not treated as separators. For more details, see Tokenization rules.

>>> from caseutil import words

>>> words('!some_reallyMESsy text--wit4Digits.3VeryWh3re--')
['some', 'really', 'ME', 'Ssy', 'text', 'wit4', 'Digits', '3Very', 'Wh3re']

Unicode support (not implemented)

Only ASCII names are supported. Unicode support is planned.

Command line

$ caseutil -c const "hi there"
HI_THERE

Invoke as Python module:

$ python -m caseutil -c const "hi there"
HI_THERE

When reading from stdin, each line is processed separately:

$ echo "hi_there\nsee you" | python -m caseutil -c camel
hiThere
seeYou

CLI Reference

$ caseutil --help
usage: caseutil [-h] (--version | -c <case> | -d) [text]

  Convert, detect, or match text case.

  When stdin is used as input, each line is tokenized and processed separately.

cases:
  ada,camel,cobol,const,kebab,lower,pascal,sentence,snake,title,train,upper

positional arguments:
  text                  text to be converted; if missing, stdin is used

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -c, --convert <case>  convert [text] or stdin to <case>
  -d, --detect          detect cases in [text] or stdin

Alternatives

70+ packages

Contributing

See Contributing guidelines.

Authors

See also