mt940.utils module

Small, dependency-free helpers shared across the package.

Contains coalesce() (first non-None value) and join_lines() (whitespace-aware line joining) together with the Strip flag enum that configures the latter.

mt940.utils.coalesce(*args)[source]

Return the first non-None argument.

Examples

>>> coalesce()
>>> coalesce(0, 1)
0
>>> coalesce(None, 0)
0
Returns:

The first non-None argument or None if all are None.

Parameters:

args (T | None)

Return type:

T | None

class mt940.utils.Strip(*values)[source]

Bases: IntFlag

Enumeration of options for stripping whitespace in strings.

NONE = 0

Do not strip any whitespace.

LEFT = 1

Strip leading whitespace.

RIGHT = 2

Strip trailing whitespace.

BOTH = 3

Strip both leading and trailing whitespace.

mt940.utils.join_lines(string, strip=<Strip.BOTH: 3>)[source]

Join strings together and strip whitespace in between if needed.

Parameters:
  • string (str) – The string with lines to join.

  • strip (Strip) – Strip options from the Strip enum.

Return type:

str

>>> join_lines('  line1\nline2  \n line3 ')
'line1line2line3'
>>> join_lines('  line1\nline2  \n line3 ', strip=Strip.LEFT)
'line1line2  line3 '
>>> join_lines('  line1\nline2  \n line3 ', strip=Strip.RIGHT)
'  line1line2 line3'
>>> join_lines('  line1\nline2  \n line3 ', strip=Strip.NONE)
'  line1line2  line3 '
Returns:

The joined string.

Parameters:
Return type:

str