1
\$\begingroup\$

I am getting log file which contain other information along with leakage information into following format Log_file

<some test1>
<some test2>
leakage1, leakage2.... til lekage29 (separated by comma)
val1, val2, val3............ (separated by comma)

#!/usr/bin/env python

import argparse
import pandas as pd
import re
import os
import sys
import xlsx_api
import techinfo

def parse_args(argv):
    """Parse the argument"""
    parser = argparse.ArgumentParser(description=__doc__,
                                     epilog=""" """,
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-cell_list',dest='cell_list', nargs="?",
                        help="cell list")
    parser.add_argument('-pxe_cell_ref',dest='pxe_cell_ref',
                        help="Provide pxe cell ref")
    parser.add_argument('-non_pxe_cell_ref', dest='non_pxe_cell_ref',
                        help="Provide non pxe cell ref")
    parser.add_argument('-pxe_job_name', dest='pxe_job_name',
                        help="Provide job name which you have run for pxe and create job_name.log")
    parser.add_argument('-non_pxe_job_name', dest='non_pxe_job_name',
                        help="Provide job name which you have run for pxe and create job_name.log")
    parser.add_argument('-o', '--output', dest='output', default='report.xlsx',
                        help="Provide output name")
    args = parser.parse_args(argv)
    return args

def get_cell_list(cell_list):
    """
    Determine cell_list from file
    """
    cell_data = []
    with open(cell_list, "r") as l_file:
        cell_data = l_file.read().splitlines()
    return cell_data

def get_simulation_path():
    """
    get path
    """
    #remove original code and replace with following line
    #As it only provide path 
    return os.getcwd()

class Leakage(object):
    """
    create lekage table
    """
    def __init__(self, ref_cell, cell_list, job_name):
        self.ref_cell = ref_cell
        self.cell_list = get_cell_list(cell_list)
        self.job_name = job_name

    def extract_log_file(self, log_file, cell_name):
        """
        Input : log_file
        Overview : Read log file and extract leakge information
        Return : leakge information
        """
        f_data = []
        with open(log_file, "r") as flog:
            f_data = flog.readlines()
        upd_f_data = []
        status = 0
        for data in f_data:
            if 'ileak' in data or cell_name in data:
                upd_f_data.append([data])
            elif 'Pass' in data:
                break
            elif 'Fail' in data:
                status = 1
                break
        return upd_f_data, status

    def update_dataframe(self, file_log_data):
        """
        Fill data frame
        Overiew : Extract index name
                : Remove duplicate column
        """
        frame_format = dict()
        columns = []
        index_name =  0
        for index, data in enumerate(file_log_data):
            data  = [da.strip() for da in re.split(r'\t|,', data[0])]
            if index%2==0 and index!=0:
                continue
            if index != 0:
                columns.append(data.pop(0))
            data.pop()
            frame_format[index] = pd.Series(data)
        data_frame = pd.DataFrame(frame_format)
        data_frame.set_index([0], inplace=True, drop=True)
        data_frame.index.name = "Leakage_val"
        columns = [columns[col] + "_" + str(col) for col in range(0, len(columns), 1)]
        data_frame.columns = columns
        data_frame = data_frame.apply(pd.to_numeric)
        for column in data_frame.columns[1:]:
            data_frame[columns[0]+"_upd"] = data_frame[column]-2*data_frame[columns[0]]
        return data_frame

    def read_data(self, sheet, workbook):
        ext = ".log"
        cells_dir = get_simulation_path()
        status = 0
        files_log_data  = []
        for cell in [self.ref_cell] + self.cell_list:
            log_data, status = self.extract_log_file(os.path.join(cells_dir, 
                                                          cell, 
                                                          self.job_name+".log"), cell)

            files_log_data.extend(log_data)
        frame_info = self.update_dataframe(files_log_data)
        row = 1
        col = 0
        sheet.write_row(0, 1, list(frame_info.columns), workbook.xlsx_format)
        for key, val in frame_info.iterrows():
            sheet.write_row(row, col, [key] + val.tolist(), workbook.row_format)
            row +=1

def main(args):
    """
    Invoke main functionality
    """
    report_xlsx = xlsx_api.xlsx_workbook(args.output)
    pxe_sheet = xlsx_api.xlsx_sheet(report_xlsx, "pxe_sheet", 0, 0, 20)
    leakge_pxe = Leakage(args.pxe_cell_ref, args.pxe_cell_list, args.job_name)
    leakge_pxe.read_data(pxe_sheet, report_xlsx)

    non_pxe_sheet = xlsx_api.xlsx_sheet(report_xlsx, "non_pxe_sheet", 0, 0, 20)
    leakge_pxe = Leakage(args.non_pxe_cell_ref, args.non_pxe_cell_list, args.job_name)
    leakge_pxe.read_data(non_pxe_sheet, report_xlsx)
    report_xlsx.close_xlsx()


if __name__ == "__main__":
    sys.exit(main(parse_args(sys.argv[1:])))
\$\endgroup\$
  • 1
    \$\begingroup\$ Python does not have an implicit return of the last object, so get_simulation_path will always return None instead of the current path. \$\endgroup\$ – Graipher Aug 13 '19 at 8:29

Browse other questions tagged or ask your own question.