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.1.tar.gz (51.0 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.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: magic_case-0.3.1.tar.gz
  • Upload date:
  • Size: 51.0 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.1.tar.gz
Algorithm Hash digest
SHA256 9d159d6117e2a727a61ea848c8bc50ae26647cc79b32d0d5493bbb25e2e15a1e
MD5 ca94cbe04ef4b750a5da880dbfc1498d
BLAKE2b-256 dcc5bdb5c64421ff9dcfe02af88a1efa3975119224483c9f0b8cfc0b664ca03c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magic_case-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 57162726b2f73d0fda0ab28cca3c6a638d8bf16c6299ce77edb91c06c9993d82
MD5 0d802316897e1d694d28541557b87579
BLAKE2b-256 d7c03b8af1e8ce2caa92b45d0b9c3ffdc2e3bb76d5f1d74d154b47db3bbcf482

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