bai2
bai2 copied to clipboard
Total Parser function skipped if the value is ''
I love this package! It saved me a ton of time!
Given a bai2 file like the one below, a stripped down example of a real bai2 generated by a the bank when accounts in the group were closed:
from bai2 import bai2
bai2_string = """01,1111,testcompany,210101,0101,010101010101,,,2/
02,testcompany,1111,1,210101,2359,,2/
03,12345,/
49,,2/
03,54321,/
49,,2/
98,0,2,6/
99,0,1,8/
"""
bai2_file = bai2.parse_from_string(bai2_string)
The problem is the Account Trailer records. Because the Summary Total value is blank when running this as is it results in the following error: bai2.exceptions.IntegrityException: Invalid account control total for Account. expected None, found 0
When implementing a simple int parser that looks for ''
and returns 0 instead...
def parse_int(value) -> int:
return int(value) if value else 0
...then updating the AccountTrailerParser fields_config
to use the parser
class AccountTrailerParser(BaseSingleParser):
model = AccountTrailer
fields_config = [
('account_control_total', parse_int),
('number_of_records', int)
]
The same error is raised because the BaseSingleParser
s _parse_field_from_config
will skip the parsing function if the value is ''
It happens here:
def _parse_field_from_config(self, field_config, raw_value):
if isinstance(field_config, str):
field_config = (field_config, lambda x: x)
field_name, parser = field_config
field_value = parser(raw_value) if raw_value else None # <----
return field_name, field_value
I see a few options:
- Add a catch for an
int
parser ontotal
fields - get rid of the
''
catch then implement parsing functions to handle''
values themselves - Adding extra options to the parse model config to know if the None catch should be skipped
- Something else?
I am going to send a pull request with #1 but I'm open to suggestions
When creating the pull request I found removing the if raw_value else None
broke other fields that were not expecting a 0
. The pull request was updated to add a guard for total fields when the parser is an int
.