Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value of Tax Rate - Reference - Readme #23

Open
ankur-katiyar opened this issue Jan 2, 2024 · 2 comments
Open

Value of Tax Rate - Reference - Readme #23

ankur-katiyar opened this issue Jan 2, 2024 · 2 comments

Comments

@ankur-katiyar
Copy link

I tried running the example on Python 3.12 and getting the following result for TAX-RATE.

TAX-RATE: -13.52

Even though, the value should have been -134.52 as the value of line is:

line = " -13452987654"

@zalmane
Copy link
Owner

zalmane commented Jan 2, 2024 via email

@ankur-katiyar
Copy link
Author

its the same example as README.md

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)}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants