Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pycases

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pycases

A case conversion library with Unicode support

  • 0.1.3
  • PyPI
  • Socket score

Maintainers
1

pycases

PyPI version License Build Status

A case conversion library for Python.

Features

  • Automatic case detection, no need to specify the input case
  • Extremely fast, written in Rust ✨
  • Support for Unicode characters
  • Support for providing acronyms in title case

Supported cases

FunctionOutput
cases.to_camel(s)camelCase
cases.to_pascal(s)PascalCase
cases.to_snake(s)snake_case
cases.to_screaming_snake(s)SCREAMING_SNAKE_CASE
cases.to_kebab(s)kebab-case
cases.to_screaming_kebab(s)SCREAMING-KEBAB-CASE
cases.to_train(s)Train-Case
cases.to_lower(s)lower case
cases.to_title(s)Title Case
cases.to_upper(s)UPPER CASE

Getting started

Install using

pip install pycases

Now convert a string using the relevant function.

import cases

cases.to_snake("XMLHttpRequest") # returns "xml_http_request"

Details

Each of the provided functions using the same underlying implementation which does the following:

  • Divide the input string into words
  • Convert each word as required
  • Join the words back together optionally with a separator

Word boundaries are defined as follows:

  • A set of consecutive Unicode non-letter and non-number characters.

    For example: 'foo _bar' is two words (foo and bar)

  • A transition from a lowercase letter to an uppercase letter.

    For example: fooBar is two words (foo and Bar)

  • A transition from multiple uppercase letters to a single uppercase letter followed by lowercase letters.

    For example: FOOBar is two words (FOO and Bar)

Functions where the transform is "title" accept an optional acronyms argument, which is a mapping of lowercase words to their output. For example:

>>> cases.to_pascal("xml_http_request", acronyms={"xml": "XML"})
'XMLHttpRequest'
>>> cases.to_pascal("xml_http_request", acronyms={"xml": "XML", "http": "HTTP"})
'XMLHTTPRequest'

Benchmarks

A simple benchmark against various other libraries is provided in ./benches. The following table shows the results when run on my Macbook M2 Max.

LibraryMin (µs)Max (µs)Mean (µs)
cases21.375049.667022.1288
pure python62.8750186.958066.2344
regex80.8330201.250087.0549
stringcase101.8340204.9590108.6977
inflection230.2920581.4580253.9194
case-conversion1,431.79201,745.70801,506.2268

License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc