Skip to main content

python copybook parser

Project description

Copybook

Copybook is a python module for parsing VSAM copybooks.

Features

  • Supports all numeric PIC variations including:
    • integers
    • floats described with dot notation
    • floats described with V notation
    • leading SIGNED
    • trailing SIGNED
  • Character PIC notations
  • Groups
  • OCCURS clauses
  • REDEFINES clauses

In addition, it provides indexes of column locations to help with parsing fixed width files based on positions within each line

Installation

pip install copybook

Usage

Copybook provides two methods for parsing copybooks: parse_file and parse_string. The result is a FieldGroup object that represents the root of the copybook tree.

Examples:

import copybook
text = """
       01  WORK-BOOK.                                  
        10  TAX-RATE        PIC S9(13)V9(2)                
                    SIGN LEADING SEPARATE.                                     
"""
root = copybook.parse_string(text)

Using Copybook module to parse a fixed width line

The FieldGroup object provides a flatten method that return a flat list of Field objects.

Example:

import copybook
text = """
       01  WORK-BOOK.                                  
        10  TAX-RATE        PIC S9(13)V9(2)                
                    SIGN LEADING SEPARATE.                                     
        10  AMOUNT        PIC S9(4)V9(2).                
"""
# copybook also provides a parse_file method that receives a text filename
root = copybook.parse_string(text)

# flatten returns a list of Fields and FieldGroups instead of traversing the tree
list_of_fields = root.flatten()

# dummy sample input
line = "          -13452987654"

# loop over the fields and parse the relevant position in the line
for field in list_of_fields:

  # FieldGroups are Copybook groups and contain Field objects as children
  if type(field)==copybook.Field:

    # each Field has a start_pos and a get_total_length method
    # to identify the position within the raw line input
    str_field = line[field.start_pos:field.start_pos+field.get_total_length()]

    # Field provides a parse method that returns a str, int, or float based on the PIC
    print(f"{field.name}: {field.parse(str_field)}")

Development

PRs are always welcome!

Support

If you encounter an unsupported copybook feature, please paste the copybook example along with whatever logs or error message you have received and open an issue.

Gratitude

Copybook uses the awesome PyParsing library for tokenization

License

MIT

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

copybook-1.0.4.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

copybook-1.0.4-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file copybook-1.0.4.tar.gz.

File metadata

  • Download URL: copybook-1.0.4.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for copybook-1.0.4.tar.gz
Algorithm Hash digest
SHA256 08de737bf424a4395c4aa215d46f372ae73b9b7fd378e69f8c63ec00e918fa28
MD5 ac8c4b495f483e8b4a88442d742e4724
BLAKE2b-256 583bcd7ed201bed43237956c146f0668a08dfe02197816eeb6ec3d7936aa48e4

See more details on using hashes here.

File details

Details for the file copybook-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: copybook-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for copybook-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 60bc81a440de08dc5b716f5d9f643dcf84c6c646d2ea445a39dd3ae2d5eaf49e
MD5 8dc14692346852f37dae6ef8a3e6e924
BLAKE2b-256 4950dfdb6e0a6f148192eb35a413571af7af8ff8257cf72a0e170104e64619cc

See more details on using hashes here.

Supported by

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