Updated formatting of Python libraries.

This commit is contained in:
Teal Dulcet 2025-01-01 07:03:07 -08:00
parent ba19fd98c0
commit 35a0b10a19
3 changed files with 409 additions and 117 deletions

View File

@ -24,6 +24,7 @@ if sys.platform != "win32":
def wcswidth(astr: str) -> int: def wcswidth(astr: str) -> int:
return libc.wcswidth(astr, len(astr)) return libc.wcswidth(astr, len(astr))
else: else:
from wcwidth import wcswidth from wcwidth import wcswidth
@ -49,7 +50,7 @@ styles = (
("", "", "", "", "", "", "", "", "", "", ""), # Double ("", "", "", "", "", "", "", "", "", "", ""), # Double
("", "", "", "", "", "", "", "", "", "", ""), # Light Arc ("", "", "", "", "", "", "", "", "", "", ""), # Light Arc
("", "", "", "", "", "", "", "", "", "", ""), # Light Dashed ("", "", "", "", "", "", "", "", "", "", ""), # Light Dashed
("", "", "", "", "", "", "", "", "", "", "") # Heavy Dashed ("", "", "", "", "", "", "", "", "", "", ""), # Heavy Dashed
# (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #No border # (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #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) colors = (39, 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97)
# fmt: off
dots = ( dots = (
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
@ -110,6 +112,7 @@ separated_blocks_sextant = ("\xA0", "𜹑", "𜹒", "𜹓", "𜹔", "𜹕", "
blocks_octant = ("\xA0", "𜺨", "𜺫", "🮂", "𜴀", "", "𜴁", "𜴂", "𜴃", "𜴄", "", "𜴅", "𜴆", "𜴇", "𜴈", "", "𜴉", "𜴊", "𜴋", "𜴌", "🯦", "𜴍", "𜴎", "𜴏", "𜴐", "𜴑", "𜴒", "𜴓", "𜴔", "𜴕", "𜴖", "𜴗", "𜴘", "𜴙", "𜴚", "𜴛", "𜴜", "𜴝", "𜴞", "𜴟", "🯧", "𜴠", "𜴡", "𜴢", "𜴣", "𜴤", "𜴥", "𜴦", "𜴧", "𜴨", "𜴩", "𜴪", "𜴫", "𜴬", "𜴭", "𜴮", "𜴯", "𜴰", "𜴱", "𜴲", "𜴳", "𜴴", "𜴵", "🮅", "𜺣", "𜴶", "𜴷", "𜴸", "𜴹", "𜴺", "𜴻", "𜴼", "𜴽", "𜴾", "𜴿", "𜵀", "𜵁", "𜵂", "𜵃", "𜵄", "", "𜵅", "𜵆", "𜵇", "𜵈", "", "𜵉", "𜵊", "𜵋", "𜵌", "", "𜵍", "𜵎", "𜵏", "𜵐", "", "𜵑", "𜵒", "𜵓", "𜵔", "𜵕", "𜵖", "𜵗", "𜵘", "𜵙", "𜵚", "𜵛", "𜵜", "𜵝", "𜵞", "𜵟", "𜵠", "𜵡", "𜵢", "𜵣", "𜵤", "𜵥", "𜵦", "𜵧", "𜵨", "𜵩", "𜵪", "𜵫", "𜵬", "𜵭", "𜵮", "𜵯", "𜵰", "𜺠", "𜵱", "𜵲", "𜵳", "𜵴", "𜵵", "𜵶", "𜵷", "𜵸", "𜵹", "𜵺", "𜵻", "𜵼", "𜵽", "𜵾", "𜵿", "𜶀", "𜶁", "𜶂", "𜶃", "𜶄", "𜶅", "𜶆", "𜶇", "𜶈", "𜶉", "𜶊", "𜶋", "𜶌", "𜶍", "𜶎", "𜶏", "", "𜶐", "𜶑", "𜶒", "𜶓", "", "𜶔", "𜶕", "𜶖", "𜶗", "", "𜶘", "𜶙", "𜶚", "𜶛", "", "𜶜", "𜶝", "𜶞", "𜶟", "𜶠", "𜶡", "𜶢", "𜶣", "𜶤", "𜶥", "𜶦", "𜶧", "𜶨", "𜶩", "𜶪", "𜶫", "", "𜶬", "𜶭", "𜶮", "𜶯", "𜶰", "𜶱", "𜶲", "𜶳", "𜶴", "𜶵", "𜶶", "𜶷", "𜶸", "𜶹", "𜶺", "𜶻", "𜶼", "𜶽", "𜶾", "𜶿", "𜷀", "𜷁", "𜷂", "𜷃", "𜷄", "𜷅", "𜷆", "𜷇", "𜷈", "𜷉", "𜷊", "𜷋", "𜷌", "𜷍", "𜷎", "𜷏", "𜷐", "𜷑", "𜷒", "𜷓", "𜷔", "𜷕", "𜷖", "𜷗", "𜷘", "𜷙", "𜷚", "", "𜷛", "𜷜", "𜷝", "𜷞", "", "𜷟", "𜷠", "𜷡", "𜷢", "", "𜷣", "", "𜷤", "𜷥", "") blocks_octant = ("\xA0", "𜺨", "𜺫", "🮂", "𜴀", "", "𜴁", "𜴂", "𜴃", "𜴄", "", "𜴅", "𜴆", "𜴇", "𜴈", "", "𜴉", "𜴊", "𜴋", "𜴌", "🯦", "𜴍", "𜴎", "𜴏", "𜴐", "𜴑", "𜴒", "𜴓", "𜴔", "𜴕", "𜴖", "𜴗", "𜴘", "𜴙", "𜴚", "𜴛", "𜴜", "𜴝", "𜴞", "𜴟", "🯧", "𜴠", "𜴡", "𜴢", "𜴣", "𜴤", "𜴥", "𜴦", "𜴧", "𜴨", "𜴩", "𜴪", "𜴫", "𜴬", "𜴭", "𜴮", "𜴯", "𜴰", "𜴱", "𜴲", "𜴳", "𜴴", "𜴵", "🮅", "𜺣", "𜴶", "𜴷", "𜴸", "𜴹", "𜴺", "𜴻", "𜴼", "𜴽", "𜴾", "𜴿", "𜵀", "𜵁", "𜵂", "𜵃", "𜵄", "", "𜵅", "𜵆", "𜵇", "𜵈", "", "𜵉", "𜵊", "𜵋", "𜵌", "", "𜵍", "𜵎", "𜵏", "𜵐", "", "𜵑", "𜵒", "𜵓", "𜵔", "𜵕", "𜵖", "𜵗", "𜵘", "𜵙", "𜵚", "𜵛", "𜵜", "𜵝", "𜵞", "𜵟", "𜵠", "𜵡", "𜵢", "𜵣", "𜵤", "𜵥", "𜵦", "𜵧", "𜵨", "𜵩", "𜵪", "𜵫", "𜵬", "𜵭", "𜵮", "𜵯", "𜵰", "𜺠", "𜵱", "𜵲", "𜵳", "𜵴", "𜵵", "𜵶", "𜵷", "𜵸", "𜵹", "𜵺", "𜵻", "𜵼", "𜵽", "𜵾", "𜵿", "𜶀", "𜶁", "𜶂", "𜶃", "𜶄", "𜶅", "𜶆", "𜶇", "𜶈", "𜶉", "𜶊", "𜶋", "𜶌", "𜶍", "𜶎", "𜶏", "", "𜶐", "𜶑", "𜶒", "𜶓", "", "𜶔", "𜶕", "𜶖", "𜶗", "", "𜶘", "𜶙", "𜶚", "𜶛", "", "𜶜", "𜶝", "𜶞", "𜶟", "𜶠", "𜶡", "𜶢", "𜶣", "𜶤", "𜶥", "𜶦", "𜶧", "𜶨", "𜶩", "𜶪", "𜶫", "", "𜶬", "𜶭", "𜶮", "𜶯", "𜶰", "𜶱", "𜶲", "𜶳", "𜶴", "𜶵", "𜶶", "𜶷", "𜶸", "𜶹", "𜶺", "𜶻", "𜶼", "𜶽", "𜶾", "𜶿", "𜷀", "𜷁", "𜷂", "𜷃", "𜷄", "𜷅", "𜷆", "𜷇", "𜷈", "𜷉", "𜷊", "𜷋", "𜷌", "𜷍", "𜷎", "𜷏", "𜷐", "𜷑", "𜷒", "𜷓", "𜷔", "𜷕", "𜷖", "𜷗", "𜷘", "𜷙", "𜷚", "", "𜷛", "𜷜", "𜷝", "𜷞", "", "𜷟", "𜷠", "𜷡", "𜷢", "", "𜷣", "", "𜷤", "𜷥", "")
bars = ("\xA0", "", "", "", "", "", "", "", "") bars = ("\xA0", "", "", "", "", "", "", "", "")
# fmt: on
class type_types(IntEnum): class type_types(IntEnum):
@ -120,14 +123,26 @@ class type_types(IntEnum):
block_sextant = auto() block_sextant = auto()
separated_block_sextant = auto() separated_block_sextant = auto()
block_octant = 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)) 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)), marks = (
((-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1))) ((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): class mark_types(IntEnum):
@ -154,13 +169,10 @@ fractions = {
"": Fraction(1, 8), "": Fraction(1, 8),
"": Fraction(3, 8), "": Fraction(3, 8),
"": Fraction(5, 8), "": Fraction(5, 8),
"": Fraction(7, 8) "": Fraction(7, 8),
} }
constants = { constants = {"π": math.pi, "e": math.e}
"π": math.pi,
"e": math.e
}
class units_types(Enum): 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") 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: 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) strm = locale.format_string("%.0f", number, grouping=True)
# "k" if power == 1 and scale == scale_SI else # "k" if power == 1 and scale == scale_SI else
strm += suffix_power_char[power] if power < len( strm += suffix_power_char[power] if power < len(suffix_power_char) else "(error)"
suffix_power_char) else "(error)"
if scale == units_types.scale_IEC_I and power > 0: if scale == units_types.scale_IEC_I and power > 0:
strm += "i" strm += "i"
@ -314,7 +325,27 @@ def outputcolor(color: color_types) -> str:
return f"\033[{colors[color]}m" 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.""" """Output graph."""
if not array: if not array:
return 1 return 1
@ -334,13 +365,11 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
if check: if check:
if aheight > w.lines: if aheight > w.lines:
print( print(f"The height of the graph ({aheight}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
f"The height of the graph ({aheight}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
return 1 return 1
if awidth > w.columns: if awidth > w.columns:
print( print(f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
f"The width of the graph ({awidth}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
return 1 return 1
if xmin >= xmax: 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: elif (not i if yaxis >= ai else i >= height - ai) and xaxislabel and axislabel:
strm += "y" strm += "y"
output = True 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 strm += ylabelstrm
output = True output = True
if ylabellength > aj: if ylabellength > aj:
@ -502,7 +537,14 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
if value: if value:
if atype == type_types.braille: if atype == type_types.braille:
dot += dotvalues[k][l] 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) dot += 1 << (l * aj + k)
elif atype == type_types.histogram: elif atype == type_types.histogram:
if not dot: if not dot:
@ -556,7 +598,27 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
return 0 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.""" """Convert one or more arrays to graph and output."""
if not aarray: if not aarray:
return 1 return 1
@ -571,13 +633,11 @@ def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
if check: if check:
if height > w.lines: if height > w.lines:
print( print(f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
return 1 return 1
if width > w.columns: if width > w.columns:
print( print(f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
return 1 return 1
ai, aj = densities[type_types.histogram] 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 aaarray[x][y] = acolor
y += 1 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.""" """Convert one or more arrays to graph and output."""
if not aarrays: if not aarrays:
return 1 return 1
@ -645,13 +746,11 @@ def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
if check: if check:
if height > w.lines: if height > w.lines:
print( print(f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
return 1 return 1
if width > w.columns: if width > w.columns:
print( print(f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
return 1 return 1
ai, aj = densities[atype] ai, aj = densities[atype]
@ -701,15 +800,99 @@ def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
else: else:
aaarray[x][y] = acolor 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.""" """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.""" """Convert one or more functions to graph and output."""
if not afunctions: if not afunctions:
return 1 return 1
@ -724,13 +907,11 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
if check: if check:
if height > w.lines: if height > w.lines:
print( print(f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
return 1 return 1
if height > w.columns: if height > w.columns:
print( print(f"The width of the graph ({height}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
f"The width of the graph ({height}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
return 1 return 1
ai, aj = densities[atype] 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)] array = [[0 for j in range(height)] for i in range(width)]
for j, function in enumerate(afunctions): for j, function in enumerate(afunctions):
acolor = color + \ acolor = color + 1 if len(afunctions) == 1 else j % (len(colors) - 2) + 3
1 if len(afunctions) == 1 else j % (len(colors) - 2) + 3
for i in (x / xres for x in range(rows * xres)): for i in (x / xres for x in range(rows * xres)):
x = i * xstep + xmin x = i * xstep + xmin
@ -774,9 +954,70 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
else: else:
array[ax][ay] = acolor 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.""" """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,
)

View File

@ -22,6 +22,7 @@ if sys.platform != "win32":
def wcswidth(astr: str) -> int: def wcswidth(astr: str) -> int:
return libc.wcswidth(astr, len(astr)) return libc.wcswidth(astr, len(astr))
else: else:
from wcwidth import wcswidth from wcwidth import wcswidth
@ -47,7 +48,7 @@ styles = (
("", "", "", "", "", "", "", "", "", "", ""), # Double ("", "", "", "", "", "", "", "", "", "", ""), # Double
("", "", "", "", "", "", "", "", "", "", ""), # Light Arc ("", "", "", "", "", "", "", "", "", "", ""), # Light Arc
("", "", "", "", "", "", "", "", "", "", ""), # Light Dashed ("", "", "", "", "", "", "", "", "", "", ""), # Light Dashed
("", "", "", "", "", "", "", "", "", "", "") # Heavy Dashed ("", "", "", "", "", "", "", "", "", "", ""), # Heavy Dashed
# (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #No border # (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #No border
) )
@ -65,8 +66,19 @@ def strcol(astr: str) -> int:
# return len(astr) # return len(astr)
def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, def table(
padding: int = 1, alignment: str = "", title: Optional[str] = None, style: style_types = style_types.light, file: TextIO = sys.stdout, check: bool = True) -> int: 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.""" """Output char array as table."""
if not array: if not array:
return 1 return 1
@ -86,8 +98,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
width += 2 * padding * columns width += 2 * padding * columns
if check and width > w.columns: if check and width > w.columns:
print( print(f"The width of the table ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
f"The width of the table ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
return 1 return 1
if title: if title:
@ -182,7 +193,21 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
return 0 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.""" """Convert array to char array and output as table."""
if not aarray: if not aarray:
return 1 return 1
@ -191,9 +216,7 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] =
columns = len(aarray[0]) columns = len(aarray[0])
if not all(len(x) == columns for x in aarray): if not all(len(x) == columns for x in aarray):
print( print("Error: The rows of the array must have the same number of columns.", file=sys.stderr)
"Error: The rows of the array must have the same number of columns.",
file=sys.stderr)
return 1 return 1
if aheaderrow: if aheaderrow:
@ -203,15 +226,11 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] =
columns += 1 columns += 1
if aheaderrow and len(aheaderrow) != columns: if aheaderrow and len(aheaderrow) != columns:
print( print("Error: The header row must have the same number of columns as the array.", file=sys.stderr)
"Error: The header row must have the same number of columns as the array.",
file=sys.stderr)
return 1 return 1
if aheadercolumn and len(aheadercolumn) != (rows - 1 if aheaderrow else rows): if aheadercolumn and len(aheadercolumn) != (rows - 1 if aheaderrow else rows):
print( print("Error: The header column must have the same number of rows as the array.", file=sys.stderr)
"Error: The header column must have the same number of rows as the array.",
file=sys.stderr)
return 1 return 1
aaarray = [["" for j in range(columns)] for i in range(rows)] 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] aaarray[i][0] = aheadercolumn[ii]
j = 1 if aheadercolumn else 0 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) 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.""" """Convert one or more functions to array and output as table."""
if not afunctions: if not afunctions:
return 1 return 1
@ -252,7 +286,11 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla
aheaderrow = [""] * columns 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)] 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] 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.""" """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,
)

View File

@ -22,7 +22,7 @@ def function1(x: float) -> float:
def function2(x: float) -> float: def function2(x: float) -> float:
return x ** 2 return x**2
rows = 5 rows = 5
@ -33,8 +33,7 @@ xmax = 10
xstep = 0.5 xstep = 0.5
print("\nOutput array as table\n") print("\nOutput array as table\n")
array = [[random.randint(0, sys.maxsize) array = [[random.randint(0, sys.maxsize) for j in range(columns)] for i in range(rows)]
for j in range(columns)] for i in range(rows)]
for style in tables.style_types: for style in tables.style_types:
tables.array(array, None, None, style=style) tables.array(array, None, None, style=style)
@ -43,50 +42,39 @@ for style in tables.style_types:
tables.array(array, None, None, style=style) tables.array(array, None, None, style=style)
print("\nOutput char array as table\n") print("\nOutput char array as table\n")
array = [["Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"], array = [
["Header column 2", "Data 1", "Data 2", "Data 3", "Data 4"], ["Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"],
["Header column 3", "Data 5", "Data 6", "Data 7", "Data 8"], ["Header column 2", "Data 1", "Data 2", "Data 3", "Data 4"],
["Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"], ["Header column 3", "Data 5", "Data 6", "Data 7", "Data 8"],
["Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"]] ["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: for style in tables.style_types:
tables.array(array, None, None, headerrow=True, tables.array(array, None, None, headerrow=True, headercolumn=True, style=style)
headercolumn=True, style=style)
print("\nOutput array as table with separate header row and column\n") print("\nOutput array as table with separate header row and column\n")
array = [[f"Data {i + j:n}" for j in range(4)] array = [[f"Data {i + j:n}" for j in range(4)] for i in range(1, 4 * 4 + 1, 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"]
headerrow = ["Header row/column 1", "Header row 2", headercolumn = ["Header column 2", "Header column 3", "Header column 4", "Header column 5"]
"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: for style in tables.style_types:
tables.array(array, headerrow, headercolumn, headerrow=True, tables.array(array, headerrow, headercolumn, headerrow=True, headercolumn=True, cellborder=True, style=style)
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, style=style)
tables.array(array, headerrow[:-1], None, headerrow=True, style=style) tables.array(array, headerrow[:-1], None, headerrow=True, style=style)
tables.array(array, None, [headerrow[0]] + tables.array(array, None, [headerrow[0]] + headercolumn[:-1], headercolumn=True, style=style)
headercolumn[:-1], headercolumn=True, style=style)
tables.array(array, None, None, cellborder=True, style=style) tables.array(array, None, None, cellborder=True, style=style)
tables.array(array, None, None, tableborder=False, style=style) tables.array(array, None, None, tableborder=False, style=style)
tables.array(array, headerrow, headercolumn, tableborder=False, tables.array(array, headerrow, headercolumn, tableborder=False, headerrow=True, headercolumn=True, style=style)
headerrow=True, headercolumn=True, style=style) tables.array(array, headerrow[:-1], None, tableborder=False, headerrow=True, style=style)
tables.array(array, headerrow[:-1], None, tables.array(array, None, [headerrow[0]] + headercolumn[:-1], tableborder=False, headercolumn=True, style=style)
tableborder=False, headerrow=True, style=style) tables.array(array, None, None, tableborder=False, cellborder=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)] array = [[bool(random.getrandbits(1)) for j in range(columns)] for i in range(rows)]
for i in range(rows)]
for style in tables.style_types: for style in tables.style_types:
tables.array(array, None, None, style=style) tables.array(array, None, None, style=style)
print("\nOutput sorted array as table\n") print("\nOutput sorted array as table\n")
array = ([random.randint(0, sys.maxsize) array = ([random.randint(0, sys.maxsize) for j in range(columns)] for i in range(rows))
for j in range(columns)] for i in range(rows))
sortdimension = 0 sortdimension = 0
array = sorted(array, key=operator.itemgetter(sortdimension)) array = sorted(array, key=operator.itemgetter(sortdimension))
for style in tables.style_types: for style in tables.style_types:
@ -96,16 +84,13 @@ print("\nOutput single function as table\n")
for style in tables.style_types: for style in tables.style_types:
tables.function(xmin, xmax, xstep, afunction, headerrow=True, style=style) tables.function(xmin, xmax, xstep, afunction, headerrow=True, style=style)
for style in tables.style_types: for style in tables.style_types:
tables.function(xmin, xmax, xstep, lambda x: x + tables.function(xmin, xmax, xstep, lambda x: x + 1, headerrow=True, style=style)
1, headerrow=True, style=style)
print("\nOutput multiple functions as table\n") print("\nOutput multiple functions as table\n")
for style in tables.style_types: for style in tables.style_types:
tables.functions(xmin, xmax, xstep, [ tables.functions(xmin, xmax, xstep, [function1, function2], headerrow=True, style=style)
function1, function2], headerrow=True, style=style)
for style in tables.style_types: for style in tables.style_types:
tables.functions(xmin, xmax, xstep, [ tables.functions(xmin, xmax, xstep, [lambda x: 2 * x, lambda x: x**2], headerrow=True, style=style)
lambda x: 2 * x, lambda x: x ** 2], headerrow=True, style=style)
height = 40 height = 40
width = 80 width = 80
@ -125,25 +110,21 @@ array = [range(i, i + 2) for i in range(10)]
for atype in graphs.atype_types: for atype in graphs.atype_types:
for mark in graphs.mark_types: for mark in graphs.mark_types:
for style in graphs.style_types: for style in graphs.style_types:
graphs.plot(height, width, xmin, xmax, ymin, ymax, graphs.plot(height, width, xmin, xmax, ymin, ymax, array, atype=atype, mark=mark, style=style)
array, atype=atype, mark=mark, style=style)
print("\nOutput single function as graph\n") print("\nOutput single function as graph\n")
for style in graphs.style_types: for style in graphs.style_types:
graphs.function(height, width, xmin, xmax, ymin, graphs.function(height, width, xmin, xmax, ymin, ymax, afunction, style=style)
ymax, afunction, style=style)
for style in graphs.style_types: for style in graphs.style_types:
graphs.function(height, width, xmin, xmax, ymin, graphs.function(height, width, xmin, xmax, ymin, ymax, lambda x: x + 1, style=style)
ymax, lambda x: x + 1, style=style)
print("\nOutput multiple functions as graph\n") print("\nOutput multiple functions as graph\n")
for style in graphs.style_types: for style in graphs.style_types:
graphs.functions(height, width, xmin, xmax, ymin, ymax, graphs.functions(height, width, xmin, xmax, ymin, ymax, [function1, function2], style=style)
[function1, function2], style=style)
for style in graphs.style_types: for style in graphs.style_types:
graphs.functions(height, width, xmin, xmax, ymin, ymax, [ graphs.functions(height, width, xmin, xmax, ymin, ymax, [lambda x: 2 * x, lambda x: x**2], style=style)
lambda x: 2 * x, lambda x: x ** 2], style=style)
for style in graphs.style_types: for style in graphs.style_types:
graphs.functions(height, width, -(2 * math.pi), 2 * math.pi, -4, 4, graphs.functions(
[math.sin, math.cos, math.tan], axisunitslabel=False, style=style) height, width, -(2 * math.pi), 2 * math.pi, -4, 4, [math.sin, math.cos, math.tan], axisunitslabel=False, style=style
)