Skip to main content

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

Project description

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

caseutil-0.7.2.tar.gz (122.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caseutil-0.7.2-py2.py3-none-any.whl (8.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file caseutil-0.7.2.tar.gz.

File metadata

  • Download URL: caseutil-0.7.2.tar.gz
  • Upload date:
  • Size: 122.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for caseutil-0.7.2.tar.gz
Algorithm Hash digest
SHA256 36e5283590e67e33749d6c1ddb913dcfabb870749215776f7b411f9eb8e361e6
MD5 07a27546ec987a2816a64d5a905978f2
BLAKE2b-256 5dde8cdaf896811af1f1d64833e511baa9dec13b4ca24cf4c34f1b45daeee82a

See more details on using hashes here.

File details

Details for the file caseutil-0.7.2-py2.py3-none-any.whl.

File metadata

  • Download URL: caseutil-0.7.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for caseutil-0.7.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 11c06c3379e381d0c3633e910be5463e28fa983c42cd086382da5f73ea587a91
MD5 a16598468865ac163735e74e2ab898ac
BLAKE2b-256 1ca820761d6a13a6a6f290c5ed53d8b633e20167477dff0851844ce00553cfda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page