diff --git a/python/graphs.py b/python/graphs.py index f85ce42..d22acdd 100644 --- a/python/graphs.py +++ b/python/graphs.py @@ -24,6 +24,7 @@ if sys.platform != "win32": def wcswidth(astr: str) -> int: return libc.wcswidth(astr, len(astr)) + else: from wcwidth import wcswidth @@ -49,7 +50,7 @@ styles = ( ("═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"), # Double ("─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"), # Light Arc ("╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"), # Light Dashed - ("╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛") # Heavy Dashed + ("╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"), # Heavy Dashed # (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #No border ) @@ -76,6 +77,7 @@ class color_types(IntEnum): colors = (39, 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97) +# fmt: off dots = ( "⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝", @@ -110,6 +112,7 @@ separated_blocks_sextant = ("\xA0", "𜹑", "𜹒", "𜹓", "𜹔", "𜹕", " blocks_octant = ("\xA0", "𜺨", "𜺫", "🮂", "𜴀", "▘", "𜴁", "𜴂", "𜴃", "𜴄", "▝", "𜴅", "𜴆", "𜴇", "𜴈", "▀", "𜴉", "𜴊", "𜴋", "𜴌", "🯦", "𜴍", "𜴎", "𜴏", "𜴐", "𜴑", "𜴒", "𜴓", "𜴔", "𜴕", "𜴖", "𜴗", "𜴘", "𜴙", "𜴚", "𜴛", "𜴜", "𜴝", "𜴞", "𜴟", "🯧", "𜴠", "𜴡", "𜴢", "𜴣", "𜴤", "𜴥", "𜴦", "𜴧", "𜴨", "𜴩", "𜴪", "𜴫", "𜴬", "𜴭", "𜴮", "𜴯", "𜴰", "𜴱", "𜴲", "𜴳", "𜴴", "𜴵", "🮅", "𜺣", "𜴶", "𜴷", "𜴸", "𜴹", "𜴺", "𜴻", "𜴼", "𜴽", "𜴾", "𜴿", "𜵀", "𜵁", "𜵂", "𜵃", "𜵄", "▖", "𜵅", "𜵆", "𜵇", "𜵈", "▌", "𜵉", "𜵊", "𜵋", "𜵌", "▞", "𜵍", "𜵎", "𜵏", "𜵐", "▛", "𜵑", "𜵒", "𜵓", "𜵔", "𜵕", "𜵖", "𜵗", "𜵘", "𜵙", "𜵚", "𜵛", "𜵜", "𜵝", "𜵞", "𜵟", "𜵠", "𜵡", "𜵢", "𜵣", "𜵤", "𜵥", "𜵦", "𜵧", "𜵨", "𜵩", "𜵪", "𜵫", "𜵬", "𜵭", "𜵮", "𜵯", "𜵰", "𜺠", "𜵱", "𜵲", "𜵳", "𜵴", "𜵵", "𜵶", "𜵷", "𜵸", "𜵹", "𜵺", "𜵻", "𜵼", "𜵽", "𜵾", "𜵿", "𜶀", "𜶁", "𜶂", "𜶃", "𜶄", "𜶅", "𜶆", "𜶇", "𜶈", "𜶉", "𜶊", "𜶋", "𜶌", "𜶍", "𜶎", "𜶏", "▗", "𜶐", "𜶑", "𜶒", "𜶓", "▚", "𜶔", "𜶕", "𜶖", "𜶗", "▐", "𜶘", "𜶙", "𜶚", "𜶛", "▜", "𜶜", "𜶝", "𜶞", "𜶟", "𜶠", "𜶡", "𜶢", "𜶣", "𜶤", "𜶥", "𜶦", "𜶧", "𜶨", "𜶩", "𜶪", "𜶫", "▂", "𜶬", "𜶭", "𜶮", "𜶯", "𜶰", "𜶱", "𜶲", "𜶳", "𜶴", "𜶵", "𜶶", "𜶷", "𜶸", "𜶹", "𜶺", "𜶻", "𜶼", "𜶽", "𜶾", "𜶿", "𜷀", "𜷁", "𜷂", "𜷃", "𜷄", "𜷅", "𜷆", "𜷇", "𜷈", "𜷉", "𜷊", "𜷋", "𜷌", "𜷍", "𜷎", "𜷏", "𜷐", "𜷑", "𜷒", "𜷓", "𜷔", "𜷕", "𜷖", "𜷗", "𜷘", "𜷙", "𜷚", "▄", "𜷛", "𜷜", "𜷝", "𜷞", "▙", "𜷟", "𜷠", "𜷡", "𜷢", "▟", "𜷣", "▆", "𜷤", "𜷥", "█") bars = ("\xA0", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█") +# fmt: on class type_types(IntEnum): @@ -120,14 +123,26 @@ class type_types(IntEnum): block_sextant = auto() separated_block_sextant = auto() block_octant = auto() - histogram = auto() # Set automatically by the histogram() function + histogram = auto() # Set automatically by the histogram() function -atype_types = (type_types.braille, type_types.block, type_types.block_quadrant, type_types.separated_block_quadrant, type_types.block_sextant, type_types.separated_block_sextant, type_types.block_octant) + +atype_types = ( + type_types.braille, + type_types.block, + type_types.block_quadrant, + type_types.separated_block_quadrant, + type_types.block_sextant, + type_types.separated_block_sextant, + type_types.block_octant, +) densities = ((4, 2), (1, 1), (2, 2), (2, 2), (3, 2), (3, 2), (4, 2), (8, 1)) -marks = (((0, 0),), ((0, 1), (-1, 0), (0, 0), (1, 0), (0, -1)), - ((-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1))) +marks = ( + ((0, 0),), + ((0, 1), (-1, 0), (0, 0), (1, 0), (0, -1)), + ((-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)), +) class mark_types(IntEnum): @@ -154,13 +169,10 @@ fractions = { "⅛": Fraction(1, 8), "⅜": Fraction(3, 8), "⅝": Fraction(5, 8), - "⅞": Fraction(7, 8) + "⅞": Fraction(7, 8), } -constants = { - "π": math.pi, - "e": math.e -} +constants = {"π": math.pi, "e": math.e} class units_types(Enum): @@ -178,7 +190,7 @@ class units_types(Enum): suffix_power_char = ("", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q") -MAX = sys.float_info.radix ** sys.float_info.mant_dig - 1 +MAX = sys.float_info.radix**sys.float_info.mant_dig - 1 def strcol(astr: str) -> int: @@ -233,8 +245,7 @@ def outputunit(number: float, scale: units_types) -> str: strm = locale.format_string("%.0f", number, grouping=True) # "k" if power == 1 and scale == scale_SI else - strm += suffix_power_char[power] if power < len( - suffix_power_char) else "(error)" + strm += suffix_power_char[power] if power < len(suffix_power_char) else "(error)" if scale == units_types.scale_IEC_I and power > 0: strm += "i" @@ -314,7 +325,27 @@ def outputcolor(color: color_types) -> str: return f"\033[{colors[color]}m" -def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], 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, atype: type_types = type_types.braille, style: style_types = style_types.light, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int: +def graph( + height: int, + width: int, + xmin: float, + xmax: float, + ymin: float, + ymax: float, + array: List[List[int]], + 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, + atype: type_types = type_types.braille, + style: style_types = style_types.light, + title: Optional[str] = None, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Output graph.""" if not array: return 1 @@ -334,13 +365,11 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if check: if aheight > w.lines: - print( - f"The height of the graph ({aheight}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) + print(f"The height of the graph ({aheight}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) return 1 if awidth > w.columns: - print( - f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) + print(f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) return 1 if xmin >= xmax: @@ -486,7 +515,13 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: elif (not i if yaxis >= ai else i >= height - ai) and xaxislabel and axislabel: strm += "y" output = True - elif ylabellength and (xaxislabel if xaxis < aj else j < xaxis - ylabellength and j + aj >= xaxis - ylabellength) and (yaxis >= ai or i < height - ai) and axistick and axisunitslabel: + elif ( + ylabellength + and (xaxislabel if xaxis < aj else j < xaxis - ylabellength and j + aj >= xaxis - ylabellength) + and (yaxis >= ai or i < height - ai) + and axistick + and axisunitslabel + ): strm += ylabelstrm output = True if ylabellength > aj: @@ -502,7 +537,14 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if value: if atype == type_types.braille: dot += dotvalues[k][l] - elif atype in {type_types.block, type_types.block_quadrant, type_types.separated_block_quadrant, type_types.block_sextant, type_types.separated_block_sextant, type_types.block_octant}: + elif atype in { + type_types.block, + type_types.block_quadrant, + type_types.separated_block_quadrant, + type_types.block_sextant, + type_types.separated_block_sextant, + type_types.block_octant, + }: dot += 1 << (l * aj + k) elif atype == type_types.histogram: if not dot: @@ -556,7 +598,27 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: return 0 -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, file: TextIO = sys.stdout, 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, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert one or more arrays to graph and output.""" if not aarray: return 1 @@ -571,13 +633,11 @@ def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ym if check: if height > w.lines: - print( - f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) + print(f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) return 1 if width > w.columns: - print( - f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) + print(f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) return 1 ai, aj = densities[type_types.histogram] @@ -623,10 +683,51 @@ def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ym aaarray[x][y] = acolor y += 1 - return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, type_types.histogram, style, title, file) + return graph( + height, + width, + xmin, + xmax, + ymin, + ymax, + aaarray, + border, + axis, + axislabel, + axistick, + axisunitslabel, + xunits, + yunits, + type_types.histogram, + style, + title, + file, + ) -def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: 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, atype: type_types = type_types.braille, mark: mark_types = mark_types.dot, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int: +def plots( + height: int, + width: int, + xmin: float, + xmax: float, + ymin: float, + ymax: float, + aarrays: 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, + atype: type_types = type_types.braille, + mark: mark_types = mark_types.dot, + style: style_types = style_types.light, + color: color_types = color_types.red, + title: Optional[str] = None, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert one or more arrays to graph and output.""" if not aarrays: return 1 @@ -645,13 +746,11 @@ def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if check: if height > w.lines: - print( - f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) + print(f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) return 1 if width > w.columns: - print( - f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) + print(f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) return 1 ai, aj = densities[atype] @@ -701,15 +800,99 @@ def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: else: aaarray[x][y] = acolor - return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, title, file) + return graph( + height, + width, + xmin, + xmax, + ymin, + ymax, + aaarray, + border, + axis, + axislabel, + axistick, + axisunitslabel, + xunits, + yunits, + atype, + style, + title, + file, + ) -def plot(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: 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, atype: type_types = type_types.braille, mark: mark_types = mark_types.dot, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int: +def plot( + height: int, + width: int, + xmin: float, + xmax: float, + ymin: float, + ymax: float, + aarray: 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, + atype: type_types = type_types.braille, + mark: mark_types = mark_types.dot, + style: style_types = style_types.light, + color: color_types = color_types.red, + title: Optional[str] = None, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert single array to graph and output.""" - return plots(height, width, xmin, xmax, ymin, ymax, (aarray,), border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, mark, style, color, title, file, check) + return plots( + height, + width, + xmin, + xmax, + ymin, + ymax, + (aarray,), + border, + axis, + axislabel, + axistick, + axisunitslabel, + xunits, + yunits, + atype, + mark, + style, + color, + title, + file, + check, + ) -def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], 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, atype: type_types = type_types.braille, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int: +def functions( + height: int, + width: int, + xmin: float, + xmax: float, + ymin: float, + ymax: float, + afunctions: Sequence[Callable[[float], 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, + atype: type_types = type_types.braille, + style: style_types = style_types.light, + color: color_types = color_types.red, + title: Optional[str] = None, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert one or more functions to graph and output.""" if not afunctions: return 1 @@ -724,13 +907,11 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym if check: if height > w.lines: - print( - f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) + print(f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr) return 1 if height > w.columns: - print( - f"The width of the graph ({height}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) + print(f"The width of the graph ({height}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr) return 1 ai, aj = densities[atype] @@ -757,8 +938,7 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym array = [[0 for j in range(height)] for i in range(width)] for j, function in enumerate(afunctions): - acolor = color + \ - 1 if len(afunctions) == 1 else j % (len(colors) - 2) + 3 + acolor = color + 1 if len(afunctions) == 1 else j % (len(colors) - 2) + 3 for i in (x / xres for x in range(rows * xres)): x = i * xstep + xmin @@ -774,9 +954,70 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym else: array[ax][ay] = acolor - return graph(height, width, xmin, xmax, ymin, ymax, array, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, title, file) + return graph( + height, + width, + xmin, + xmax, + ymin, + ymax, + array, + border, + axis, + axislabel, + axistick, + axisunitslabel, + xunits, + yunits, + atype, + style, + title, + file, + ) -def function(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], 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, atype: type_types = type_types.braille, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int: +def function( + height: int, + width: int, + xmin: float, + xmax: float, + ymin: float, + ymax: float, + afunction: Callable[[float], 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, + atype: type_types = type_types.braille, + style: style_types = style_types.light, + color: color_types = color_types.red, + title: Optional[str] = None, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert single function to function array and output.""" - return functions(height, width, xmin, xmax, ymin, ymax, (afunction,), border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, color, title, file, check) + return functions( + height, + width, + xmin, + xmax, + ymin, + ymax, + (afunction,), + border, + axis, + axislabel, + axistick, + axisunitslabel, + xunits, + yunits, + atype, + style, + color, + title, + file, + check, + ) diff --git a/python/tables.py b/python/tables.py index 2d5cb26..a494168 100644 --- a/python/tables.py +++ b/python/tables.py @@ -22,6 +22,7 @@ if sys.platform != "win32": def wcswidth(astr: str) -> int: return libc.wcswidth(astr, len(astr)) + else: from wcwidth import wcswidth @@ -47,7 +48,7 @@ styles = ( ("═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"), # Double ("─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"), # Light Arc ("╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"), # Light Dashed - ("╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛") # Heavy Dashed + ("╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"), # Heavy Dashed # (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #No border ) @@ -65,8 +66,19 @@ def strcol(astr: str) -> int: # return len(astr) -def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, - padding: int = 1, alignment: str = "", title: Optional[str] = None, style: style_types = style_types.light, file: TextIO = sys.stdout, check: bool = True) -> int: +def table( + array: List[List[str]], + headerrow: bool = False, + headercolumn: bool = False, + tableborder: bool = True, + cellborder: bool = False, + padding: int = 1, + alignment: str = "", + title: Optional[str] = None, + style: style_types = style_types.light, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Output char array as table.""" if not array: return 1 @@ -86,8 +98,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = width += 2 * padding * columns 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) + 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: @@ -182,7 +193,21 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = return 0 -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: str = "", title: Optional[str] = None, style: style_types = style_types.light, file: TextIO = sys.stdout, check: bool = True) -> 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: str = "", + title: Optional[str] = None, + style: style_types = style_types.light, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert array to char array and output as table.""" if not aarray: return 1 @@ -191,9 +216,7 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] = columns = len(aarray[0]) if not all(len(x) == columns for x in aarray): - print( - "Error: The rows of the array must have the same number of columns.", - file=sys.stderr) + print("Error: The rows of the array must have the same number of columns.", file=sys.stderr) return 1 if aheaderrow: @@ -203,15 +226,11 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] = columns += 1 if aheaderrow and len(aheaderrow) != columns: - print( - "Error: The header row must have the same number of columns as the array.", - file=sys.stderr) + print("Error: The header row must have the same number of columns as the array.", file=sys.stderr) return 1 if aheadercolumn and len(aheadercolumn) != (rows - 1 if aheaderrow else rows): - print( - "Error: The header column must have the same number of rows as the array.", - file=sys.stderr) + print("Error: The header column must have the same number of rows as the array.", file=sys.stderr) return 1 aaarray = [["" for j in range(columns)] for i in range(rows)] @@ -226,12 +245,27 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] = aaarray[i][0] = aheadercolumn[ii] j = 1 if aheadercolumn else 0 - aaarray[i][j:] = map(str, aarray[ii][:columns - j]) + aaarray[i][j:] = map(str, aarray[ii][: columns - j]) return table(aaarray, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style, file, check) -def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: str = "", title: Optional[str] = None, style: style_types = style_types.light, file: TextIO = sys.stdout, check: bool = True) -> int: +def functions( + xmin: float, + xmax: float, + xstep: float, + afunctions: Sequence[Callable[[float], float]], + headerrow: bool = False, + headercolumn: bool = False, + tableborder: bool = True, + cellborder: bool = False, + padding: int = 1, + alignment: str = "", + title: Optional[str] = None, + style: style_types = style_types.light, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert one or more functions to array and output as table.""" if not afunctions: return 1 @@ -252,7 +286,11 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla aheaderrow = [""] * columns - aheaderrow = aaheaderrow if len(afunctions) == 1 else 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)] @@ -261,9 +299,41 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla aarray[i][1:] = [function(temp) for function in afunctions] - return array(aarray, aheaderrow, None, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style, file, check) + return array( + aarray, aheaderrow, None, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style, file, check + ) -def function(xmin: float, xmax: float, xstep: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: str = "", title: Optional[str] = None, style: style_types = style_types.light, file: TextIO = sys.stdout, check: bool = True) -> int: +def function( + xmin: float, + xmax: float, + xstep: float, + afunction: Callable[[float], float], + headerrow: bool = False, + headercolumn: bool = False, + tableborder: bool = True, + cellborder: bool = False, + padding: int = 1, + alignment: str = "", + title: Optional[str] = None, + style: style_types = style_types.light, + file: TextIO = sys.stdout, + check: bool = True, +) -> int: """Convert single function to array and output as table.""" - return functions(xmin, xmax, xstep, (afunction,), headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style, file, check) + return functions( + xmin, + xmax, + xstep, + (afunction,), + headerrow, + headercolumn, + tableborder, + cellborder, + padding, + alignment, + title, + style, + file, + check, + ) diff --git a/python/test.py b/python/test.py index 64c58df..87216e2 100644 --- a/python/test.py +++ b/python/test.py @@ -22,7 +22,7 @@ def function1(x: float) -> float: def function2(x: float) -> float: - return x ** 2 + return x**2 rows = 5 @@ -33,8 +33,7 @@ xmax = 10 xstep = 0.5 print("\nOutput array as table\n") -array = [[random.randint(0, sys.maxsize) - for j in range(columns)] for i in range(rows)] +array = [[random.randint(0, sys.maxsize) for j in range(columns)] for i in range(rows)] for style in tables.style_types: tables.array(array, None, None, style=style) @@ -43,50 +42,39 @@ for style in tables.style_types: tables.array(array, None, None, style=style) print("\nOutput char array as table\n") -array = [["Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"], - ["Header column 2", "Data 1", "Data 2", "Data 3", "Data 4"], - ["Header column 3", "Data 5", "Data 6", "Data 7", "Data 8"], - ["Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"], - ["Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"]] +array = [ + ["Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"], + ["Header column 2", "Data 1", "Data 2", "Data 3", "Data 4"], + ["Header column 3", "Data 5", "Data 6", "Data 7", "Data 8"], + ["Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"], + ["Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"], +] for style in tables.style_types: - tables.array(array, None, None, headerrow=True, - headercolumn=True, style=style) + tables.array(array, None, None, headerrow=True, headercolumn=True, style=style) print("\nOutput array as table with separate header row and column\n") -array = [[f"Data {i + j:n}" for j in range(4)] - for i in range(1, 4 * 4 + 1, 4)] -headerrow = ["Header row/column 1", "Header row 2", - "Header row 3", "Header row 4", "Header row 5"] -headercolumn = ["Header column 2", "Header column 3", - "Header column 4", "Header column 5"] +array = [[f"Data {i + j:n}" for j in range(4)] for i in range(1, 4 * 4 + 1, 4)] +headerrow = ["Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"] +headercolumn = ["Header column 2", "Header column 3", "Header column 4", "Header column 5"] for style in tables.style_types: - tables.array(array, headerrow, headercolumn, headerrow=True, - headercolumn=True, cellborder=True, style=style) - tables.array(array, headerrow, headercolumn, - headerrow=True, headercolumn=True, style=style) + tables.array(array, headerrow, headercolumn, headerrow=True, headercolumn=True, cellborder=True, style=style) + tables.array(array, headerrow, headercolumn, headerrow=True, headercolumn=True, style=style) tables.array(array, headerrow[:-1], None, headerrow=True, style=style) - tables.array(array, None, [headerrow[0]] + - headercolumn[:-1], headercolumn=True, style=style) + tables.array(array, None, [headerrow[0]] + headercolumn[:-1], headercolumn=True, style=style) tables.array(array, None, None, cellborder=True, style=style) tables.array(array, None, None, tableborder=False, style=style) - tables.array(array, headerrow, headercolumn, tableborder=False, - headerrow=True, headercolumn=True, style=style) - tables.array(array, headerrow[:-1], None, - tableborder=False, headerrow=True, style=style) - tables.array(array, None, [headerrow[0]] + headercolumn[:-1], - tableborder=False, headercolumn=True, style=style) - tables.array(array, None, None, tableborder=False, - cellborder=True, style=style) + tables.array(array, headerrow, headercolumn, tableborder=False, headerrow=True, headercolumn=True, style=style) + tables.array(array, headerrow[:-1], None, tableborder=False, headerrow=True, style=style) + tables.array(array, None, [headerrow[0]] + headercolumn[:-1], tableborder=False, headercolumn=True, style=style) + tables.array(array, None, None, tableborder=False, cellborder=True, style=style) -array = [[bool(random.getrandbits(1)) for j in range(columns)] - for i in range(rows)] +array = [[bool(random.getrandbits(1)) for j in range(columns)] for i in range(rows)] for style in tables.style_types: tables.array(array, None, None, style=style) print("\nOutput sorted array as table\n") -array = ([random.randint(0, sys.maxsize) - for j in range(columns)] for i in range(rows)) +array = ([random.randint(0, sys.maxsize) for j in range(columns)] for i in range(rows)) sortdimension = 0 array = sorted(array, key=operator.itemgetter(sortdimension)) for style in tables.style_types: @@ -96,16 +84,13 @@ print("\nOutput single function as table\n") for style in tables.style_types: tables.function(xmin, xmax, xstep, afunction, headerrow=True, style=style) for style in tables.style_types: - tables.function(xmin, xmax, xstep, lambda x: x + - 1, headerrow=True, style=style) + tables.function(xmin, xmax, xstep, lambda x: x + 1, headerrow=True, style=style) print("\nOutput multiple functions as table\n") for style in tables.style_types: - tables.functions(xmin, xmax, xstep, [ - function1, function2], headerrow=True, style=style) + tables.functions(xmin, xmax, xstep, [function1, function2], headerrow=True, style=style) for style in tables.style_types: - tables.functions(xmin, xmax, xstep, [ - lambda x: 2 * x, lambda x: x ** 2], headerrow=True, style=style) + tables.functions(xmin, xmax, xstep, [lambda x: 2 * x, lambda x: x**2], headerrow=True, style=style) height = 40 width = 80 @@ -125,25 +110,21 @@ array = [range(i, i + 2) for i in range(10)] for atype in graphs.atype_types: for mark in graphs.mark_types: for style in graphs.style_types: - graphs.plot(height, width, xmin, xmax, ymin, ymax, - array, atype=atype, mark=mark, style=style) + graphs.plot(height, width, xmin, xmax, ymin, ymax, array, atype=atype, mark=mark, style=style) print("\nOutput single function as graph\n") for style in graphs.style_types: - graphs.function(height, width, xmin, xmax, ymin, - ymax, afunction, style=style) + graphs.function(height, width, xmin, xmax, ymin, ymax, afunction, style=style) for style in graphs.style_types: - graphs.function(height, width, xmin, xmax, ymin, - ymax, lambda x: x + 1, style=style) + graphs.function(height, width, xmin, xmax, ymin, ymax, lambda x: x + 1, style=style) print("\nOutput multiple functions as graph\n") for style in graphs.style_types: - graphs.functions(height, width, xmin, xmax, ymin, ymax, - [function1, function2], style=style) + graphs.functions(height, width, xmin, xmax, ymin, ymax, [function1, function2], style=style) for style in graphs.style_types: - graphs.functions(height, width, xmin, xmax, ymin, ymax, [ - lambda x: 2 * x, lambda x: x ** 2], style=style) + graphs.functions(height, width, xmin, xmax, ymin, ymax, [lambda x: 2 * x, lambda x: x**2], style=style) for style in graphs.style_types: - graphs.functions(height, width, -(2 * math.pi), 2 * math.pi, -4, 4, - [math.sin, math.cos, math.tan], axisunitslabel=False, style=style) + graphs.functions( + height, width, -(2 * math.pi), 2 * math.pi, -4, 4, [math.sin, math.cos, math.tan], axisunitslabel=False, style=style + )