Giter VIP home page Giter VIP logo

python-cfonb's Introduction

Python-CFONB

Introduction

The CFONB format is an old file format for banking interchange, made by the CFONB (Comité français d'organisation et de normalisation bancaires). It is still used today to retrieve bank statements or make transfer orders. The specifications of the format can be found on the CFONB website.

python-cfonb is a pure Python lib to read or write CFONB files, distributed under the LGPL license:

  • import method should read file-like object (with IOString or real plain text file)
  • export method to export Python object in CFO file
  • format checker according bank specification: special char, mandatory fields, etc.

The first two usecase are : read bank statements, and make transfer orders

Statement Parser

You can read a statement like this:

>>> from os.path import join
>>> statement_file = open(join('cfonb', 'tests', 'bank_statement.cfo'))
>>> from cfonb import StatementReader
>>> reader = StatementReader()
>>> result = reader.parse(statement_file)
>>> statement = result[0]

The statement has a header and a footer, which are both statement rows:

>>> statement.header
<cfonb.parser.common.Row object at ...>
>>> statement.footer
<cfonb.parser.common.Row object at ...>

A row can be read as a list:

>>> statement.header.as_list()
['01', '30002', '    ', '00447', ...]

Or as a dict:

>>> statement.header.as_dict()
{'bank_code': '30002', 'nb_of_dec': ' ', '_5': ...}

Or as an object:

>>> header = statement.header.as_obj()
>>> header.bank_code
'30002'

The statement lines between the header and the footer can be iterated:

>>> # TODO: use an interator, and hide the parse_amount in the object
>>> from cfonb.parser.common import parse_amount
>>> for line in statement.lines:
...     l = line.as_obj()
...     print parse_amount(l.amount, l.nb_of_dec)
-2000.0
-1000.0
4000.0
-3000.0

Transfer Writer

Prepare the contents:

>>> from datetime import datetime
>>> from cfonb.writer.transfert import Transfert
>>> transfert = Transfert()
>>> transfert.setEmeteurInfos('2000121','bigbrother','virement de test',503103,2313033,1212,datetime(2011,11,27))
<cfonb.writer.transfert.Transfert instance at ...>
>>> transfert.add('un test','littlebrother','credit agricole ile de france',50011,6565329000,100,'un peu d\'argent',6335)
<cfonb.writer.transfert.Transfert instance at ...>
>>> content = transfert.render()

You can use a filename with render method:

>>> transfert.render(filename='./virement.cfonb')
"0302        200012      ..."

python-cfonb's People

Contributors

ccomb avatar pinaraf avatar sebastienbeau avatar thibault avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-cfonb's Issues

In bank statements, the internal_code on lines "04" is optionnal

In the CFONB spec for bank statements, the internal_code (position 8 lenght 4) is optional. In the code of the lib, in cfonb/parser/common.py line 158, we have:

('internal_code', G_AN, 4),

So it means this field is considered as mandatory. It should be changed to optional, so the code should be:

('internal_code', G_AN_, 4),

I found this bug when testing a CFONB file from HSBC.

Faulty `print` instruction

Hi,

I'm using this app in a django project. Somewhere in a file, there is a forgotten print instruction that breaks our output.

Here is the result of us launching tests:

{'12345678901': <cfonb.parser.statement.Statement object at 0xb5e2a2ac>}
.{'12345678901': <cfonb.parser.statement.Statement object at 0xb5e2a06c>}
.{'12345678902': <cfonb.parser.statement.Statement object at 0xb5de27cc>, '12345678903': <cfonb.parser.statement.Statement object at 0xb5de2b0c>, '12345678901': <cfonb.parser.statement.Statement object at 0xb5dd718c>}
.{'12345678901': <cfonb.parser.statement.Statement object at 0xb5e739cc>}
F{'12345678902': <cfonb.parser.statement.Statement object at 0xb5eaeb6c>, '12345678903': <cfonb.parser.statement.Statement object at 0xb5ea502c>, '12345678901': <cfonb.parser.statement.Statement object at 0xb5e2a24c>}
F.{'12345678902': <cfonb.parser.statement.Statement object at 0xb5de2dec>, '12345678903': <cfonb.parser.statement.Statement object at 0xb5df206c>, '12345678901': <cfonb.parser.statement.Statement object at 0xb5e2a2ac>}
..{'12345678901': <cfonb.parser.statement.Statement object at 0xb5ea518c>}
.

I think print instructions should not be used to print debug information, since it's not very friendly with integration in other projects. The corresponding line should be deletable without any problem, IMO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.