From fb9352d23658f08b145ea5d685495c179207def5 Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Sun, 31 Dec 2023 07:57:30 -0800 Subject: [PATCH] Simplified libraries and updated CI. --- .github/workflows/ci.yml | 6 ++-- graphs.hpp | 47 +++++++++++++------------ python/graphs.py | 39 +++++++++------------ python/tables.py | 76 ++++++++++++++++------------------------ python/test.py | 6 ++-- tables.hpp | 55 +++++++++++++---------------- 6 files changed, 100 insertions(+), 129 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d8b3d57..d180845 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,7 +48,7 @@ jobs: python3 -m pip install --upgrade pip python3 -m pip install pylint - name: Script - run: pylint -f colorized --py-version 3.7 -d design,C0103,W0311,C0301,C0302,C0209 --load-plugins pylint.extensions.code_style,pylint.extensions.emptystring,pylint.extensions.comparetozero,pylint.extensions.comparison_placement,pylint.extensions.for_any_all,pylint.extensions.consider_refactoring_into_while_condition,pylint.extensions.consider_ternary_expression,pylint.extensions.dict_init_mutate,pylint.extensions.docstyle,pylint.extensions.check_elif,pylint.extensions.set_membership,pylint.extensions.typing -e R6104 -r y python/*.py + run: pylint -f colorized --py-version 3.7 -d design,C0103,W0311,C0301,C0302,C0209 --load-plugins pylint.extensions.code_style,pylint.extensions.comparison_placement,pylint.extensions.for_any_all,pylint.extensions.consider_refactoring_into_while_condition,pylint.extensions.consider_ternary_expression,pylint.extensions.dict_init_mutate,pylint.extensions.docstyle,pylint.extensions.check_elif,pylint.extensions.set_membership,pylint.extensions.typing -e R6104,C1804,C1805 -r y python/*.py continue-on-error: true Ruff: @@ -62,7 +62,7 @@ jobs: python3 -m pip install --upgrade pip python3 -m pip install ruff - name: Script - run: ruff --output-format=github --target-version py37 --select F,E,W,I,D,UP,YTT,S,BLE,B,A,C4,T10,EM,EXE,ISC,ICN,G,PIE,PYI,Q,RSE,RET,SLF,SLOT,SIM,TID,TCH,ARG,PGH,PL,TRY,FLY,PERF,FURB,LOG,RUF --preview --ignore E101,E501,W191,D211,D213,D401,PLR09,PLR2004,RUF001,RUF002,RUF003 . + run: ruff --output-format=github --target-version py37 --select F,E4,E7,E9,W,I,D,UP,YTT,ANN,S,BLE,B,A,COM819,C4,T10,EM,EXE,ISC,ICN,G,PIE,PYI,Q,RSE,RET,SLF,SLOT,SIM,TID,TCH,ARG,PGH,PL,TRY,FLY,PERF,FURB,LOG,RUF --preview --ignore W191,D211,D213,D401,PLR09,PLR2004,RUF001,RUF002,RUF003 . continue-on-error: true Python: @@ -76,7 +76,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies diff --git a/graphs.hpp b/graphs.hpp index a35c2a1..47a40d3 100644 --- a/graphs.hpp +++ b/graphs.hpp @@ -324,7 +324,7 @@ namespace graphs : anumber < 1000 ? 0.05 : 0.5; - if (number != 0 and anumber < 1000 and power > 0) + if (number and anumber < 1000 and power > 0) { strm << setprecision(LDBL_DIG) << number; string str = strm.str(); @@ -460,7 +460,6 @@ namespace graphs const bool axisunitslabel = aoptions.axisunitslabel; const type_type type = aoptions.type; const char *const title = aoptions.title; - const style_type style = aoptions.style; if (height == 0) return 1; @@ -519,14 +518,16 @@ namespace graphs if (title and title[0] != '\0') cout << wrap(title, awidth) << '\n'; + const char *const *astyle = styles[aoptions.style]; + if (border) { - cout << styles[style][2]; + cout << astyle[2]; for (size_t k = 0; k < awidth; ++k) - cout << styles[style][0]; + cout << astyle[0]; - cout << styles[style][4] << '\n'; + cout << astyle[4] << '\n'; } for (size_t i = 0; i < height; i += ai) @@ -561,7 +562,7 @@ namespace graphs } if (border) - cout << styles[style][1]; + cout << astyle[1]; for (size_t j = 0; j < width; j += aj) { @@ -574,19 +575,19 @@ namespace graphs { if (axaxis and ayaxis) { - cout << styles[style][6]; + cout << astyle[6]; output = true; } else if (axaxis) { if (i == 0) { - cout << styles[style][4]; + cout << astyle[4]; output = true; } else if (i >= (height - ai)) { - cout << styles[style][10]; + cout << astyle[10]; output = true; } else if (axistick) @@ -597,14 +598,14 @@ namespace graphs { if (i <= k and (i + ai) > k) { - cout << styles[style][xaxis >= aj ? 7 : 5]; + cout << astyle[xaxis >= aj ? 7 : 5]; output = true; } } } if (!output) { - cout << styles[style][1]; + cout << astyle[1]; output = true; } } @@ -612,12 +613,12 @@ namespace graphs { if (j == 0) { - cout << styles[style][2]; + cout << astyle[2]; output = true; } else if (j >= (width - aj)) { - cout << styles[style][4]; + cout << astyle[4]; output = true; } else if (axistick) @@ -628,25 +629,25 @@ namespace graphs { if (j <= k and (j + aj) > k) { - cout << styles[style][yaxis <= (height - ai) ? 3 : 9]; + cout << astyle[yaxis <= (height - ai) ? 3 : 9]; output = true; } } } if (!output) { - cout << styles[style][0]; + cout << astyle[0]; output = true; } } else if (yaxislabel and xaxislabel and axistick and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0) { - cout << "0"; + cout << '0'; output = true; } else if ((xaxis <= (width - aj) ? j >= (width - aj) : j == 0) and yaxislabel and axislabel) { - cout << "x"; + cout << 'x'; output = true; } else if (yaxislabel and axistick and axisunitslabel) @@ -692,7 +693,7 @@ namespace graphs } else if ((yaxis >= ai ? i == 0 : i >= (height - ai)) and xaxislabel and axislabel) { - cout << "y"; + cout << 'y'; output = true; } else if (ylabellength and (xaxis < aj ? xaxislabel : j < (xaxis - ylabellength) and (j + aj) >= (xaxis - ylabellength)) and (yaxis >= ai or i < (height - ai)) and axistick and axisunitslabel) @@ -751,7 +752,7 @@ namespace graphs } if (border) - cout << styles[style][1]; + cout << astyle[1]; if (i < (height - ai) or border) cout << '\n'; @@ -759,12 +760,12 @@ namespace graphs if (border) { - cout << styles[style][8]; + cout << astyle[8]; for (size_t k = 0; k < awidth; ++k) - cout << styles[style][0]; + cout << astyle[0]; - cout << styles[style][10]; + cout << astyle[10]; } cout << endl; @@ -972,7 +973,7 @@ namespace graphs for (size_t j = 0; j < graphs::size(arrays); ++j) { const auto &array = arrays[j]; - const unsigned int acolor = graphs::size(arrays) == 1 ? color + 1 : (j % (graphs::size(colors) - 2)) + 3; + const unsigned acolor = graphs::size(arrays) == 1 ? color + 1 : (j % (graphs::size(colors) - 2)) + 3; for (size_t i = 0; i < graphs::size(array); ++i) { diff --git a/python/graphs.py b/python/graphs.py index 4bb4ac5..ad4947c 100644 --- a/python/graphs.py +++ b/python/graphs.py @@ -334,14 +334,12 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if title: print(textwrap.fill(title, width=awidth)) + astyle = styles[style] + strm = "" if border: - strm += styles[style][2] - - strm += styles[style][0] * awidth - - strm += styles[style][4] + "\n" + strm += astyle[2] + (astyle[0] * awidth) + astyle[4] + "\n" i = 0 while i < height: @@ -369,7 +367,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: ylabellength *= aj if border: - strm += styles[style][1] + strm += astyle[1] j = 0 while j < width: @@ -380,14 +378,14 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if axis: if axaxis and ayaxis: - strm += styles[style][6] + strm += astyle[6] output = True elif axaxis: if i == 0: - strm += styles[style][4] + strm += astyle[4] output = True elif i >= height - ai: - strm += styles[style][10] + strm += astyle[10] output = True elif axistick: adivisor = -ydivisor if i < yaxis else ydivisor @@ -395,18 +393,18 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: k = yaxis + adivisor while (k >= i if i < yaxis else k < i + ai) and i >= ai and not output: if i <= k < i + ai: - strm += styles[style][7 if xaxis >= aj else 5] + strm += astyle[7 if xaxis >= aj else 5] output = True k += adivisor if not output: - strm += styles[style][1] + strm += astyle[1] output = True elif ayaxis: if j == 0: - strm += styles[style][2] + strm += astyle[2] output = True elif j >= width - aj: - strm += styles[style][4] + strm += astyle[4] output = True elif axistick: adivisor = -xdivisor if j < xaxis else xdivisor @@ -414,12 +412,11 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: k = xaxis + adivisor while (k >= j if j < xaxis else k < j + aj) and j < width - ai and not output: if j <= k < j + aj: - strm += styles[style][3 if yaxis <= - height - ai else 9] + strm += astyle[3 if yaxis <= height - ai else 9] output = True k += adivisor if not output: - strm += styles[style][0] + strm += astyle[0] output = True elif yaxislabel and xaxislabel and axistick and axisunitslabel and ymin <= 0 <= ymax and xmin <= 0 <= xmax: strm += "0" @@ -503,25 +500,21 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: j += aj if border: - strm += styles[style][1] + strm += astyle[1] if i < height - ai or border: strm += "\n" i += ai if border: - strm += styles[style][8] - - strm += styles[style][0] * awidth - - strm += styles[style][10] + strm += astyle[8] + (astyle[0] * awidth) + astyle[10] print(strm) return 0 -def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[Sequence[float]]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int: +def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[float], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int: """Convert one or more arrays to graph and output.""" if not aarray: return 1 diff --git a/python/tables.py b/python/tables.py index e3dde5e..040566f 100644 --- a/python/tables.py +++ b/python/tables.py @@ -74,116 +74,104 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = else: width += 2 * padding * columns - if check: - if width > w.columns: - print( - f"The width of the table ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) - return 1 + if check and width > w.columns: + print( + f"The width of the table ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) + return 1 if title: print(textwrap.fill(title, width=width)) + astyle = styles[style] + strm = "" if tableborder: - strm += styles[style][2] + strm += astyle[2] for j in range(columns): - strm += styles[style][0] * (2 * padding + columnwidth[j]) + strm += astyle[0] * (2 * padding + columnwidth[j]) if j < columns - 1: if cellborder or headerrow or (j == 0 and headercolumn): - strm += styles[style][3] + strm += astyle[3] else: - strm += styles[style][0] + strm += astyle[0] - strm += styles[style][4] + "\n" + strm += astyle[4] + "\n" for i in range(rows): if tableborder: - strm += styles[style][1] + strm += astyle[1] for j in range(columns): if (j > 0 and cellborder) or (i == 0 and j > 0 and headerrow) or (j == 1 and headercolumn): - strm += styles[style][1] + strm += astyle[1] elif j > 0 and (tableborder or (i > 0 and headerrow) or headercolumn): strm += " " awidth = columnwidth[j] - (strcol(array[i][j]) - len(array[i][j])) if (i == 0 and headerrow) or (j == 0 and headercolumn): - strm += " " * padding - - strm += "\033[1m" + array[i][j].center(awidth) + "\033[22m" - - strm += " " * padding + strm += (" " * padding) + "\033[1m" + array[i][j].center(awidth) + "\033[22m" + (" " * padding) else: - strm += " " * padding - - if alignment is None: - strm += f"{array[i][j]:{awidth}}" - elif alignment: - strm += array[i][j].rjust(awidth) - else: - strm += array[i][j].ljust(awidth) - - strm += " " * padding + strm += (" " * padding) + (f"{array[i][j]:{awidth}}" if alignment is None else array[i][j].rjust(awidth) if alignment else array[i][j].ljust(awidth)) + (" " * padding) if tableborder: - strm += styles[style][1] + strm += astyle[1] if i < rows - 1 or tableborder: strm += "\n" if (i < rows - 1 and cellborder) or (i == 0 and headerrow) or (i < rows - 1 and headercolumn): if tableborder and (cellborder or (i == 0 and headerrow) or headercolumn): - strm += styles[style][5] + strm += astyle[5] for j in range(columns): if cellborder or (i == 0 and headerrow) or (j == 0 and headercolumn): - strm += styles[style][0] * (2 * padding + columnwidth[j]) + strm += astyle[0] * (2 * padding + columnwidth[j]) elif headercolumn: strm += " " * (2 * padding + columnwidth[j]) if j < columns - 1: if cellborder or ((i == 0 and headerrow) and (j == 0 and headercolumn)): - strm += styles[style][6] + strm += astyle[6] elif i == 0 and headerrow: - strm += styles[style][9] + strm += astyle[9] elif headercolumn: if j == 0: - strm += styles[style][7] + strm += astyle[7] else: strm += " " if tableborder: if cellborder or (i == 0 and headerrow): - strm += styles[style][7] + strm += astyle[7] elif headercolumn: - strm += styles[style][1] + strm += astyle[1] strm += "\n" if tableborder: - strm += styles[style][8] + strm += astyle[8] for j in range(columns): - strm += styles[style][0] * (2 * padding + columnwidth[j]) + strm += astyle[0] * (2 * padding + columnwidth[j]) if j < columns - 1: if cellborder or (j == 0 and headercolumn): - strm += styles[style][9] + strm += astyle[9] else: - strm += styles[style][0] + strm += astyle[0] - strm += styles[style][10] + strm += astyle[10] print(strm) return 0 -def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = None, aheadercolumn: Optional[Sequence[Any]] = None, headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: +def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] = None, aheadercolumn: Optional[Sequence[str]] = None, headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: """Convert array to char array and output as table.""" if not aarray: return 1 @@ -253,11 +241,7 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla aheaderrow = [""] * columns - if len(afunctions) == 1: - aheaderrow = aaheaderrow - else: - aheaderrow = aaheaderrow[:-1] + [aaheaderrow[-1] + - str(j - length + 2) for j in range(1, columns)] + aheaderrow = aaheaderrow if len(afunctions) == 1 else aaheaderrow[:-1] + [aaheaderrow[-1] + str(j - length + 2) for j in range(1, columns)] aarray = [[0 for j in range(columns)] for i in range(rows)] diff --git a/python/test.py b/python/test.py index 9ab8865..262e395 100644 --- a/python/test.py +++ b/python/test.py @@ -12,15 +12,15 @@ import graphs import tables -def afunction(x: float): +def afunction(x: float) -> float: return x + 1 -def function1(x: float): +def function1(x: float) -> float: return 2 * x -def function2(x: float): +def function2(x: float) -> float: return x ** 2 diff --git a/tables.hpp b/tables.hpp index 4b86a79..e051951 100644 --- a/tables.hpp +++ b/tables.hpp @@ -179,7 +179,6 @@ namespace tables const bool cellborder = aoptions.cellborder; const unsigned padding = aoptions.padding; const char *const title = aoptions.title; - const style_type style = aoptions.style; const size_t rows = array.size(); const size_t columns = array[0].size(); @@ -225,36 +224,38 @@ namespace tables if (aoptions.alignment) cout << aoptions.alignment; + const char *const *astyle = styles[aoptions.style]; + if (tableborder) { - cout << styles[style][2]; + cout << astyle[2]; for (size_t j = 0; j < columns; ++j) { for (size_t k = 0; k < (2 * padding) + columnwidth[j]; ++k) - cout << styles[style][0]; + cout << astyle[0]; if (j < (columns - 1)) { if (cellborder or headerrow or (j == 0 and headercolumn)) - cout << styles[style][3]; + cout << astyle[3]; else - cout << styles[style][0]; + cout << astyle[0]; } } - cout << styles[style][4] << '\n'; + cout << astyle[4] << '\n'; } for (size_t i = 0; i < rows; ++i) { if (tableborder) - cout << styles[style][1]; + cout << astyle[1]; for (size_t j = 0; j < columns; ++j) { if ((j and cellborder) or (i == 0 and j and headerrow) or (j == 1 and headercolumn)) - cout << styles[style][1]; + cout << astyle[1]; else if (j and (tableborder or (i and headerrow) or headercolumn)) cout << ' '; @@ -264,24 +265,16 @@ namespace tables { const int apadding = (difference / 2); - cout << string(padding + apadding, ' '); - - cout << "\e[1m" << array[i][j] << "\e[22m"; - - cout << string(padding + (difference - apadding), ' '); + cout << string(padding + apadding, ' ') << "\e[1m" << array[i][j] << "\e[22m" << string(padding + (difference - apadding), ' '); } else { - cout << string(padding, ' '); - - cout << setw(difference + array[i][j].length()) << array[i][j]; - - cout << string(padding, ' '); + cout << string(padding, ' ') << setw(difference + array[i][j].length()) << array[i][j] << string(padding, ' '); } } if (tableborder) - cout << styles[style][1]; + cout << astyle[1]; if (i < (rows - 1) or tableborder) cout << '\n'; @@ -291,27 +284,27 @@ namespace tables if (tableborder) { if (cellborder or (i == 0 and headerrow) or headercolumn) - cout << styles[style][5]; + cout << astyle[5]; } for (size_t j = 0; j < columns; ++j) { if (cellborder or (i == 0 and headerrow) or (j == 0 and headercolumn)) for (size_t k = 0; k < (2 * padding) + columnwidth[j]; ++k) - cout << styles[style][0]; + cout << astyle[0]; else if (headercolumn) cout << string((2 * padding) + columnwidth[j], ' '); if (j < (columns - 1)) { if (cellborder or ((i == 0 and headerrow) and (j == 0 and headercolumn))) - cout << styles[style][6]; + cout << astyle[6]; else if (i == 0 and headerrow) - cout << styles[style][9]; + cout << astyle[9]; else if (headercolumn) { if (j == 0) - cout << styles[style][7]; + cout << astyle[7]; else cout << ' '; } @@ -321,9 +314,9 @@ namespace tables if (tableborder) { if (cellborder or (i == 0 and headerrow)) - cout << styles[style][7]; + cout << astyle[7]; else if (headercolumn) - cout << styles[style][1]; + cout << astyle[1]; } cout << '\n'; @@ -332,23 +325,23 @@ namespace tables if (tableborder) { - cout << styles[style][8]; + cout << astyle[8]; for (size_t j = 0; j < columns; ++j) { for (size_t k = 0; k < (2 * padding) + columnwidth[j]; ++k) - cout << styles[style][0]; + cout << astyle[0]; if (j < (columns - 1)) { if (cellborder or (j == 0 and headercolumn)) - cout << styles[style][9]; + cout << astyle[9]; else - cout << styles[style][0]; + cout << astyle[0]; } } - cout << styles[style][10]; + cout << astyle[10]; } cout << endl;