-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlf_csv.py
More file actions
executable file
·124 lines (100 loc) · 4.04 KB
/
lf_csv.py
File metadata and controls
executable file
·124 lines (100 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
'''
NAME: lf_csv.py
PURPOSE: Library code for generating CSV from LANforge output KPI data
NOTES: Assumes '/home/lanforge/html-reports/' directory present
EXAMPLE: See 'lf_report_test.py' for example usage
LICENSE: Free to distribute and modify. LANforge systems must be licensed.
Copyright (C) 2020-2026 Candela Technologies Inc
INCLUDE_IN_README:
False
'''
import pandas as pd
from csv import reader
import csv
import argparse
class lf_csv:
def __init__(self,
_columns: list = None,
_rows: list = None,
_filename: str = 'test.csv'):
DEFAULT_ROWS = [
['sta0001', 'sta0002', 'sta0003', 'sta0004', 'sta0005'],
[1, 2, 3, 4, 5],
[11, 22, 33, 44, 55],
[6, 7, 8, 9, 10],
[66, 77, 88, 99, 100],
]
DEFAULT_COLUMNS = ['Stations', 'bk', 'be', 'vi', 'vo']
self.rows = _rows if _rows is not None else DEFAULT_ROWS
self.columns = _columns if _columns is not None else DEFAULT_COLUMNS
self.filename = _filename
def generate_csv(self):
df = {}
if self.rows != []:
for i in range(len(self.columns)):
df[self.columns[i]] = self.rows[i]
else:
for i in range(len(self.columns)):
df[self.columns[i]] = []
csv_df = pd.DataFrame(df)
print(csv_df)
csv_df.to_csv(self.filename, index=False, encoding='utf-8', na_rep='NA', float_format='%.2f')
def read_csv(self, file_name, column=None):
data = pd.read_csv(str(file_name))
value = data[str(column)].tolist()
print("value of column", value)
return value
def read_csv_row(self, file_name):
lst = []
with open(str(file_name), 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Iterate over each row in the csv using reader object
for row in csv_reader:
# row variable is a list that represents a row in csv
print(row)
lst.append(row)
print("list", lst)
return lst
def open_csv_append(self, fields, name):
# fields = ['first', 'second', 'third']
with open(str(name), 'a') as f:
writer = csv.writer(f)
writer.writerow(fields)
def main():
help_summary = '''\
This script is designed to generate the csv file with the test report data of lanforge tests, any file which runs
the tests can import this file to generate the csv file with the test report data. The file name is test.csv and
the file will the stored in the same directory of this file (pyscripts) while creating the object of this lf_csv
file, the data can be passed while creating the object and the report can be generated by calling the method
generate_csv()
Note this is not the kpi csv format. Use lf_kpi_csv.py for the kpi csv format.
'''
parser = argparse.ArgumentParser(
prog='lf_mixed_traffic.py',
formatter_class=argparse.RawTextHelpFormatter,
epilog='''\
The lf_csv is used to generate csv report of the lanforge test results
''',
description='''
NAME: lf_csv.py
PURPOSE: Library code for generating CSV from LANforge output KPI data
NOTES: Assumes '/home/lanforge/html-reports/' directory present
EXAMPLE: See 'lf_report_test.py' for example usage
LICENSE: Free to distribute and modify. LANforge systems must be licensed.
Copyright (C) 2020-2026 Candela Technologies Inc
INCLUDE_IN_README:
False
''')
parser.add_argument('--help_summary', help='Show summary of what this script does', default=None,
action="store_true")
args = parser.parse_args()
# help summary
if args.help_summary:
print(help_summary)
exit(0)
test = lf_csv()
test.generate_csv()
if __name__ == "__main__":
main()