csb43.utils

Note

license: GNU Lesser General Public License v3.0 (see LICENSE)

exception csb43.utils.Csb43Exception(message: str | bytes)
exception csb43.utils.Csb43Warning(message: str | bytes)
csb43.utils.CurrencyLite(*args, **kwargs)

Deprecated since version 0.10.0: use utils.currency.CurrencyLite

csb43.utils.b_left_pad(bvalue: bytes, n: int, fill=b' ') bytes

pad with fill chars at the leftside and return a record of n chars

csb43.utils.b_right_pad(bvalue: bytes, n: int, fill=b' ') bytes

pad with fill chars at the rightside and return a record of n chars

csb43.utils.check_strict(pattern: str | bytes)

Note

decorator

Parameters:
  • pattern (basestring) – pattern description using regular expressions

  • field – variable to be checked

  • strict (bool) – treat exceptions as warnings if False

Raises:

Csb43Exception if field doesn’t match pattern and strict is True

csb43.utils.check_string(pattern='', field='', strict=True, **csbexc)
Parameters:
  • pattern (basestring) – pattern description using regular expressions

  • field – variable to be checked

  • strict (bool) – treat exceptions as warnings if False

Raises:

Csb43Exception if field doesn’t match pattern and strict is True

csb43.utils.currency2raw(value: float | int | Decimal, decimal: int = 2) tuple[int, str]

Convert a real to the CSB amount format

Args:

value (float) – quantity as a real number decimal (int) – number of digits reserved for decimal numbers

Return:

tuple of absolute amount and debit flag

Examples:

>>> currency2raw(-123.456)
(12345, '1')
>>> currency2raw(123.45)
(12345, '2')
csb43.utils.date2raw(value: datetime | date, yearFirst=True) str

Convert a datetime.date / datetime.datetime object to a CSB formatted date

Args:

value (datetime.datetime, datetime.date) – datetime object yearFirst (bool) – if False, consider the CSB format is DDMMYY instead of YYMMDD

Return:

(str) the CSB date

Examples:

>>> a = raw2date('020301')
>>> date2raw(a)
'020301'
csb43.utils.raiseCsb43Exception(value='', strict=False, silent=False, line: int | None = None, **kwargs)

raise a Csb43Exception or print the exception’s message to standard error

Parameters:
  • value – message of the exception

  • strict – print to standard error instead of raising an exception if not strict

Raises:

Csb43Exception

csb43.utils.raw2currency(value: str | bytes | int, decimal: int = 2, debit: str | bytes = '2') Decimal

Format the CSB composite type for amounts as a real number

Args:

value (long or str) – absolute amount without decimal separator decimal (int) – number of digits reserved for decimal numbers debit (‘1’,’2’) – ‘1’ debit, ‘2’ credit

Return:

(float) the amount as a real number

Examples:

>>> raw2currency('123456')
Decimal('1234.56')
>>> raw2currency('12345',debit='1')
Decimal('-123.45')
csb43.utils.raw2date(value: str, yearFirst=True) date

Convert the CSB date format to a datetime.date object

Args:

value (str) – date using the CSB format yearFirst (bool) – if False, consider the CSB format is DDMMYY instead of YYMMDD

Return:

(datetime.date) the date object

Examples:

>>> raw2date('020301')
datetime.date(2002, 3, 1)
class csb43.utils.currency.AnyCurrency(*args, **kwargs)

protocol for a currency object compatible with pycountry

class csb43.utils.currency.CurrencyLite(alpha_3: str | None, numeric: str | None)

cheap alternative to pycountry Currency

csb43.utils.currency.currency_from_iso_code(code: str) AnyCurrency | None

Return AnyCurrency from an ISO 4217 numeric or letter code Return None if no code was found

csb43.utils.currency.currency_from_iso_letter(alpha_3: str) AnyCurrency

Return a currency object from an alpha_3 ISO 4217 code.

Deprecated since version 0.10.0: use utils.currency.currency_from_iso_letter

csb43.utils.currency.currency_from_iso_number(numeric: str | None) AnyCurrency | None

Return a currency object from a numeric ISO 4217 code.

Deprecated since version 0.10.0: use utils.currency.currency_from_iso_number

csb43.utils.currency.is_currency(obj) bool

return True is obj is a currency object

Deprecated since version 0.10.0: use utils.currency.is_currency

csb43.utils.currency.simplify_currency(obj: AnyCurrency) CurrencyLite

convert currency to CurrencyLite

csb43.utils.currency.yaml_currency_representer(dumper, obj: AnyCurrency)

a yaml representer for AnyCurrency

functions for tabulated strings manipulation

csb43.utils.tabulated.adjust_table_width(data: Sequence[str], delim='||', gap=1, indent=0) Iterator[str]

adjust width for columns marked by sep