hbutils.string.inflection

Overview:

Useful utilities for word inflections.

Extended based on jpvanhal/inflection.

camelize

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

Convert strings to CamelCase.

Parameters:
  • string – Original string.

  • uppercase_first_letter – if set to True camelize() converts strings to UpperCamelCase. If set to False camelize() produces lowerCamelCase. Defaults to True.

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]
Overview:

Replace underscores with dashes in the string.

Parameters:

word – Original word.

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

humanize

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

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.

Parameters:

word – Original word.

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

ordinal

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

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

Parameters:

number – Int format number.

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]
Overview:

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

Parameters:

number – Int format number.

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]
Overview:

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

Parameters:
  • string – Original string.

  • separator – Separator of parameter words.

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

pluralize

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

Return the plural form of a word.

Parameters:

word – Original word.

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

singularize

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

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

Parameters:

word – Original word.

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]
Overview:

Create the name of a table like Rails does for models to table names. This method uses the pluralize() method on the last word in the string.

Parameters:

word – Original word.

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]
Overview:

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

Parameters:

word – Original word.

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]
Overview:

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

Parameters:

string – Original string.

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

underscore

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

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

Parameters:

word – Original word.

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'