mt940.models module

Data models returned by the MT940 parser.

The parser produces a Transactions collection (statement-level data plus a sequence of Transaction objects). The remaining classes are the value types stored on them: Amount, Balance, Date, DateTime and FixedOffset. They accept the string fields found in raw MT940 data and coerce them to native Python types.

class mt940.models.Model[source]

Bases: object

Base class for MT940 models, providing a uniform repr.

class mt940.models.FixedOffset(offset=0, name=None)[source]

Bases: tzinfo

Fixed time offset based on the Python docs Source: https://docs.python.org/2/library/datetime.html#tzinfo-objects

>>> offset = FixedOffset(60)
>>> offset.utcoffset(None).total_seconds()
3600.0
>>> offset.dst(None)
datetime.timedelta(0)
>>> offset.tzname(None)
'60'
Parameters:
utcoffset(dt)[source]

Return the fixed offset east of UTC.

Parameters:

dt (datetime | None)

Return type:

timedelta

dst(dt)[source]

Return a zero DST adjustment (fixed offsets have no DST).

Parameters:

dt (datetime | None)

Return type:

timedelta

tzname(dt)[source]

Return the offset’s name.

Parameters:

dt (datetime | None)

Return type:

str

class mt940.models.DateTime(*args, **kwargs)[source]

Bases: datetime, Model

Just a regular datetime object which supports dates given as strings

>>> DateTime(
...     year='2000',
...     month='1',
...     day='2',
...     hour='3',
...     minute='4',
...     second='5',
...     microsecond='6',
... )
DateTime(2000, 1, 2, 3, 4, 5, 6)
>>> DateTime(
...     year='123',
...     month='1',
...     day='2',
...     hour='3',
...     minute='4',
...     second='5',
...     microsecond='6',
... )
DateTime(2123, 1, 2, 3, 4, 5, 6)
>>> DateTime(2000, 1, 2, 3, 4, 5, 6)
DateTime(2000, 1, 2, 3, 4, 5, 6)
>>> DateTime(
...     year='123',
...     month='1',
...     day='2',
...     hour='3',
...     minute='4',
...     second='5',
...     microsecond='6',
...     tzinfo=FixedOffset('60'),
... )
DateTime(2123, 1, 2, 3, 4, 5, 6, tzinfo=<mt940.models.FixedOffset ...>)
Parameters:
  • year (str) – Year (0-100), will automatically add 2000 when needed

  • month (str) – Month

  • day (str) – Day

  • hour (str) – Hour

  • minute (str) – Minute

  • second (str) – Second

  • microsecond (str) – Microsecond

  • tzinfo (datetime.tzinfo) – Timezone information. Overwrites offset

  • offset (str) – Timezone offset in minutes, generates a tzinfo object with the given offset if no tzinfo is available.

  • args (Any)

  • kwargs (Any)

Return type:

DateTime

class mt940.models.Date(*args, **kwargs)[source]

Bases: date, Model

Just a regular date object which supports dates given as strings

>>> Date(year='2000', month='1', day='2')
Date(2000, 1, 2)
>>> Date(year='123', month='1', day='2')
Date(2123, 1, 2)
Parameters:
  • year (str) – Year (0-100), will automatically add 2000 when needed

  • month (str) – Month

  • day (str) – Day

  • args (Any)

  • kwargs (Any)

Return type:

Date

class mt940.models.Amount(amount, status, currency=None, **kwargs)[source]

Bases: Model

Amount object containing currency and amount

Parameters:
  • amount (str) – Amount using either a , or a . as decimal separator

  • status (str) – Either C or D for credit or debit respectively

  • currency (str) – A 3 letter currency (e.g. EUR)

  • kwargs (Any)

>>> Amount('123.45', 'C', 'EUR')
<123.45 EUR>
>>> Amount('123.45', 'D', 'EUR')
<-123.45 EUR>
class mt940.models.SumAmount(*args, number, **kwargs)[source]

Bases: Amount

An Amount that also tracks how many entries it sums.

Used for the :90D:/:90C: tags, which report the total amount and the number of debit/credit entries that make it up.

Parameters:
  • args (Any)

  • number (int)

  • kwargs (Any)

class mt940.models.Balance(status=None, amount=None, date=None, **kwargs)[source]

Bases: Model

Parse balance statement

Parameters:
  • status (str) – Either C or D for credit or debit respectively

  • amount (Amount | str | None) – Object containing the amount and currency or amount string

  • date (Date | None) – The balance date

  • kwargs (Any)

>>> balance = Balance('C', '0.00', Date(2010, 7, 22))
>>> balance.status
'C'
>>> balance.amount.amount
Decimal('0.00')
>>> isinstance(balance.date, Date)
True
>>> balance.date.year, balance.date.month, balance.date.day
(2010, 7, 22)
>>> Balance()
<None @ None>
class mt940.models.Transaction(transactions, data=None)[source]

Bases: Model

A single statement transaction and its parsed fields.

Holds a back-reference to its owning Transactions collection and a data dictionary with the parsed tag fields (amount, dates, references, purpose, …). Field availability depends on the source bank and tags.

Parameters:
update(data)[source]

Update transaction data with provided data dictionary.

Parameters:

data (dict[str, Any] | None) – Data to update the transaction with.

Return type:

None

class mt940.models.Transactions(processors=None, tags=None, transaction_boundary=None)[source]

Bases: Sequence[Transaction]

Collection of Transaction objects with global properties such as begin and end balance

Parameters:
DEFAULT_PROCESSORS: ClassVar[dict[str, list[Callable[[...], dict[str, Any]]]]] = {'post_account_identification': [], 'post_available_balance': [], 'post_closing_balance': [], 'post_date_time_indication': [], 'post_final_closing_balance': [], 'post_final_opening_balance': [], 'post_floor_limit_indicator': [], 'post_forward_available_balance': [], 'post_intermediate_closing_balance': [], 'post_intermediate_opening_balance': [], 'post_non_swift': [], 'post_opening_balance': [], 'post_related_reference': [], 'post_statement': [<function date_cleanup_post_processor>, <function transactions_to_transaction.<locals>._transactions_to_transaction>], 'post_statement_number': [], 'post_sum_credit_entries': [], 'post_sum_debit_entries': [], 'post_transaction_details': [<function transaction_details_post_processor>], 'post_transaction_reference_number': [], 'pre_account_identification': [], 'pre_available_balance': [], 'pre_closing_balance': [], 'pre_date_time_indication': [], 'pre_final_closing_balance': [], 'pre_final_opening_balance': [], 'pre_floor_limit_indicator': [], 'pre_forward_available_balance': [], 'pre_intermediate_closing_balance': [], 'pre_intermediate_opening_balance': [], 'pre_non_swift': [], 'pre_opening_balance': [], 'pre_related_reference': [], 'pre_statement': [<function date_fixup_pre_processor>], 'pre_statement_number': [], 'pre_sum_credit_entries': [], 'pre_sum_debit_entries': [], 'pre_transaction_details': [], 'pre_transaction_reference_number': []}
property currency: str | None

The statement currency, derived from the first available balance.

Returns None when no balance or floor-limit carrying a currency has been parsed yet.

classmethod defaultTags()[source]

Deprecated alias for default_tags().

Return type:

Mapping[int | str, Tag]

static default_tags()[source]

Return the built-in tag parsers keyed by tag id.

Return type:

Mapping[int | str, Tag]

parse(data)[source]

Parses mt940 data, expects a string with data

Parameters:

data (str) – The MT940 data

Returns:

list of Transaction

Return type:

list[Transaction]

static strip(lines)[source]

Strip extraneous whitespace and lines from list of strings.

Parameters:

lines (list[str]) – List of lines to strip.

Returns:

List of cleaned lines.

Return type:

list[str]

classmethod normalize_tag_id(tag_id)[source]

Normalize a tag ID to int if possible, or return as string.

Parameters:

tag_id (str) – The tag ID to normalize.

Returns:

Normalized tag ID as integer or string.

Return type:

int | str

sanitize_tag_id_matches(matches)[source]

Sanitize the list of tag ID matches.

Parameters:

matches (list[re.Match[str]]) – List of regex match objects for tag IDs.

Returns:

List of valid match objects for recognized tag IDs.

Return type:

list[re.Match[str]]

class mt940.models.TransactionsAndTransaction(processors=None, tags=None, transaction_boundary=None)[source]

Bases: Transactions, Transaction

Subclass of both Transactions and Transaction for scope definitions.

This is useful for the non-swift data for example which can function both as details for a transaction and for a collection of transactions.

Parameters: