From 58c7613d874f7fb7b31be7d725db8a111218634f Mon Sep 17 00:00:00 2001 From: Chuck SmileyRekiere Date: Fri, 12 Nov 2021 07:21:44 -0700 Subject: [PATCH] lf_graph.py lf_report_test.py : pep8 complience Signed-off-by: Chuck SmileyRekiere --- py-scripts/lf_graph.py | 134 +++++++++++++++++++++++++++-------- py-scripts/lf_report_test.py | 49 +++++++------ 2 files changed, 133 insertions(+), 50 deletions(-) diff --git a/py-scripts/lf_graph.py b/py-scripts/lf_graph.py index aef088e5..139c08d5 100755 --- a/py-scripts/lf_graph.py +++ b/py-scripts/lf_graph.py @@ -25,15 +25,18 @@ import numpy as np import pdfkit from matplotlib.colors import ListedColormap import argparse - + sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../"))) lf_csv = importlib.import_module("py-scripts.lf_csv") lf_csv = lf_csv.lf_csv -# internal candela references included during intial phases, to be deleted at future date +# internal candela references included during intial phases, to be deleted +# at future date # graph reporting classes + + class lf_bar_graph(): def __init__(self, _data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]], _xaxis_name="x-axis", @@ -52,11 +55,11 @@ class lf_bar_graph(): _figsize=(10, 5), _show_bar_value=False, _xaxis_step=1, - _xticks_font = None, - _xaxis_value_location = 0, + _xticks_font=None, + _xaxis_value_location=0, _text_font=None, _text_rotation=None, - _grp_title = "", + _grp_title="", _legend_handles=None, _legend_loc="best", _legend_box=None, @@ -132,11 +135,19 @@ class lf_bar_graph(): plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) if self.xaxis_categories[0] == 0: - plt.xticks(np.arange(0, len(self.xaxis_categories), step=self.xaxis_step),fontsize = self.xticks_font) + plt.xticks(np.arange(0, + len(self.xaxis_categories), + step=self.xaxis_step), + fontsize=self.xticks_font) else: plt.xticks([i + self._xaxis_value_location for i in np.arange(0, len(self.data_set[0]), step=self.xaxis_step)], - self.xaxis_categories, fontsize=self.xticks_font) - plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize) + self.xaxis_categories, fontsize=self.xticks_font) + plt.legend( + handles=self.legend_handles, + loc=self.legend_loc, + bbox_to_anchor=self.legend_box, + ncol=self.legend_ncol, + fontsize=self.legend_fontsize) plt.suptitle(self.title, fontsize=self.title_size) plt.title(self.grp_title) fig = plt.gcf() @@ -155,7 +166,8 @@ class lf_bar_graph(): self.lf_csv.filename = f"{self.graph_image_name}.csv" self.lf_csv.generate_csv() else: - raise ValueError("Length and x-axis values and y-axis values should be same.") + raise ValueError( + "Length and x-axis values and y-axis values should be same.") else: print("No Dataset Found") print("{}.csv".format(self.graph_image_name)) @@ -188,20 +200,38 @@ class lf_scatter_graph(): def build_scatter_graph(self): if self.color is None: - self.color = ["orchid", "lime", "aquamarine", "royalblue", "darkgray", "maroon"] + self.color = [ + "orchid", + "lime", + "aquamarine", + "royalblue", + "darkgray", + "maroon"] fig = plt.subplots(figsize=self.figsize) if self.values is None: - plt.scatter(self.x_data_set, self.y_data_set[0], color=self.color[0], label=self.label[0]) + plt.scatter( + self.x_data_set, + self.y_data_set[0], + color=self.color[0], + label=self.label[0]) if len(self.y_data_set) > 1: for i in range(1, len(self.y_data_set)): - plt.scatter(self.x_data_set, self.y_data_set[i], color=self.color[i], label=self.label[i]) + plt.scatter( + self.x_data_set, + self.y_data_set[i], + color=self.color[i], + label=self.label[i]) plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) plt.gcf().autofmt_xdate() plt.legend() else: colours = ListedColormap(self.color) - scatter = plt.scatter(self.x_data_set, self.y_data_set, c=self.values, cmap=colours) + scatter = plt.scatter( + self.x_data_set, + self.y_data_set, + c=self.values, + cmap=colours) plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) plt.gcf().autofmt_xdate() @@ -241,9 +271,19 @@ class lf_stacked_graph(): def build_stacked_graph(self): fig = plt.subplots(figsize=self.figsize) if self.color is None: - self.color = ["darkred", "tomato", "springgreen", "skyblue", "indigo", "plum"] + self.color = [ + "darkred", + "tomato", + "springgreen", + "skyblue", + "indigo", + "plum"] plt.bar(self.data_set[0], self.data_set[1], color=self.color[0]) - plt.bar(self.data_set[0], self.data_set[2], bottom=self.data_set[1], color=self.color[1]) + plt.bar( + self.data_set[0], + self.data_set[2], + bottom=self.data_set[1], + color=self.color[1]) if len(self.data_set) > 3: for i in range(3, len(self.data_set)): plt.bar(self.data_set[0], self.data_set[i], @@ -303,8 +343,19 @@ class lf_horizontal_stacked_graph(): ind = np.arange(n) + .15 width = 0.3 - rects1 = plt.barh(ind, values1, width, color=self.color[0], label=self.label[0]) - rects2 = plt.barh(ind, values2, width, left=sumzip(values1), color=self.color[1], label=self.label[1]) + rects1 = plt.barh( + ind, + values1, + width, + color=self.color[0], + label=self.label[0]) + rects2 = plt.barh( + ind, + values2, + width, + left=sumzip(values1), + color=self.color[1], + label=self.label[1]) extra_space = 0.15 ax.set_yticks(ind + width - extra_space) @@ -326,7 +377,12 @@ class lf_horizontal_stacked_graph(): ax.spines['top'].set_visible(False) ax.legend(loc='upper right') if self.disable_xaxis: - plt.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False) # disable x-axis + plt.tick_params( + axis='x', + which='both', + bottom=False, + top=False, + labelbottom=False) # disable x-axis plt.savefig("%s.png" % self.graph_image_name, dpi=96) plt.close() print("{}.png".format(self.graph_image_name)) @@ -339,7 +395,7 @@ class lf_horizontal_stacked_graph(): class lf_line_graph(): - def __init__(self,_data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]], + def __init__(self, _data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]], _xaxis_name="x-axis", _yaxis_name="y-axis", _xaxis_categories=[1, 2, 3, 4, 5], @@ -351,8 +407,8 @@ class lf_line_graph(): _font_weight='bold', _color=['forestgreen', 'c', 'r', 'g', 'b', 'p'], _figsize=(10, 5), - _xaxis_step = 5, - _xticks_font = None, + _xaxis_step=5, + _xticks_font=None, _text_font=None, _legend_handles=None, _legend_loc="best", @@ -390,12 +446,22 @@ class lf_line_graph(): fig = plt.subplots(figsize=self.figsize) i = 0 for data in self.data_set: - plt.plot(self.xaxis_categories, data, color=self.color[i], label=self.label[i], marker = self.marker) + plt.plot( + self.xaxis_categories, + data, + color=self.color[i], + label=self.label[i], + marker=self.marker) i += 1 plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) - plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize) + plt.legend( + handles=self.legend_handles, + loc=self.legend_loc, + bbox_to_anchor=self.legend_box, + ncol=self.legend_ncol, + fontsize=self.legend_fontsize) plt.suptitle(self.grp_title, fontsize=self.title_size) fig = plt.gcf() plt.savefig("%s.png" % self.graph_image_name, dpi=96) @@ -412,6 +478,7 @@ class lf_line_graph(): print("{}.csv".format(self.graph_image_name)) return "%s.png" % self.graph_image_name + def main(): # arguments parser = argparse.ArgumentParser( @@ -421,7 +488,7 @@ def main(): lf_graph.py : unit test in lf_graph.py for exersizing the lf_graph.py library ''', description='''\ ------------------ +----------------- NAME: lf_graph.py PURPOSE: @@ -440,8 +507,13 @@ COPYWRITE INCLUDE_IN_README --------------------- ''') - parser.add_argument('--mgr','--lfmgr', dest='lfmgr', help='sample argument: where LANforge GUI is running', default='localhost') - # the args parser is not really used , this is so the report is not generated when testing + parser.add_argument( + '--mgr', + '--lfmgr', + dest='lfmgr', + help='sample argument: where LANforge GUI is running', + default='localhost') + # the args parser is not really used , this is so the report is not generated when testing # the imports with --help args = parser.parse_args() print("LANforge manager {lfmgr}".format(lfmgr=args.lfmgr)) @@ -464,7 +536,8 @@ INCLUDE_IN_README # write logic to generate pdf here # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb - options = {"enable-local-file-access": None} # prevent eerror Blocked access to file + # prevent eerror Blocked access to file + options = {"enable-local-file-access": None} pdfkit.from_file(output_html_1, output_pdf_1, options=options) # test build_bar_graph setting values @@ -497,10 +570,11 @@ INCLUDE_IN_README # write logic to generate pdf here # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb - options = {"enable-local-file-access": None} # prevent eerror Blocked access to file + # prevent eerror Blocked access to file + options = {"enable-local-file-access": None} pdfkit.from_file(output_html_2, output_pdf_2, options=options) + # Unit Test if __name__ == "__main__": - main() - + main() diff --git a/py-scripts/lf_report_test.py b/py-scripts/lf_report_test.py index 831fe400..114af078 100755 --- a/py-scripts/lf_report_test.py +++ b/py-scripts/lf_report_test.py @@ -2,13 +2,13 @@ ''' NAME: lf_report_test.py -PURPOSE: +PURPOSE: Common file for testing lf_report and lf_graph Library generates html and pdf output -SETUP: +SETUP: /lanforge/html-reports directory needs to be present or output generated in local file -EXAMPLE: +EXAMPLE: ./lf_report_test.py : currently script does not accept input COPYWRITE @@ -28,7 +28,7 @@ import pdfkit import random import argparse - + sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../"))) lf_report = importlib.import_module("py-scripts.lf_report") @@ -40,22 +40,24 @@ lf_stacked_graph = lf_graph.lf_stacked_graph lf_horizontal_stacked_graph = lf_graph.lf_horizontal_stacked_graph # Unit Test + + def main(): # Testing: generate data frame parser = argparse.ArgumentParser( prog="lf_report_test.py", formatter_class=argparse.RawTextHelpFormatter, description='''\ ------------------ +----------------- NAME: lf_report_test.py -PURPOSE: +PURPOSE: Common file for testing lf_report and lf_graph Library generates html and pdf output -SETUP: +SETUP: /lanforge/html-reports directory needs to be present or output generated in local file -EXAMPLE: +EXAMPLE: ./lf_report_test.py : currently script does not accept input COPYWRITE @@ -65,8 +67,13 @@ COPYWRITE INCLUDE_IN_README ''') - parser.add_argument('--mgr','--lfmgr', dest='lfmgr', help='sample argument: where LANforge GUI is running', default='localhost') - # the args parser is not really used , this is so the report is not generated when testing + parser.add_argument( + '--mgr', + '--lfmgr', + dest='lfmgr', + help='sample argument: where LANforge GUI is running', + default='localhost') + # the args parser is not really used , this is so the report is not generated when testing # the imports with --help args = parser.parse_args() print("LANforge manager {lfmgr}".format(lfmgr=args.lfmgr)) @@ -82,7 +89,7 @@ INCLUDE_IN_README print(dataframe) - # Testing: generate data frame + # Testing: generate data frame dataframe2 = pd.DataFrame({ 'station': [1, 2, 3, 4, 5, 6, 7], 'time_seconds': [23, 78, 22, 19, 45, 22, 25] @@ -137,7 +144,7 @@ INCLUDE_IN_README _xaxis_categories=x_axis_values, _graph_image_name="Bi-single_radio_2.4GHz", _label=["bi-downlink", "bi-uplink", 'uplink'], - _color=['darkorange', 'forestgreen','blueviolet'], + _color=['darkorange', 'forestgreen', 'blueviolet'], _color_edge='red', _grp_title="Throughput for each clients", _xaxis_step=5, @@ -146,7 +153,7 @@ INCLUDE_IN_README _text_rotation=45, _xticks_font=7, _legend_loc="best", - _legend_box=(1,1), + _legend_box=(1, 1), _legend_ncol=1, _legend_fontsize=None, _enable_csv=True) @@ -156,7 +163,7 @@ INCLUDE_IN_README print("graph name {}".format(graph_png)) report.set_graph_image(graph_png) - # need to move the graph image to the results + # need to move the graph image to the results report.move_graph_image() if graph.enable_csv: report.set_csv_filename(graph_png) @@ -169,7 +176,7 @@ INCLUDE_IN_README _graph_image_name="image_name1", _color=None, _label=["s1", "s2", "s3"], - _enable_csv = False) + _enable_csv=False) graph_png = graph2.build_scatter_graph() print("graph name {}".format(graph_png)) @@ -178,14 +185,15 @@ INCLUDE_IN_README report.move_graph_image() report.build_graph() - # this will generate graph which is independent,we can customize the value with different colors + # this will generate graph which is independent,we can customize the value + # with different colors graph2 = lf_scatter_graph(_x_data_set=set1, _y_data_set=[45, 67, 45, 34], _values=[0, 0, 0, 1], _xaxis_name="x-axis", _yaxis_name="y-axis", _graph_image_name="image_name_map", _color=None, _label=["s1", "s2"], - _enable_csv = False) + _enable_csv=False) graph_png = graph2.build_scatter_graph() print("graph name {}".format(graph_png)) @@ -194,14 +202,15 @@ INCLUDE_IN_README report.move_graph_image() report.build_graph() - dataset = [["1", "2", "3", "4"], [12, 45, 67, 34], [23, 67, 23, 12], [25, 45, 34, 23]] + dataset = [["1", "2", "3", "4"], [12, 45, 67, 34], + [23, 67, 23, 12], [25, 45, 34, 23]] graph = lf_stacked_graph(_data_set=dataset, _xaxis_name="Stations", _yaxis_name="Login PASS/FAIL", _label=['Success', 'Fail', 'both'], _graph_image_name="login_pass_fail1", _color=None, - _enable_csv = False) + _enable_csv=False) graph_png = graph.build_stacked_graph() @@ -221,7 +230,7 @@ INCLUDE_IN_README _graph_image_name="image_name_pass_fail", _color=["r", "g"], _figsize=(9, 4), - _enable_csv = False) + _enable_csv=False) graph_png = graph.build_horizontal_stacked_graph()