Skip to main content

BAI2 Parser

Project description

bai2

Python module for parsing and writing BAI2 files.

The library is not production ready at the moment as we don’t have enough data to test against, please help us improve it.

Requirements

Only Python 2.7 and Python 3.3+ are supported.

Installation

pip install bai2

Usage

To use bai2 in a project

>>> from bai2 import bai2

>>> # parse from a file
>>> with open(<file-path>) as f:
>>>     bai2_file = bai2.parse_from_file(f)

>>> # parse from a string
>>> bai2_file = bai2.parse_from_string(<bai2_as_string>)

>>> # parse from lines
>>> bai2_file = bai2.parse_from_lines(<bai2_as_lines>)

The parse_from_* methods return a bai2.models.Bai2File object which can be used to inspect the parsed data.

To write a BAI2 file:

>>> from bai2 import bai2
>>> from bai2 import models

>>> bai2_file = models.Bai2File()
>>> bai2_file.header.sender_id = 'EGBANK'

>>> bai2_file.children.append(models.Group())

>>> transactions = [models.TransactionDetail(amount=100)]
>>> bai2_file.children[0].children.append(models.Account(children=transactions))

>>> # write to string
>>> output = bai2.write(bai2_file)

Models

Models structure:

Bai2File
    Bai2FileHeader
    Group
        GroupHeader
        Account
            AccountIdentifier
            TransactionDetail
            AccountTrailer
        GroupTrailer
    Bai2FileTrailer

Section models define a header, a trailer and a list of children whilst single models define properties matching the bai2 fields.

Each Model has a rows property with the original rows from the BAI2 file.

Exceptions

The parse method might raise 3 exceptions:

  1. ParsingException: when the file contains an error and the library can’t interpret the data

  2. NotSupportedYetException: when the library doesn’t support the feature yet

  3. IntegrityException: when the control totals or the number of objects reported in the trailers don’t match the ones in the file.

Incongruences

We’ve noticed that different banks implement the specs in slightly different ways and the parse method might therefore raise an ParsingException. It is expected to work correctly with files produced by NatWest, RBS, and JP Morgan.

We don’t know yet how to deal with these cases as we don’t have access to many bai2 files so we can’t test it as we would like.

Please let me know if this happens to you.

Development

https://travis-ci.org/ministryofjustice/bai2.svg?branch=master

Please report bugs and open pull requests on GitHub.

Use python setup.py test or tox to run all tests.

Distribute a new version to PyPi by updating the __version__ string in bai2 and run python setup.py sdist bdist_wheel upload.

History

0.6.0 (2019-09-18)

  • Fix regular expression escaping

  • Add python 3.7 testing

0.5.0 (2018-03-05)

  • Updated packaging details and improved python version compatibility

0.1.0 (2015-08-06)

  • WIP

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bai2-0.6.0.tar.gz (32.1 kB view details)

Uploaded Source

Built Distribution

bai2-0.6.0-py2.py3-none-any.whl (18.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file bai2-0.6.0.tar.gz.

File metadata

  • Download URL: bai2-0.6.0.tar.gz
  • Upload date:
  • Size: 32.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for bai2-0.6.0.tar.gz
Algorithm Hash digest
SHA256 66960a0660aa4544b6c59c1014e06af12489bae1be1fd5b31bf6e753e88bc14a
MD5 6d78173ccc8e4344e3b277f4d5e5e780
BLAKE2b-256 730a4ffa78b58bc135d51da09f297d979aebd1d232e7883a534c59cfb82ce9dc

See more details on using hashes here.

File details

Details for the file bai2-0.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: bai2-0.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for bai2-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9cea71318b7e66b86ba22b17f51ef7ae3bf4c8736449061164d7cae70aae3193
MD5 0d8cc515d88b70d3682dc4cf1769f960
BLAKE2b-256 6212bd225faed0e0f3362e56a71ed4e749fe9f3ff1820c602eff2b0e283e9e49

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page