Inflection

Inflection is a string transformation library. It singularizes and pluralizes English words, and transforms strings from CamelCase to underscored_string. Inflection is a port of Ruby on Railsinflector to Python.

Installation

Use pip to install from PyPI:

pip install Inflection

Contributing

To contribute to Inflector create a fork on GitHub. Clone your fork, make some changes, and submit a pull request.

API Documentation

inflection.camelize(string, uppercase_first_letter=True)[source]

Convert strings to CamelCase.

Examples:

>>> camelize("device_type")
"DeviceType"
>>> camelize("device_type", False)
"deviceType"

camelize() can be though as a inverse of underscore(), although there are some cases where that does not hold:

>>> camelize(underscore("IOError"))
"IoError"
Parameters:uppercase_first_letter – if set to True camelize() converts strings to UpperCamelCase. If set to False camelize() produces lowerCamelCase. Defaults to True.
inflection.dasherize(word)[source]

Replace underscores with dashes in the string.

Example:

>>> dasherize("puni_puni")
"puni-puni"
inflection.humanize(word)[source]

Capitalize the first word and turn underscores into spaces and strip a trailing "_id", if any. Like titleize(), this is meant for creating pretty output.

Examples:

>>> humanize("employee_salary")
"Employee salary"
>>> humanize("author_id")
"Author"
inflection.ordinal(number)[source]

Return the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Examples:

>>> ordinal(1)
"st"
>>> ordinal(2)
"nd"
>>> ordinal(1002)
"nd"
>>> ordinal(1003)
"rd"
>>> ordinal(-11)
"th"
>>> ordinal(-1021)
"st"
inflection.ordinalize(number)[source]

Turn a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Examples:

>>> ordinalize(1)
"1st"
>>> ordinalize(2)
"2nd"
>>> ordinalize(1002)
"1002nd"
>>> ordinalize(1003)
"1003rd"
>>> ordinalize(-11)
"-11th"
>>> ordinalize(-1021)
"-1021st"
inflection.parameterize(string, separator='-')[source]

Replace special characters in a string so that it may be used as part of a ‘pretty’ URL.

Example:

>>> parameterize(u"Donald E. Knuth")
'donald-e-knuth'
inflection.pluralize(word)[source]

Return the plural form of a word.

Examples:

>>> pluralize("post")
"posts"
>>> pluralize("octopus")
"octopi"
>>> pluralize("sheep")
"sheep"
>>> pluralize("CamelOctopus")
"CamelOctopi"
inflection.singularize(word)[source]

Return the singular form of a word, the reverse of pluralize().

Examples:

>>> singularize("posts")
"post"
>>> singularize("octopi")
"octopus"
>>> singularize("sheep")
"sheep"
>>> singularize("word")
"word"
>>> singularize("CamelOctopi")
"CamelOctopus"
inflection.titleize(word)[source]

Capitalize all the words and replace some characters in the string to create a nicer looking title. titleize() is meant for creating pretty output.

Examples:

>>> titleize("man from the boondocks")
"Man From The Boondocks"
>>> titleize("x-men: the last stand")
"X Men: The Last Stand"
>>> titleize("TheManWithoutAPast")
"The Man Without A Past"
>>> titleize("raiders_of_the_lost_ark")
"Raiders Of The Lost Ark"
inflection.transliterate(string)[source]

Replace non-ASCII characters with an ASCII approximation. If no approximation exists, the non-ASCII character is ignored. The string must be unicode.

Examples:

>>> transliterate(u'älämölö')
'alamolo'
>>> transliterate(u'Ærøskøbing')
'rskbing'
inflection.underscore(word)[source]

Make an underscored, lowercase form from the expression in the string.

Example:

>>> underscore("DeviceType")
"device_type"

As a rule of thumb you can think of underscore() as the inverse of camelize(), though there are cases where that does not hold:

>>> camelize(underscore("IOError"))
"IoError"

Changelog

Here you can see the full list of changes between each Inflection release.

0.1.2 (2012-03-13)

  • Added Python 2.5 support.

0.1.1 (2012-02-24)

  • Fixed some files not included in the distribution package.

0.1.0 (2012-02-24)

  • Initial public release

License

Copyright (C) 2012 Janne Vanhala Copyright (c) 2005-2012 David Heinemeier Hansson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project Versions

Table Of Contents

This Page