hbutils.string.inflection

Overview:

This module provides useful utilities for word inflections, including functions for pluralization, singularization, camelization, and other string transformations. It is extended based on the jpvanhal/inflection library.

Functions:
  • camelize: Convert strings to CamelCase.

  • dasherize: Replace underscores with dashes in the string.

  • humanize: Capitalize the first word and turn underscores into spaces.

  • ordinal: Return the suffix for ordinal numbers.

  • ordinalize: Turn a number into an ordinal string.

  • parameterize: Replace special characters in a string for URL-friendly format.

  • pluralize: Return the plural form of a word.

  • singularize: Return the singular form of a word.

  • tableize: Create the name of a table from a model name.

  • titleize: Capitalize all words and replace some characters for a nicer looking title.

  • transliterate: Replace non-ASCII characters with ASCII approximations.

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

Note

This module includes predefined rules for plural and singular forms, as well as a list of uncountable words.

camelize

hbutils.string.inflection.camelize(string: str, uppercase_first_letter: bool = True) → str[source]

Convert strings to CamelCase.

Parameters:
  • string (str) – Original string to be converted

  • uppercase_first_letter (bool) – If True, converts to UpperCamelCase; if False, to lowerCamelCase

Returns:

The camelized string

Return type:

str

This function converts strings to CamelCase. If uppercase_first_letter is True (default), it produces UpperCamelCase. If False, it produces lowerCamelCase.

Examples:
>>> camelize("device_type")
'DeviceType'
>>> camelize("device_type", False)
'deviceType'
Note:

camelize can be thought of as a inverse of underscore, although there are some cases where that does not hold: >>> camelize(underscore(“IOError”)) ‘IoError’

dasherize

hbutils.string.inflection.dasherize(word: str) → str[source]

Replace underscores with dashes in the string.

Parameters:

word (str) – Original word to be dasherized

Returns:

The dasherized string

Return type:

str

This function replaces all underscores in the input string with dashes.

Example:
>>> dasherize("puni_puni")
'puni-puni'

humanize

hbutils.string.inflection.humanize(word: str) → str[source]

Capitalize the first word and turn underscores into spaces and strip a trailing “_id”, if any.

Parameters:

word (str) – Original word to be humanized

Returns:

The humanized string

Return type:

str

This function is meant for creating pretty output. It capitalizes the first word, replaces underscores with spaces, and removes a trailing “_id” if present.

Examples:
>>> humanize("employee_salary")
'Employee salary'
>>> humanize("author_id")
'Author'

ordinal

hbutils.string.inflection.ordinal(number: int) → str[source]

Return the suffix that should be added to a number to denote the position in an ordered sequence.

Parameters:

number (int) – The number for which to generate the ordinal suffix

Returns:

The ordinal suffix (e.g., ‘st’, ‘nd’, ‘rd’, ‘th’)

Return type:

str

This function returns the appropriate suffix for ordinal numbers (1st, 2nd, 3rd, 4th, etc.).

Examples:
>>> ordinal(1)
'st'
>>> ordinal(2)
'nd'
>>> ordinal(1002)
'nd'
>>> ordinal(1003)
'rd'
>>> ordinal(-11)
'th'
>>> ordinal(-1021)
'st'

ordinalize

hbutils.string.inflection.ordinalize(number: int) → str[source]

Turn a number into an ordinal string used to denote the position in an ordered sequence.

Parameters:

number (int) – The number to be ordinalized

Returns:

The ordinalized number as a string

Return type:

str

This function converts a number into its ordinal form (1st, 2nd, 3rd, 4th, etc.).

Examples:
>>> ordinalize(1)
'1st'
>>> ordinalize(2)
'2nd'
>>> ordinalize(1002)
'1002nd'
>>> ordinalize(1003)
'1003rd'
>>> ordinalize(-11)
'-11th'
>>> ordinalize(-1021)
'-1021st'

parameterize

hbutils.string.inflection.parameterize(string: str, separator: str = '-') → str[source]

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

Parameters:
  • string (str) – Original string to be parameterized

  • separator (str) – Separator to use between words (default is ‘-‘)

Returns:

The parameterized string

Return type:

str

This function replaces special characters and spaces with the specified separator, making the string suitable for use in URLs.

Example:
>>> parameterize(u"Donald E. Knuth")
'donald-e-knuth'

pluralize

hbutils.string.inflection.pluralize(word: str) → str[source]

Return the plural form of a word.

Parameters:

word (str) – Original word to be pluralized

Returns:

The pluralized word

Return type:

str

This function returns the plural form of the given word based on predefined rules.

Examples:
>>> pluralize("posts")
'posts'
>>> pluralize("octopus")
'octopi'
>>> pluralize("sheep")
'sheep'
>>> pluralize("CamelOctopus")
'CamelOctopi'

singularize

hbutils.string.inflection.singularize(word: str) → str[source]

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

Parameters:

word (str) – Original word to be singularized

Returns:

The singularized word

Return type:

str

This function returns the singular form of the given word based on predefined rules.

Examples:
>>> singularize("posts")
'post'
>>> singularize("octopi")
'octopus'
>>> singularize("sheep")
'sheep'
>>> singularize("word")
'word'
>>> singularize("CamelOctopi")
'CamelOctopus'

tableize

hbutils.string.inflection.tableize(word: str) → str[source]

Create the name of a table like Rails does for models to table names.

Parameters:

word (str) – Original word to be tableized

Returns:

The tableized word

Return type:

str

This method uses the pluralize method on the last word in the string after underscoring it.

Examples:
>>> tableize('RawScaledScorer')
'raw_scaled_scorers'
>>> tableize('egg_and_ham')
'egg_and_hams'
>>> tableize('fancyCategory')
'fancy_categories'

titleize

hbutils.string.inflection.titleize(word: str) → str[source]

Capitalize all the words and replace some characters in the string to create a nicer looking title.

Parameters:

word (str) – Original word to be titleized

Returns:

The titleized string

Return type:

str

This function is meant for creating pretty output by capitalizing all words and replacing certain characters for improved readability.

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'

transliterate

hbutils.string.inflection.transliterate(string: str) → str[source]

Replace non-ASCII characters with an ASCII approximation.

Parameters:

string (str) – Original string to be transliterated

Returns:

The transliterated string

Return type:

str

If no approximation exists, the non-ASCII character is ignored. The input string must be unicode.

Examples:
>>> transliterate('älämölö')
'alamolo'
>>> transliterate('Ærøskøbing')
'rskbing'

underscore

hbutils.string.inflection.underscore(word: str) → str[source]

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

Parameters:

word (str) – Original word to be underscored

Returns:

The underscored string

Return type:

str

This function converts CamelCase or space-separated words to lowercase, underscore-separated words.

Example:
>>> underscore("DeviceType")
'device_type'
Note:

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'