iso8583 – Functions

Main Functions

iso8583.decode(s: bytes, spec: dict) → Tuple[dict, dict][source]

Deserialize a bytes or bytearray instance containing ISO8583 data to a Python dict.

Parameters:
  • s (bytes or bytearray) – Encoded ISO8583 data
  • spec (dict) – A Python dict defining ISO8583 specification. See iso8583.specs module for examples.
Returns:

  • doc_dec (dict) – Dict containing decoded ISO8583 data
  • doc_enc (dict) – Dict containing encoded ISO8583 data

Raises:
  • DecodeError – An error decoding ISO8583 bytearray
  • TypeErrors must be a bytes or bytearray instance

Examples

>>> import pprint
>>> import iso8583
>>> from iso8583.specs import default_ascii as spec
>>> s = b"02004010100000000000161234567890123456123456111"
>>> doc_dec, doc_enc = iso8583.decode(s, spec)
>>> pprint.pprint(doc_dec)
{'12': '123456',
 '2': '1234567890123456',
 '20': '111',
 'bm': {2, 12, 20},
 'p': '4010100000000000',
 't': '0200'}
iso8583.encode(doc_dec: dict, spec: dict) → Tuple[bytearray, dict][source]

Serialize Python dict containing ISO8583 data to a bytearray.

Parameters:
  • doc_dec (dict) – Dict containing decoded ISO8583 data
  • spec (dict) – A Python dict defining ISO8583 specification. See iso8583.specs module for examples.
Returns:

  • s (bytearray) – Encoded ISO8583 data
  • doc_enc (dict) – Dict containing encoded ISO8583 data

Raises:
  • EncodeError – An error encoding ISO8583 bytearray
  • TypeErrordoc_dec must be a dict instance

Examples

>>> import iso8583
>>> from iso8583.specs import default_ascii as spec
>>> doc_dec = {
...     't': '0210',
...     'bm': set([3, 39]),
...     '3': '111111',
...     '39': '05'}
>>> s, doc_enc = iso8583.encode(doc_dec, spec)
>>> s
bytearray(b'0210200000000200000011111105')

Exceptions

exception iso8583.DecodeError(msg: str, s: bytes, doc_dec: dict, doc_enc: dict, pos: int, field: str)[source]

Subclass of ValueError that describes ISO8583 decoding error.

Variables:
  • msg (str) – The unformatted error message
  • s (bytes or bytearray) – The ISO8583 bytes instance being parsed
  • doc_dec (dict) – Dict containing partially decoded ISO8583 data
  • doc_enc (dict) – Dict containing partially encoded ISO8583 data
  • pos (int) – The start index where ISO8583 bytes data failed parsing
  • field (str) – The ISO8583 field where parsing failed
exception iso8583.EncodeError(msg: str, doc_dec: dict, doc_enc: dict, field: str)[source]

Subclass of ValueError that describes ISO8583 encoding error.

Variables:
  • msg (str) – The unformatted error message
  • doc_dec (dict) – Dict containing decoded ISO8583 data being encoded
  • doc_enc (dict) – Dict containing partially encoded ISO8583 data
  • field (str) – The ISO8583 field where parsing failed

Helper Functions

iso8583.add_field(doc_dec: dict, field: str, val: str) → None[source]

Add or override field and its value in ISO8583 dictionary.

Parameters:
  • doc_dec (dict) – Dict containing decoded ISO8583 data
  • field (str) – Field key to be added
  • val (str) – Field value to be added

Examples

>>> import iso8583
>>> from iso8583.specs import default_ascii as spec
>>> s = b"02004010100000000000161234567890123456123456840"
>>> doc_dec, doc_enc = iso8583.decode(s, spec)
>>> iso8583.pp(doc_dec, spec)
'bm'  Enabled Fields                      : [2, 12, 20]
't'   Message Type                        : [0200]
'p'   Bitmap, Primary                     : [4010100000000000]
'2'   Primary Account Number (PAN)        : 16 [1234567890123456]
'12'  Time, Local Transaction             : [123456]
'20'  PAN Country Code                    : [840]
>>> iso8583.add_field(doc_dec, "t", "0210")
>>> iso8583.add_field(doc_dec, "39", "00")
>>> s, doc_enc = iso8583.encode(doc_dec, spec)
>>> iso8583.pp(doc_dec, spec)
'bm'  Enabled Fields                      : [2, 12, 20, 39]
't'   Message Type                        : [0210]
'p'   Bitmap, Primary                     : [4010100002000000]
'2'   Primary Account Number (PAN)        : 16 [1234567890123456]
'12'  Time, Local Transaction             : [123456]
'20'  PAN Country Code                    : [840]
'39'  Response Code                       : [00]
iso8583.del_field(doc_dec: dict, field: str) → Any[source]

Delete field from ISO8583 dictionary.

Parameters:
  • doc_dec (dict) – Dict containing decoded ISO8583 data
  • field (int or str) – Field key to be deleted
Returns:

Deleted field value. None if the field did not exist.

Return type:

Key Value or None

Examples

>>> import iso8583
>>> from iso8583.specs import default_ascii as spec
>>> s = b"02004010100000000000161234567890123456123456840"
>>> doc_dec, doc_enc = iso8583.decode(s, spec)
>>> iso8583.pp(doc_dec, spec)
'bm'  Enabled Fields                      : [2, 12, 20]
't'   Message Type                        : [0200]
'p'   Bitmap, Primary                     : [4010100000000000]
'2'   Primary Account Number (PAN)        : 16 [1234567890123456]
'12'  Time, Local Transaction             : [123456]
'20'  PAN Country Code                    : [840]
>>> iso8583.del_field(doc_dec, "20")
'840'
>>> s, doc_enc = iso8583.encode(doc_dec, spec)
>>> iso8583.pp(doc_dec, spec)
'bm'  Enabled Fields                      : [2, 12]
't'   Message Type                        : [0200]
'p'   Bitmap, Primary                     : [4010000000000000]
'2'   Primary Account Number (PAN)        : 16 [1234567890123456]
'12'  Time, Local Transaction             : [123456]
iso8583.pp(doc_dec: dict, spec: dict, desc_width: int = 36, stream: IO[AnyStr] = None) → None[source]

Pretty Print Python dict containing decoded ISO8583 data.

Parameters:
  • doc_dec (dict) – Dict containing decoded ISO8583 data
  • spec (dict) – A Python dict defining ISO8583 specification. See iso8583.specs module for examples.
  • desc_width (int, optional) – Width of field description that’s printed (default 36). Specify 0 to print no descriptions.
  • stream (stream, optional) – An output stream. The only method used on the stream object is the file protocol’s write() method. If not specified, the iso8583.pp() adopts sys.stdout.

Notes

For iso8583.pp() to work in all circumstances it’s recommended to be used after iso8583.encode() or iso8583.decode().

Examples

>>> import iso8583
>>> from iso8583.specs import default_ascii as spec
>>> s = b"02004010100000000000161234567890123456123456840"
>>> doc_dec, doc_enc = iso8583.decode(s, spec)
>>> iso8583.pp(doc_dec, spec)
'bm'  Enabled Fields                      : [2, 12, 20]
't'   Message Type                        : [0200]
'p'   Bitmap, Primary                     : [4010100000000000]
'2'   Primary Account Number (PAN)        : 16 [1234567890123456]
'12'  Time, Local Transaction             : [123456]
'20'  PAN Country Code                    : [840]