Inflection
Inflection is a Python library that provides string transformation utilities, porting the functionality of Ruby on Rails' inflector. It handles pluralization, singularization, and various case conversions like CamelCase to snake_case. The current version is 0.5.1, released in August 2020, indicating an irregular release cadence and a maintenance status.
Warnings
- breaking Version 0.4.0 dropped support for Python 2.x (2.6, 2.7) and older Python 3.x versions (3.3, 3.4), as well as PyPy. The library now requires Python 3.5 or newer. Ensure your environment meets this requirement before upgrading or using.
- gotcha The `pluralize` and `singularize` functions are specifically designed for English words and may not produce correct results for other languages. The library is a direct port of Ruby on Rails' inflector, which primarily targets English.
- gotcha The `camelize()` function defaults to `uppercase_first_letter=True`, resulting in `UpperCamelCase`. If `lowerCamelCase` is desired, you must explicitly set this parameter to `False`. For example, `inflection.camelize('foo_bar', False)` returns 'fooBar'.
Install
-
pip install inflection
Imports
- camelize
import inflection inflection.camelize('device_type') - underscore
import inflection inflection.underscore('DeviceType') - pluralize
import inflection inflection.pluralize('cat') - singularize
import inflection inflection.singularize('cats') - dasherize
import inflection inflection.dasherize('puni_puni') - humanize
import inflection inflection.humanize('employee_salary') - ordinalize
import inflection inflection.ordinalize(1)
- tableize
import inflection inflection.tableize('fancy_category') - titleize
import inflection inflection.titleize('man from the boondocks') - transliterate
import inflection inflection.transliterate('你好')
Quickstart
import inflection
# Convert a snake_case string to CamelCase
result_camel = inflection.camelize("my_variable_name")
print(f"CamelCase: {result_camel}")
# Pluralize a word
result_plural = inflection.pluralize("mouse")
print(f"Plural: {result_plural}")
# Singularize a word
result_singular = inflection.singularize("data")
print(f"Singular: {result_singular}")