Skip to main content

Python script to parse and return records based on the record line. This script specifically handles microfile headers sequentially.

Project description

py-visual-cobol
│
├───py_visual_cobol
│   ├───record_extractor.py
│   ├───__init__.py
│   │
│   ├───constants
│      ├───params.py
│      └───__init__.py
│   │
│   └───utils
│       ├───bytes_converter.py
│       ├───segment_patterns_generator.py
│       └─── __init__.py
├───tests
│      test_bytes_converter.py
│      test_segment_patterns_generator.py
│   └───__init__.py
│
├───poetry.lock
├───pyproject.toml
└───README.md
from pyspark.sql.types import StructType, StructField, IntegerType, BinaryType
from py_visual_cobol.record_extractor import record_header_extractor
from pyspark.sql import SparkSession,functions as F
from pyspark import StorageLevel
import argparse
import mmap


def parse_args():
    """Parse command line arguments."""
    parser = argparse.ArgumentParser(description="Process a microfile.")
    parser.add_argument("file_path", type=str, help="Path to the microfile.")
    return parser.parse_args()

if __name__ == "__main__":
    # Parse command-line arguments
    args = parse_args()

    # Read the file content
    with open(args.file_path, mode="rb") as file:
        # Use mmap to map the file into memory
        with mmap.mmap(file.fileno(), length=0, access=mmap.ACCESS_READ) as mmapped_file:
            # Read data in a memory-efficient way
            content = mmapped_file[:]  # Only extract bytes as needed

    records_length = [
            126,
            836,
            96,
            694,
            302,
            160,
            107,
            76,
            266,
            68,
            99,
            78,
            425,
        ]

    # Extract records using the segment patterns generated
    records = record_header_extractor(content,records_length=records_length,debug=True)
    
    print(len(records))
    spark = SparkSession.builder \
        .appName('app') \
        .config("spark.executor.memory", "8g") \
        .config("spark.driver.memory", "8g") \
        .config("spark.executor.cores", "8") \
        .config("spark.driver.maxResultSize", "0") \
        .config("spark.dynamicAllocation.enabled", "true") \
        .getOrCreate()


    # Define schema for the DataFrame


    schema = StructType([
        StructField("rdw", IntegerType(), True),
        StructField("value", BinaryType(), True)
    ])

    df = spark.createDataFrame(records, schema=schema)

    records.clear()

    df.persist(StorageLevel.DISK_ONLY)

    df.write.partitionBy("rdw").mode("overwrite").parquet("data", compression="snappy")
    df.unpersist(True)
    spark.stop()

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

py_visual_cobol-1.1.2.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

py_visual_cobol-1.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file py_visual_cobol-1.1.2.tar.gz.

File metadata

  • Download URL: py_visual_cobol-1.1.2.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.9 Windows/10

File hashes

Hashes for py_visual_cobol-1.1.2.tar.gz
Algorithm Hash digest
SHA256 cdf4eecb0d39cced193088f3e6029db9a2a6cf4afb22c185c16b710b801727a8
MD5 14ebd7bfd5841e75638608a7dfd4d074
BLAKE2b-256 4e24be38316314c09155e5ed7822410eb2a27b41679f9da2a94eb23915bc0d06

See more details on using hashes here.

File details

Details for the file py_visual_cobol-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: py_visual_cobol-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.9 Windows/10

File hashes

Hashes for py_visual_cobol-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aee500fcde59f45ed5315b89ccb7861e44cec62f836496e04e7adbac844498f4
MD5 241b2d74987d33fd5d4de40274b27ba4
BLAKE2b-256 5450fcd4d36b2ee58bb73dcafdc044981b9ff7e7538180b1426c3ba51008e1ba

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