csb43.utils

Note

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

class csb43.utils.BytesMixin
exception csb43.utils.Csb43Exception(message: str | bytes)
exception csb43.utils.Csb43Warning(message: str | bytes)
class csb43.utils.CurrencyLite(alpha_3, numeric)
class csb43.utils.Message(message: str | bytes)
csb43.utils.b_left_pad(bvalue: bytes, n: int, fill=b' ') bytes
csb43.utils.b_right_pad(bvalue: bytes, n: int, fill=b' ') bytes
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.currencyISO(numeric: str | None)
Parameters:

code (str) – a ISO 4217 numeric code

Return type:

pycountry.db.Currency object from its numeric code

csb43.utils.currencyISOByLetter(alpha_3: str)
Parameters:

code (str) – a ISO 4217 numeric code

Return type:

pycountry.db.Currency object from its numeric code

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.export_currency_code(value) str
csb43.utils.export_date(value) str
csb43.utils.export_decimal(value: Decimal, fallback: T | None = None) Decimal | float | str | T
csb43.utils.isCurrency(obj) bool
csb43.utils.nullable(f)
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)