qtils.string_utils module

String Utilities

Common string transformation and conversation utilities. These are based on taken from github gists and stack overflow answers. See notes of the origins in the specific functions.

qtils.string_utils.camelize(value, uppercase_first_letter=True)

Convert values to CamelCase.

See origin here

Parameters:
  • value (str) – string to convert
  • uppercase_first_letter (bool) – 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')
'DeviceType'
>>> camelize('device_Type')
'DeviceType'
>>> camelize('device type')
'DeviceType'
>>> camelize('device_type', False)
'deviceType'

camelize() can be though as a inverse of underscorize(), but not in every case:

>>> camelize(underscorize("IOError"))
'IoError'
>>> camelize(underscorize('SomeABTest'))
'SomeAbTest'
qtils.string_utils.underscorize(value)

Make an underscore, lowercase form from input

See origin here

Parameters:value (str) – Value to convert

Examples

>>> underscorize('device')
'device'
>>> underscorize('DeviceType')
'device_type'
>>> underscorize('deviceType')
'device_type'
>>> underscorize('device-type')
'device_type'
>>> underscorize('device-Type')
'device_type'
>>> underscorize('DeviceTypeWithALotOfThings')
'device_type_with_a_lot_of_things'

underscorize() can be though as a inverse of camelize(), but not in every case:

>>> camelize(underscorize("IOError"))
'IoError'
>>> camelize(underscorize('SomeABTest'))
'SomeAbTest'
qtils.string_utils.titleize(value)

Convert strings to ‘Title String’

Parameters:value (str) – Value to convert

Examples

>>> titleize('device_type')
'Device Type'
>>> titleize('device-type')
'Device Type'
>>> titleize('deviceType')
'Device Type'
>>> titleize('device Type')
'Device Type'
qtils.string_utils.firstline(value)

Returns the first line of a string

>>> text = "This is a long\nmulti\nline text with\nmany line breaks"
>>> firstline(text)
'This is a long'