Records API¶
The records API is the lower level API. It parses each line of “OCR” text input into a record object. A record object also knows about its OCR representation.
For most use cases, the objects API is preferable to the records API.
File parsing¶
To parse an OCR file, you must first read the contents of the OCR file. It should be decoded using the ISO-8559-1 encoding:
with open('my-ocr-file.txt', 'r', encoding='iso-8859-1') as fh:
data = fh.read()
For the purpose of the following example we use the following input data:
>>> data = '''
... NY000010555555551000081000080800000000000000000000000000000000000000000000000000
... NY210020000000000400008688888888888000000000000000000000000000000000000000000000
... NY2121300000001170604 00000000000000100 008000011688373000000
... NY2121310000001NAVN 00000
... NY212149000000140011 Gjelder Faktura: 168837 Dato: 19/03/0400000000000000000000
... NY212149000000140012 ForfallsDato: 17/06/0400000000000000000000
... NY2121300000002170604 00000000000000100 008000021688389000000
... NY2121310000002NAVN 00000
... NY212149000000240011 Gjelder Faktura: 168838 Dato: 19/03/0400000000000000000000
... NY212149000000240012 ForfallsDato: 17/06/0400000000000000000000
... NY2121300000003170604 00000000000000100 008000031688395000000
... NY2121310000003NAVN 00000
... NY2121300000004170604 00000000000000100 008000041688401000000
... NY2121310000004NAVN 00000
... NY2121300000005170604 00000000000000100 008000051688416000000
... NY2121310000005NAVN 00000
... NY212149000000540011 Gjelder Faktura: 168841 Dato: 19/03/0400000000000000000000
... NY212149000000540012 ForfallsDato: 17/06/0400000000000000000000
... NY2102300000006170604 00000000000000100 008000061688422000000
... NY2102310000006NAVN 00000
... NY210088000000060000002000000000000000600170604170604000000000000000000000000000
... NY000089000000060000002200000000000000600170604000000000000000000000000000000000
... '''.strip() # noqa
netsgiro.records.parse()
parses the input and returns a record object for
each line of input:
>>> import netsgiro.records
>>> records = netsgiro.records.parse(data)
>>> len(records)
22
>>> pprint(records)
[TransmissionStart(service_code=<ServiceCode.NONE: 0>, transmission_number='1000081', data_transmitter='55555555', data_recipient='00008080'),
AssignmentStart(service_code=<ServiceCode.AVTALEGIRO: 21>, assignment_type=<AssignmentType.TRANSACTIONS: 0>, assignment_number='4000086', assignment_account='88888888888', agreement_id='000000000'),
TransactionAmountItem1(service_code=<ServiceCode.AVTALEGIRO: 21>, transaction_type=<TransactionType.PURCHASE_WITH_TEXT: 21>, transaction_number=1, nets_date=datetime.date(2004, 6, 17), amount=100, kid='008000011688373', centre_id=None, day_code=None, partial_settlement_number=None, partial_settlement_serial_number=None, sign=None),
TransactionAmountItem2(service_code=<ServiceCode.AVTALEGIRO: 21>, transaction_type=<TransactionType.PURCHASE_WITH_TEXT: 21>, transaction_number=1, reference=None, form_number=None, bank_date=None, debit_account=None, _filler=None, payer_name='NAVN'),
TransactionSpecification(service_code=<ServiceCode.AVTALEGIRO: 21>, transaction_type=<TransactionType.PURCHASE_WITH_TEXT: 21>, transaction_number=1, line_number=1, column_number=1, text=' Gjelder Faktura: 168837 Dato: 19/03/04'),
TransactionSpecification(service_code=<ServiceCode.AVTALEGIRO: 21>, transaction_type=<TransactionType.PURCHASE_WITH_TEXT: 21>, transaction_number=1, line_number=1, column_number=2, text=' ForfallsDato: 17/06/04'),
...
AssignmentEnd(service_code=<ServiceCode.AVTALEGIRO: 21>, assignment_type=<AssignmentType.TRANSACTIONS: 0>, num_transactions=6, num_records=20, total_amount=600, nets_date_1=datetime.date(2004, 6, 17), nets_date_2=datetime.date(2004, 6, 17), nets_date_3=None),
TransmissionEnd(service_code=<ServiceCode.NONE: 0>, num_transactions=6, num_records=22, total_amount=600, nets_date=datetime.date(2004, 6, 17))]
Record types¶
Given a record object, all record fields are available as sensible Python types:
>>> assignment_end = records[-2]
>>> assignment_end.service_code
<ServiceCode.AVTALEGIRO: 21>
>>> assignment_end.RECORD_TYPE
<RecordType.ASSIGNMENT_END: 88>
>>> assignment_end.assignment_type
<AssignmentType.TRANSACTIONS: 0>
>>> assignment_end.nets_date_earliest
datetime.date(2004, 6, 17)
>>> assignment_end.nets_date_latest
datetime.date(2004, 6, 17)
>>> assignment_end.num_records
20
>>> assignment_end.num_transactions
6
>>> assignment_end.total_amount
600
You can also convert the record back to an OCR string:
>>> assignment_end.to_ocr()
'NY210088000000060000002000000000000000600170604170604000000000000000000000000000'
For details on the semantics of each field, please refer to Nets’
documentation. The reference
directory of the netsgiro Git repo
contains the file format specifications, which is a good place to start.
-
class
netsgiro.records.
TransmissionStart
(service_code: Union[netsgiro.enums.ServiceCode, int, str], transmission_number: str, data_transmitter: str, data_recipient: str)[source]¶ TransmissionStart is the first record in every OCR file.
A file can only contain a single transmission.
Each transmission can contain any number of assignments.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 10¶
-
data_recipient
¶
-
data_transmitter
¶
-
service_code
¶
-
transmission_number
¶
-
classmethod
-
class
netsgiro.records.
TransmissionEnd
(service_code: Union[netsgiro.enums.ServiceCode, int, str], num_transactions, num_records, total_amount, nets_date: Union[datetime.date, str, None])[source]¶ TransmissionEnd is the first record in every OCR file.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 89¶
-
nets_date
¶
-
num_records
¶
-
num_transactions
¶
-
service_code
¶
-
total_amount
¶
-
classmethod
-
class
netsgiro.records.
AssignmentStart
(service_code: Union[netsgiro.enums.ServiceCode, int, str], assignment_type: Union[netsgiro.enums.AssignmentType, int, str], assignment_number: str, assignment_account: str, agreement_id: Optional[str] = None)[source]¶ AssignmentStart is the first record of an assignment.
Each assignment can contain any number of transactions.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 20¶
-
agreement_id
¶
-
assignment_account
¶
-
assignment_number
¶
-
assignment_type
¶
-
service_code
¶
-
classmethod
-
class
netsgiro.records.
AssignmentEnd
(service_code: Union[netsgiro.enums.ServiceCode, int, str], assignment_type: Union[netsgiro.enums.AssignmentType, int, str], num_transactions, num_records, total_amount: Union[None, int, str] = None, nets_date_1: Union[datetime.date, str, None] = None, nets_date_2: Union[datetime.date, str, None] = None, nets_date_3: Union[datetime.date, str, None] = None)[source]¶ AssignmentEnd is the last record of an assignment.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 88¶
-
assignment_type
¶
-
nets_date
¶ Nets’ processing date.
Only used for OCR Giro.
-
nets_date_1
¶
-
nets_date_2
¶
-
nets_date_3
¶
-
nets_date_earliest
¶ Earliest date from the contained transactions.
-
nets_date_latest
¶ Latest date from the contained transactions.
-
num_records
¶
-
num_transactions
¶
-
service_code
¶
-
total_amount
¶
-
classmethod
-
class
netsgiro.records.
TransactionAmountItem1
(service_code: Union[netsgiro.enums.ServiceCode, int, str], transaction_type: Union[netsgiro.enums.TransactionType, int, str], transaction_number, nets_date: Union[datetime.date, str], amount, kid: Optional[str], centre_id: Optional[str] = None, day_code: Union[None, int, str] = None, partial_settlement_number: Union[None, int, str] = None, partial_settlement_serial_number: Optional[str] = None, sign: Optional[str] = None)[source]¶ TransactionAmountItem1 is the first record of a transaction.
The record is used both for AvtaleGiro and for OCR Giro.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 30¶
-
amount
¶
-
centre_id
¶
-
day_code
¶
-
kid
¶
-
nets_date
¶
-
partial_settlement_number
¶
-
partial_settlement_serial_number
¶
-
service_code
¶
-
sign
¶
-
transaction_number
¶
-
transaction_type
¶
-
classmethod
-
class
netsgiro.records.
TransactionAmountItem2
(service_code: Union[netsgiro.enums.ServiceCode, int, str], transaction_type: Union[netsgiro.enums.TransactionType, int, str], transaction_number, reference: Optional[str], form_number: Optional[str] = None, bank_date: Union[datetime.date, str, None] = None, debit_account: Optional[str] = None, filler: Optional[str] = None, payer_name: Optional[str] = None)[source]¶ TransactionAmountItem2 is the second record of a transaction.
The record is used both for AvtaleGiro and for OCR Giro.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 31¶
-
bank_date
¶
-
debit_account
¶
-
form_number
¶
-
payer_name
¶
-
reference
¶
-
service_code
¶
-
transaction_number
¶
-
transaction_type
¶
-
classmethod
-
class
netsgiro.records.
TransactionAmountItem3
(service_code: Union[netsgiro.enums.ServiceCode, int, str], transaction_type: Union[netsgiro.enums.TransactionType, int, str], transaction_number, text: Optional[str])[source]¶ TransactionAmountItem3 is the third record of a transaction.
The record is only used for some OCR Giro transaction types.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 32¶
-
service_code
¶
-
text
¶
-
transaction_number
¶
-
transaction_type
¶
-
classmethod
-
class
netsgiro.records.
TransactionSpecification
(service_code: Union[netsgiro.enums.ServiceCode, int, str], transaction_type: Union[netsgiro.enums.TransactionType, int, str], transaction_number, line_number, column_number, text)[source]¶ TransactionSpecification is used for AvtaleGiro transactions.
The record is only used when bank notification is used to notify the payer.
Each record contains half of an 80 char long line of text and can be repeated up to 84 times for a single transaction for a total of 42 lines of specification text.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
classmethod
from_text
(*, service_code: netsgiro.enums.ServiceCode, transaction_type: TransactionType, transaction_number: int, text: Optional[str]) → Iterable[TransactionSpecification][source]¶ Create a sequence of specification records from a text string.
-
classmethod
to_text
(records: List[TransactionSpecification]) → str[source]¶ Get a text string from a sequence of specification records.
-
RECORD_TYPE
= 49¶
-
column_number
¶
-
line_number
¶
-
service_code
¶
-
text
¶
-
transaction_number
¶
-
transaction_type
¶
-
classmethod
-
class
netsgiro.records.
AvtaleGiroAgreement
(service_code: Union[netsgiro.enums.ServiceCode, int, str], transaction_type: Union[netsgiro.enums.TransactionType, int, str], transaction_number, registration_type: Union[netsgiro.enums.AvtaleGiroRegistrationType, int, str], kid: Optional[str], notify: Union[bool, str])[source]¶ AvtaleGiroAgreement is used by Nets to notify about agreement changes.
This includes new or deleted agreements, as well as updates to the payer’s notification preferences.
-
classmethod
from_string
(line: str) → R¶ Parse OCR string into a record object.
-
RECORD_TYPE
= 70¶
-
kid
¶
-
notify
¶
-
registration_type
¶
-
service_code
¶
-
transaction_number
¶
-
transaction_type
¶
-
classmethod