Skip to main content

Lightweight utilities for converting strings between common cases

Project description

Magic Case

Lightweight utilities for converting strings between common cases, built on a simple BaseCase abstraction.

  • Cases: SnakeCase, CamelCase, PascalCase, KebabCase, UpperCase, SentenceCase, TitleCase, DotCase, SpaceCase, FlatCase, HttpHeaderCase, CamelSnakeCase, HungarianCase, MacroCase, PascalSnakeCase, PathCase, SlashTitleCase
  • Composable: Construct from raw strings or from another case class
  • Zero deps: No runtime dependencies

Installation

Using uv:

uv add magic-case

Standard pip:

pip install "magic-case"

Quick start

from magic_case import (
    SnakeCase,
    CamelCase,
    PascalCase,
    KebabCase,
    UpperCase,
    SentenceCase,
    TitleCase,
    DotCase,
    SpaceCase,
    FlatCase,
    HttpHeaderCase,
    CamelSnakeCase,
    HungarianCase,
    MacroCase,
    PascalSnakeCase,
    PathCase,
    SlashTitleCase,
)

# Convert a string to different cases
snake = SnakeCase('hello_world')
print(snake.get())  # hello_world

Convert between cases

Construct from another case to re-render with a different style:

from magic_case import SnakeCase, KebabCase, PascalCase

snake = SnakeCase("hello_world")

kebab = KebabCase(snake)
print(kebab)  # hello-world

pascal = PascalCase(snake)
print(pascal)  # HelloWorld

Round-trip conversion example

from magic_case import SnakeCase, KebabCase, CamelCase

original = SnakeCase("hello_world_example")
converted = KebabCase(original)
print(converted)  # hello-world-example

back_to_snake = SnakeCase(converted)
print(back_to_snake)  # hello_world_example
assert back_to_snake.words == original.words

Case Examples Table

Case Input Output
SnakeCase helloWorldAgain hello_world_again
KebabCase helloWorldAgain hello-world-again
CamelCase hello_world_again helloWorldAgain
PascalCase hello_world_again HelloWorldAgain
UpperCase hello_world_again HELLO_WORLD_AGAIN
SentenceCase hello_world_again Hello world again
TitleCase hello_world_again Hello World Again
DotCase helloWorldAgain hello.world.again
SpaceCase helloWorldAgain hello world again
FlatCase helloWorldAgain helloworldagain
HttpHeaderCase helloWorldAgain Hello-World-Again
CamelSnakeCase helloWorldAgain hello_World_Again
HungarianCase strHelloWorld str_hello_world
MacroCase helloWorldAgain HELLO_WORLD_AGAIN
PascalSnakeCase HelloWorldAgain Hello_World_Again
PathCase helloWorldAgain hello/world/again
SlashTitleCase helloWorldAgain Hello/World/Again

API

  • BaseCase
    • words: List[str] normalized to lowercase
    • get() -> str returns the rendered string (same as str(instance))
    • Subclasses implement:
      • _split_into_words(text: str) -> List[str]
      • __str__(self) -> str
  • Concrete cases
    • SnakeCase, CamelCase, PascalCase, KebabCase, UpperCase, SentenceCase, TitleCase, DotCase, SpaceCase, FlatCase, HttpHeaderCase, CamelSnakeCase, HungarianCase, MacroCase, PascalSnakeCase, PathCase, SlashTitleCase

Requirements

  • Python 3.8+

Contributing

If you find a bug, unexpected behavior, or have ideas to improve magic-case, please report it via the GitHub issues page. Your feedback helps make it better!

License

MIT

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

magic_case-0.3.2.tar.gz (51.1 kB view details)

Uploaded Source

Built Distribution

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

magic_case-0.3.2-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file magic_case-0.3.2.tar.gz.

File metadata

  • Download URL: magic_case-0.3.2.tar.gz
  • Upload date:
  • Size: 51.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for magic_case-0.3.2.tar.gz
Algorithm Hash digest
SHA256 fb66277e410264b56c0040dcbf94d4eb02dc7423647032b2f89b20b517f8572e
MD5 13ce567a913b7cfba78a1f75e2241227
BLAKE2b-256 8eb222212ffdb964e7bea5c7f525561d90811c320e6431ebe64703b75c1d69b7

See more details on using hashes here.

File details

Details for the file magic_case-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: magic_case-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for magic_case-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e48f282c4d5e6829e3454838e42b4ed301341edf2448ffbbe40ed19ee3829e8d
MD5 f21df4d8aedb96be9547350a6a2b0c6f
BLAKE2b-256 6ebcc5d0dc44072ab8deaf95920f440472497cd34c7df32e17cd8d1fb82e61d7

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