Skip to main content

sc_crawler.str_utils

String helpers, e.g. for wrapping, pluralizing and converting to snake case.

Functions:

  • wrap – Wrap string between before/after strings (default to spaces) if not empty.
  • space_after – Add space after string if not empty.
  • snake_case – Convert CamelCase to snake_case.
  • plural – Super basic implementation of pluralizing an English word.
  • extract_last_number – Extract the last non-negative number from a string.

func wrap str_utils.py:7–15⁠ (opens in a new tab)​

wrap(text, before=' ', after=' ')

Wrap string between before/after strings (default to spaces) if not empty.

Parameters:

func space_after str_utils.py:18–20⁠ (opens in a new tab)​

space_after(text)

Add space after string if not empty.

func snake_case str_utils.py:24–37⁠ (opens in a new tab)​

snake_case(text)

Convert CamelCase to snake_case.

Parameters:

Returns:

Examples:

>>> snake_case('DescriptionToComment')
'description_to_comment'

func plural str_utils.py:41–63⁠ (opens in a new tab)​

plural(text)

Super basic implementation of pluralizing an English word.

Note that grammar exceptions are not handled, so better to use a proper NLP method for real use-cases.

Parameters:

Returns:

Examples:

>>> plural('dog')
'dogs'
>>> plural('boy') # :facepalm:
'boies'

func extract_last_number str_utils.py:66–82⁠ (opens in a new tab)​

extract_last_number(text)

Extract the last non-negative number from a string.

Parameters:

Returns:

Examples:

>>> extract_last_number("foo42")
42.0
>>> extract_last_number("foo24.42bar")
24.42