mirror of
https://github.com/tdulcet/Table-and-Graph-Libs.git
synced 2025-09-19 13:58:07 +08:00
Add option to change output stream/file.
This commit is contained in:
@@ -198,14 +198,18 @@ Default value: `False`
|
||||
Option: `padding`\
|
||||
Default value: `1`
|
||||
|
||||
#### Alignment
|
||||
#### Format and Alignment
|
||||
|
||||
Option: `alignment`\
|
||||
Values:
|
||||
|
||||
* `None` (default)
|
||||
* `False` (left)
|
||||
* `True` (right)
|
||||
* `""` (default)
|
||||
* `"<"` (left)
|
||||
* `">"` (right)
|
||||
* `"="` (internal, numeric types only)
|
||||
* `"^"` (center)
|
||||
|
||||
See the [Python documentation](https://docs.python.org/3/library/string.html#formatspec) for the full available options.
|
||||
|
||||
#### Title
|
||||
|
||||
@@ -236,6 +240,16 @@ Values:
|
||||
7. `style_types.heavy_dashed`: Heavy Dashed
|
||||

|
||||
|
||||
#### Output file
|
||||
|
||||
Option: `file`\
|
||||
Values:
|
||||
|
||||
* `sys.stdout` (default)
|
||||
* `sys.stderr`
|
||||
|
||||
Any other text [file object](https://docs.python.org/3/glossary.html#term-file-object).
|
||||
|
||||
#### Check size
|
||||
|
||||
Option: `check`\
|
||||
@@ -542,6 +556,16 @@ This option is only used when plotting a single array and when graphing a single
|
||||
|
||||

|
||||
|
||||
#### Output file
|
||||
|
||||
Option: `file`\
|
||||
Values:
|
||||
|
||||
* `sys.stdout` (default)
|
||||
* `sys.stderr`
|
||||
|
||||
Any other text [file object](https://docs.python.org/3/glossary.html#term-file-object).
|
||||
|
||||
#### Check size
|
||||
|
||||
Option: `check`\
|
||||
|
@@ -10,7 +10,7 @@ import textwrap
|
||||
from datetime import datetime, timezone
|
||||
from enum import Enum, IntEnum, auto
|
||||
from fractions import Fraction
|
||||
from typing import Callable, List, Optional, Sequence, Tuple
|
||||
from typing import Callable, List, Optional, Sequence, TextIO, Tuple
|
||||
|
||||
if sys.platform != "win32":
|
||||
import ctypes
|
||||
@@ -298,7 +298,7 @@ def outputlabel(label: float, units: units_types) -> Tuple[int, str]:
|
||||
return length, strm
|
||||
|
||||
|
||||
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, 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
|
||||
@@ -344,7 +344,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
ydivisor = 2 * ai * ((((4 * height) // ai) // 160) + 2)
|
||||
|
||||
if title:
|
||||
print(textwrap.fill(title, width=awidth))
|
||||
print(textwrap.fill(title, width=awidth), file=file)
|
||||
|
||||
astyle = styles[style]
|
||||
|
||||
@@ -521,12 +521,12 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if border:
|
||||
strm += astyle[8] + (astyle[0] * awidth) + astyle[10]
|
||||
|
||||
print(strm)
|
||||
print(strm, file=file)
|
||||
|
||||
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, 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
|
||||
@@ -594,10 +594,10 @@ 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)
|
||||
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, 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
|
||||
@@ -673,15 +673,15 @@ 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)
|
||||
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) -> 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)
|
||||
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, 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
|
||||
@@ -747,9 +747,9 @@ 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)
|
||||
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) -> 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)
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, (afunction,), border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, color, title, file, check)
|
||||
|
@@ -8,7 +8,7 @@ import shutil
|
||||
import sys
|
||||
import textwrap
|
||||
from enum import IntEnum, auto
|
||||
from typing import Any, Callable, List, Optional, Sequence
|
||||
from typing import Any, Callable, List, Optional, Sequence, TextIO
|
||||
|
||||
if sys.platform != "win32":
|
||||
import ctypes
|
||||
@@ -66,7 +66,7 @@ def strcol(astr: str) -> int:
|
||||
|
||||
|
||||
def table(array: List[List[str]], 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, check: bool = True) -> int:
|
||||
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
|
||||
@@ -91,7 +91,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
return 1
|
||||
|
||||
if title:
|
||||
print(textwrap.fill(title, width=width))
|
||||
print(textwrap.fill(title, width=width), file=file)
|
||||
|
||||
astyle = styles[style]
|
||||
|
||||
@@ -124,9 +124,9 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
awidth = columnwidth[j] - (strcol(array[i][j]) - len(array[i][j]))
|
||||
|
||||
if (not i and headerrow) or (not j and headercolumn):
|
||||
strm += (" " * padding) + "\033[1m" + array[i][j].center(awidth) + "\033[22m" + (" " * padding)
|
||||
strm += (" " * padding) + "\033[1m" + f"{array[i][j]:^{awidth}}" + "\033[22m" + (" " * padding)
|
||||
else:
|
||||
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)
|
||||
strm += (" " * padding) + f"{array[i][j]:{alignment}{awidth}}" + (" " * padding)
|
||||
|
||||
if tableborder:
|
||||
strm += astyle[1]
|
||||
@@ -177,12 +177,12 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
|
||||
strm += astyle[10]
|
||||
|
||||
print(strm)
|
||||
print(strm, file=file)
|
||||
|
||||
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: 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: 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
|
||||
@@ -228,10 +228,10 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[str]] =
|
||||
j = 1 if aheadercolumn else 0
|
||||
aaarray[i][j:] = map(str, aarray[ii][:columns - j])
|
||||
|
||||
return table(aaarray, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|
||||
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: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> 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
|
||||
@@ -261,9 +261,9 @@ 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)
|
||||
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: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> 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)
|
||||
return functions(xmin, xmax, xstep, (afunction,), headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style, file, check)
|
||||
|
Reference in New Issue
Block a user