upsies.utils.string

String formatting and parsing

Functions

upsies.utils.string.capitalize(text)[source]

Capitalize each word in text

Unlike str.title(), only words at in front of a space or at the beginning of text are capitalized.

upsies.utils.string.decode_nfo(bytes, *, strip=False)[source]

Return decoded bytes

Try to decode as UTF-8 first. If that fails, decode as CP437 and replace invalid characters with “�” (U+FFFD).

All line breaks (e.g. CR+LF) are converted to “n”.

Parameters:

strip (bool) –

Whether to remove whitespace at the beginning and end

Note

To preserve ASCII art, spaces at the beginning of the first non-empty line are kept.

upsies.utils.string.evaluate_fstring(template, **variables)[source]

Deferred f-string evaluation

Unlike "Bar: {foo}".format(foo="bar"), this function will also evaluate function calls inside the curly braces.

Warning

Because template is passed to eval(), this function must NEVER be called with code that comes from the user. Only use this function to evaluate hardcoded strings.

Parameters:
  • template (str) – Unevaluated f-string (i.e. a normal string containing curly braces without the f in front of the quote)

  • variables (dict) – dict instance that maps names that can be used in template in curly braces to objects that replace the curly braced name.

Example:

>>> template = 'Evaluate {x}: {sum((1, 2, 3)) * 2}'
>>> evaluate_fstring(template, x='this')
Evaluate this: 12
>>> evaluate_fstring(template, x='that')
Evaluate that: 12

References:

upsies.utils.string.get_vimeo_id(text)[source]

Same as get_youtube_id() but for Vimeo

upsies.utils.string.get_youtube_id(text)[source]

Return YouTube video ID or None

The matching is very lenient and does not require an URL. Anything that reasonably looks like it could be a YouTube ID will match, e.g. “yt/_d34d-_B33F”. If text is an arbitrary string, make sure it’s an URL first.

upsies.utils.string.normalize_comma_separated_list(text, *, lower=False, sort=False)[source]

Split and re-join text at ,

Parameters:
  • lower (bool) – Whether text is lower-cased

  • sort (bool) – Whether items are sorted

upsies.utils.string.read_nfo(path, *, strip=False)[source]

Recursively search directory for *.nfo files and read the first one found

path may also be an nfo file.

The nfo file is decoded with decode_nfo().

Files larger than 1 MiB are ignored.

If no *.nfo file is found, return None.

Parameters:

strip (bool) – See decode_nfo()

Raises:

ContentError – if the nfo file is not readable

upsies.utils.string.remove_prefix(string, prefix)[source]
upsies.utils.string.remove_suffix(string, suffix)[source]
upsies.utils.string.star_rating(rating, max_rating=10)[source]

Return star rating string with the characters “★” (U+2605), “⯪” (U+2BEA) and “☆” (U+2605)

Parameters:
  • rating (float,int) – Number between 0 and max_rating

  • max_rating (float,int) – Maximum rating

Classes

class upsies.utils.string.CaseInsensitiveString[source]

Bases: str

str that ignores case when compared or sorted