Skip to content

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

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

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

caseutil ⇄ 🐍🐫🍒

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

versions
pypi Tests Coverage PyPI - Downloads
license Documentation Status 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

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

For more details about cases and their relations, see Cases classification.

Installation

$ pip install caseutil

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

Simple usage

>>> from caseutil import *

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

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

Advanced usage

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'

Universal operations

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

>>> is_case(Case.CAMEL, 'myVarName')
True
>>> to_case(Case.CONST, 'myVarName')
'MY_VAR_NAME'

Cases detection

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

>>> get_cases('fooBar')
('camel',)
>>> get_cases('My var-name')  # mixed case
()
>>> get_cases('Title')
('ada', 'pascal', 'sentence', 'title', 'train')

Custom separators

Use words() function:

>>> '/'.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.

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

Unicode support

Only ASCII names are supported. Unicode support is planned.

Development

This project requires Docker.

git clone https://github.com/makukha/caseutil.git
cd caseutil
task dev
root@caseutil:/project# task lint
root@caseutil:/project# task format
root@caseutil:/project# task test

Contributing

See Contributing guidelines.

Authors

License

MIT License