mirror of
https://github.com/tdulcet/Table-and-Graph-Libs.git
synced 2025-09-19 13:58:07 +08:00
Updated the style and color options to use Enums.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
### Usage
|
||||
|
||||
Requires Python 3.5 or greater and the [wcwidth library](https://pypi.org/project/wcwidth/), which users can install with: `pip3 install wcwidth`. See the [tables.py](tables.py) file for full usage information.
|
||||
Requires Python 3.6 or greater and the [wcwidth library](https://pypi.org/project/wcwidth/), which users can install with: `pip3 install wcwidth`. See the [tables.py](tables.py) file for full usage information.
|
||||
|
||||
Complete versions of all of the examples below and more can be found in the [test.py](test.py) file.
|
||||
|
||||
@@ -75,9 +75,9 @@ def afunction(x):
|
||||
|
||||
xmin = -10
|
||||
xmax = 10
|
||||
xscl = 2
|
||||
xstep = 0.5
|
||||
|
||||
tables.function(xmin, xmax, xscl, afunction, headerrow=True)
|
||||
tables.function(xmin, xmax, xstep, afunction, headerrow=True)
|
||||
```
|
||||
|
||||

|
||||
@@ -89,11 +89,11 @@ import tables
|
||||
|
||||
xmin = -10
|
||||
xmax = 10
|
||||
xscl = 2
|
||||
xstep = 0.5
|
||||
|
||||
afunction = lambda x: x + 1
|
||||
|
||||
tables.function(xmin, xmax, xscl, afunction, headerrow=True)
|
||||
tables.function(xmin, xmax, xstep, afunction, headerrow=True)
|
||||
```
|
||||
|
||||
Output same as example above.
|
||||
@@ -111,12 +111,12 @@ def function2(x):
|
||||
|
||||
xmin = -10
|
||||
xmax = 10
|
||||
xscl = 2
|
||||
xstep = 0.5
|
||||
|
||||
# Function parameter and return value can be any data type, as long as they are the same
|
||||
functions = [function1, function2]
|
||||
|
||||
tables.functions(xmin, xmax, xscl, functions, headerrow=True)
|
||||
tables.functions(xmin, xmax, xstep, functions, headerrow=True)
|
||||
```
|
||||
|
||||

|
||||
@@ -128,12 +128,12 @@ import tables
|
||||
|
||||
xmin = -10
|
||||
xmax = 10
|
||||
xscl = 2
|
||||
xstep = 0.5
|
||||
|
||||
# Function parameter and return value can be any data type, as long as they are the same
|
||||
functions = [lambda x: 2 * x, lambda x: x ** 2]
|
||||
|
||||
tables.functions(xmin, xmax, xscl, functions, headerrow=True)
|
||||
tables.functions(xmin, xmax, xstep, functions, headerrow=True)
|
||||
```
|
||||
|
||||
Output same as example above.
|
||||
@@ -174,7 +174,8 @@ Default value: `1`
|
||||
Option: `alignment`\
|
||||
Values:
|
||||
|
||||
* `False` (left, default)
|
||||
* `None` (default)
|
||||
* `False` (left)
|
||||
* `True` (right)
|
||||
|
||||
#### Title
|
||||
@@ -189,33 +190,40 @@ The title is word wrapped based on the current width of the terminal. Handles ne
|
||||
Option: `style`\
|
||||
Values:
|
||||
|
||||
0. ASCII
|
||||
0. `style_types.ASCII`: ASCII
|
||||
|
||||

|
||||
1. Basic
|
||||
1. `style_types.basic`: Basic
|
||||
|
||||

|
||||
2. Light (default)
|
||||
2. `style_types.light`: Light (default)
|
||||
|
||||

|
||||
3. Heavy
|
||||
3. `style_types.heavy`: Heavy
|
||||
|
||||

|
||||
4. Double
|
||||
4. `style_types.double`: Double
|
||||
|
||||

|
||||
5. Light Dashed
|
||||
5. `style_types.light_dashed`: Light Dashed
|
||||
|
||||

|
||||
6. Heavy Dashed
|
||||
6. `style_types.heavy_dashed`: Heavy Dashed
|
||||
|
||||

|
||||
|
||||
#### Check size
|
||||
|
||||
Option: `check`\
|
||||
Default value: `True`
|
||||
|
||||
Check that the width of the table is not greater then the width of the terminal.
|
||||
|
||||
## Graphs/Plots
|
||||
|
||||
### Usage
|
||||
|
||||
Requires Python 3.5 or greater and the [wcwidth library](https://pypi.org/project/wcwidth/), which users can install with: `pip3 install wcwidth`. See the [graphs.py](graphs.py) file for full usage information.
|
||||
Requires Python 3.6 or greater and the [wcwidth library](https://pypi.org/project/wcwidth/), which users can install with: `pip3 install wcwidth`. See the [graphs.py](graphs.py) file for full usage information.
|
||||
|
||||
Complete versions of all of the examples below and more can be found in the [test.py](test.py) file.
|
||||
|
||||
@@ -369,25 +377,25 @@ The title is word wrapped based on the current width of the terminal. Handles ne
|
||||
Option: `style`\
|
||||
Values:
|
||||
|
||||
0. ASCII
|
||||
0. `style_types.ASCII`: ASCII
|
||||
|
||||

|
||||
1. Basic
|
||||
1. `style_types.basic`: Basic
|
||||
|
||||

|
||||
2. Light (default)
|
||||
2. `style_types.light`: Light (default)
|
||||
|
||||

|
||||
3. Heavy
|
||||
3. `style_types.heavy`: Heavy
|
||||
|
||||

|
||||
4. Double
|
||||
4. `style_types.double`: Double
|
||||
|
||||

|
||||
5. Light Dashed
|
||||
5. `style_types.light_dashed`: Light Dashed
|
||||
|
||||

|
||||
6. Heavy Dashed
|
||||
6. `style_types.heavy_dashed`: Heavy Dashed
|
||||
|
||||

|
||||
|
||||
@@ -396,27 +404,25 @@ Values:
|
||||
Option: `color`\
|
||||
Values:
|
||||
|
||||
0. System default
|
||||
1. Black
|
||||
2. Red (default)
|
||||
3. Green
|
||||
4. Yellow
|
||||
5. Blue
|
||||
6. Cyan
|
||||
7. Light gray
|
||||
8. Dark gray
|
||||
9. Light red
|
||||
10. Light green
|
||||
11. Light yellow
|
||||
12. Light blue
|
||||
13. Light cyan
|
||||
14. White
|
||||
0. `color_types.default`: System default
|
||||
1. `color_types.black`: Black
|
||||
2. `color_types.red`: Red (default)
|
||||
3. `color_types.green`: Green
|
||||
4. `color_types.yellow`: Yellow
|
||||
5. `color_types.blue`: Blue
|
||||
6. `color_types.cyan`: Cyan
|
||||
7. `color_types.dark_gray`: Light gray
|
||||
8. `color_types.dark_gray`: Dark gray
|
||||
9. `color_types.light_red`: Light red
|
||||
10. `color_types.light_green`: Light green
|
||||
11. `color_types.light_yellow`: Light yellow
|
||||
12. `color_types.light_blue`: Light blue
|
||||
13. `color_types.light_cyan`: Light cyan
|
||||
14. `color_types.white`: White
|
||||
|
||||
See [here](https://misc.flogisoft.com/bash/tip_colors_and_formatting#foreground_text) for examples of the colors.
|
||||
|
||||
Only used for plots and when graphing a single function.
|
||||
|
||||
When graphing multiple functions, colors `2` - `14` are used inorder. Color `0` is used where the functions cross.
|
||||
Only used when plotting a single array and when graphing a single function. When plotting multiple arrays or graphing multiple functions, colors 2 - 14 are used inorder. The system default color is used where the plots cross.
|
||||
|
||||
##### Plot
|
||||
|
||||
@@ -425,3 +431,10 @@ When graphing multiple functions, colors `2` - `14` are used inorder. Color `0`
|
||||
##### Graph
|
||||
|
||||

|
||||
|
||||
#### Check size
|
||||
|
||||
Option: `check`\
|
||||
Default value: `True`
|
||||
|
||||
Check that the width and height of the graph are not greater then the respective width and height of the terminal.
|
||||
|
382
python/graphs.py
382
python/graphs.py
@@ -7,13 +7,26 @@ from __future__ import division, print_function, unicode_literals
|
||||
import sys
|
||||
import math
|
||||
import shutil
|
||||
from fractions import Fraction
|
||||
import textwrap
|
||||
from enum import Enum, IntEnum, auto
|
||||
from wcwidth import wcswidth
|
||||
from typing import List, Tuple, Optional, Sequence, Callable
|
||||
import locale
|
||||
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
|
||||
|
||||
class style_types(IntEnum):
|
||||
ASCII = 0
|
||||
basic = auto()
|
||||
light = auto()
|
||||
heavy = auto()
|
||||
double = auto()
|
||||
light_dashed = auto()
|
||||
heavy_dashed = auto()
|
||||
|
||||
|
||||
styles = [
|
||||
["-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # ASCII
|
||||
["—", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # Basic
|
||||
@@ -25,33 +38,71 @@ styles = [
|
||||
]
|
||||
# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border
|
||||
|
||||
colors = ["\033[39m", "\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m",
|
||||
"\033[37m", "\033[90m", "\033[91m", "\033[92m", "\033[93m", "\033[94m", "\033[95m", "\033[96m", "\033[97m"]
|
||||
|
||||
dots = ["⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾",
|
||||
"⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⣐", "⣑", "⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯", "⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"]
|
||||
class color_types(IntEnum):
|
||||
default = 0
|
||||
black = auto()
|
||||
red = auto()
|
||||
green = auto()
|
||||
yellow = auto()
|
||||
blue = auto()
|
||||
cyan = auto()
|
||||
light_gray = auto()
|
||||
dark_gray = auto()
|
||||
light_red = auto()
|
||||
light_green = auto()
|
||||
light_yellow = auto()
|
||||
light_blue = auto()
|
||||
light_cyan = auto()
|
||||
white = auto()
|
||||
|
||||
|
||||
colors = ["\033[39m", "\033[30m", "\033[31m", "\033[32m", "\033[33m",
|
||||
"\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[90m",
|
||||
"\033[91m", "\033[92m", "\033[93m", "\033[94m", "\033[95m",
|
||||
"\033[96m", "\033[97m"]
|
||||
|
||||
dots = [
|
||||
"⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎",
|
||||
"⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝",
|
||||
"⠞", "⠟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⠨", "⠩", "⠪", "⠫", "⠬",
|
||||
"⠭", "⠮", "⠯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⠸", "⠹", "⠺", "⠻",
|
||||
"⠼", "⠽", "⠾", "⠿", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⡈", "⡉", "⡊",
|
||||
"⡋", "⡌", "⡍", "⡎", "⡏", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⡘", "⡙",
|
||||
"⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⡨",
|
||||
"⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷",
|
||||
"⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆",
|
||||
"⢇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕",
|
||||
"⢖", "⢗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⢠", "⢡", "⢢", "⢣", "⢤",
|
||||
"⢥", "⢦", "⢧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⢰", "⢱", "⢲", "⢳",
|
||||
"⢴", "⢵", "⢶", "⢷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣀", "⣁", "⣂",
|
||||
"⣃", "⣄", "⣅", "⣆", "⣇", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⣐", "⣑",
|
||||
"⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⣠",
|
||||
"⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯",
|
||||
"⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾",
|
||||
"⣿"]
|
||||
|
||||
values = [[0x1, 0x2, 0x4, 0x40], [0x8, 0x10, 0x20, 0x80]]
|
||||
|
||||
fractions = {
|
||||
"¼": 1.0 / 4.0,
|
||||
"½": 1.0 / 2.0,
|
||||
"¾": 3.0 / 4.0,
|
||||
"⅐": 1.0 / 7.0,
|
||||
"⅑": 1.0 / 9.0,
|
||||
"⅒": 1.0 / 10.0,
|
||||
"⅓": 1.0 / 3.0,
|
||||
"⅔": 2.0 / 3.0,
|
||||
"⅕": 1.0 / 5.0,
|
||||
"⅖": 2.0 / 5.0,
|
||||
"⅗": 3.0 / 5.0,
|
||||
"⅘": 4.0 / 5.0,
|
||||
"⅙": 1.0 / 6.0,
|
||||
"⅚": 5.0 / 6.0,
|
||||
"⅛": 1.0 / 8.0,
|
||||
"⅜": 3.0 / 8.0,
|
||||
"⅝": 5.0 / 8.0,
|
||||
"⅞": 7.0 / 8.0
|
||||
"¼": Fraction(1, 4),
|
||||
"½": Fraction(1, 2),
|
||||
"¾": Fraction(3, 4),
|
||||
"⅐": Fraction(1, 7),
|
||||
"⅑": Fraction(1, 9),
|
||||
"⅒": Fraction(1, 10),
|
||||
"⅓": Fraction(1, 3),
|
||||
"⅔": Fraction(2, 3),
|
||||
"⅕": Fraction(1, 5),
|
||||
"⅖": Fraction(2, 5),
|
||||
"⅗": Fraction(3, 5),
|
||||
"⅘": Fraction(4, 5),
|
||||
"⅙": Fraction(1, 6),
|
||||
"⅚": Fraction(5, 6),
|
||||
"⅛": Fraction(1, 8),
|
||||
"⅜": Fraction(3, 8),
|
||||
"⅝": Fraction(5, 8),
|
||||
"⅞": Fraction(7, 8)
|
||||
}
|
||||
|
||||
constants = {
|
||||
@@ -59,68 +110,75 @@ constants = {
|
||||
"e": math.e
|
||||
}
|
||||
|
||||
MAX = sys.float_info.radix ** sys.float_info.mant_dig - 1
|
||||
|
||||
def strcol(str: str) -> int:
|
||||
|
||||
def strcol(astr: str) -> int:
|
||||
"""Returns the number of columns that the given string would take up if printed."""
|
||||
width = wcswidth(str)
|
||||
width = wcswidth(astr)
|
||||
if width == -1:
|
||||
print("\nError! wcswidth failed. Nonprintable wide character.", file=sys.stderr)
|
||||
print(
|
||||
"\nError! wcswidth failed. Nonprintable wide character.",
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
return width
|
||||
# return len(str)
|
||||
# return len(astr)
|
||||
|
||||
|
||||
def outputlabel(label: float) -> Tuple[int, str]:
|
||||
"""Outputs a label in a nice, human readable format."""
|
||||
def outputfraction(number: float) -> Tuple[int, str]:
|
||||
"""Convert fractions and constants to Unicode characters"""
|
||||
output = False
|
||||
|
||||
fractionpart, intpart = math.modf(label)
|
||||
fractionpart = abs(fractionpart)
|
||||
|
||||
strm = ""
|
||||
|
||||
for fraction in fractions:
|
||||
if abs(fractionpart - fractions[fraction]) < sys.float_info.epsilon:
|
||||
if intpart != 0:
|
||||
strm += str(intpart)
|
||||
n = abs(number)
|
||||
|
||||
strm += fraction
|
||||
if n <= MAX:
|
||||
# fractionpart, intpart = math.modf(number)
|
||||
# fractionpart = abs(fractionpart)
|
||||
intpart, fractionpart = divmod(Fraction(number).limit_denominator(), 1)
|
||||
|
||||
output = True
|
||||
break
|
||||
|
||||
if abs(label) >= sys.float_info.epsilon and not output:
|
||||
for constant in constants:
|
||||
if not output and label % constants[constant] == 0:
|
||||
intpart = label / constants[constant]
|
||||
|
||||
if intpart == -1:
|
||||
for fraction, value in fractions.items():
|
||||
if abs(fractionpart - value) <= sys.float_info.epsilon * n:
|
||||
if intpart == 0 and number < 0:
|
||||
strm += "-"
|
||||
elif intpart != 1:
|
||||
strm += str(intpart)
|
||||
elif intpart != 0:
|
||||
strm += "{0:n}".format(intpart)
|
||||
|
||||
strm += constant
|
||||
strm += fraction
|
||||
|
||||
output = True
|
||||
break
|
||||
|
||||
if n > sys.float_info.epsilon and not output:
|
||||
for constant, value in constants.items():
|
||||
if not output and number % value <= sys.float_info.epsilon * n:
|
||||
intpart = number / value
|
||||
|
||||
if intpart == -1:
|
||||
strm += "-"
|
||||
elif intpart != 1:
|
||||
strm += "{0:.{prec}n}".format(intpart,
|
||||
prec=sys.float_info.dig)
|
||||
|
||||
strm += constant
|
||||
|
||||
output = True
|
||||
break
|
||||
|
||||
if not output:
|
||||
strm += "{0:n}".format(label)
|
||||
strm += "{0:n}".format(number)
|
||||
|
||||
length = strcol(strm)
|
||||
|
||||
return length, strm
|
||||
|
||||
|
||||
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, title: Optional[str] = None) -> int:
|
||||
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Output graph"""
|
||||
if not array:
|
||||
return 1
|
||||
|
||||
if not (0 <= style < len(styles)):
|
||||
return 1
|
||||
|
||||
if height == 0:
|
||||
return 1
|
||||
|
||||
@@ -129,18 +187,19 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
|
||||
w = shutil.get_terminal_size()
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
@@ -150,46 +209,51 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
print("ymin must be less than ymax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
xscl = width / (xmax - xmin)
|
||||
yscl = height / (ymax - ymin)
|
||||
xaxis = width - (xmax * xscl)
|
||||
yaxis = ymax * yscl
|
||||
divisor = 2 * 4 * ((width // 160) + 1 if (width / 160.0) > 1 else 1)
|
||||
xstep = (xmax - xmin) / width
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = 0 if xmin > 0 else width if xmax < 0 else width - (xmax / xstep)
|
||||
yaxis = height if ymin > 0 else 0 if ymax < 0 else ymax / ystep
|
||||
xdivisor = 2 * 4 * ((width // 160) + 2)
|
||||
ydivisor = 2 * 4 * ((height // 160) + 2)
|
||||
|
||||
if title:
|
||||
print(textwrap.fill(title, width=w.columns))
|
||||
print(textwrap.fill(title, width=width // 2))
|
||||
|
||||
strm = ""
|
||||
|
||||
i = 0
|
||||
while i < height:
|
||||
ayaxis = i <= yaxis and (i + 4) > yaxis
|
||||
yaxislabel = i <= (yaxis + 4) and (i + 4) > (yaxis + 4)
|
||||
ayaxis = i <= yaxis and (
|
||||
i + 4) > yaxis if yaxis <= (height - 4) else i < yaxis and (i + 4) >= yaxis
|
||||
yaxislabel = i <= (yaxis + 4) and (i + 4) > (yaxis + 4) if yaxis <= (
|
||||
height - 4) else i < (yaxis - 4) and (i + 4) >= (yaxis - 4)
|
||||
|
||||
ylabelstrm = ""
|
||||
ylabellength = 0
|
||||
|
||||
if border and axislabel and axisunitslabel:
|
||||
if border and axislabel and axisunitslabel and yaxis >= 0 and yaxis <= height:
|
||||
output = False
|
||||
label = 0.0
|
||||
adivisor = -divisor if i < yaxis else divisor
|
||||
adivisor = -ydivisor if i < yaxis else ydivisor
|
||||
|
||||
k = yaxis + adivisor
|
||||
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and i >= 4 and not output:
|
||||
while (k >= i if i < yaxis else k < (i + 4)) and i >= 4 and not output:
|
||||
if (i <= k and (i + 4) > k):
|
||||
label = ymax - (k / yscl)
|
||||
label = ymax - ((height if k > height else k) * ystep)
|
||||
|
||||
output = True
|
||||
k += adivisor
|
||||
|
||||
if (output):
|
||||
ylabellength, ylabelstrm = outputlabel(label)
|
||||
ylabellength, ylabelstrm = outputfraction(label)
|
||||
ylabellength *= 2
|
||||
|
||||
j = 0
|
||||
while j < width:
|
||||
axaxis = j <= xaxis and (j + 2) > xaxis
|
||||
xaxislabel = j <= (xaxis - 2) and (j + 2) > (xaxis - 2)
|
||||
axaxis = j < xaxis and (
|
||||
j + 2) >= xaxis if xaxis >= 2 else j <= xaxis and (j + 2) > xaxis
|
||||
xaxislabel = j < (xaxis - 2) and (j + 2) >= (
|
||||
xaxis - 2) if xaxis >= 2 else j <= (xaxis + 2) and (j + 2) > (xaxis + 2)
|
||||
|
||||
output = False
|
||||
|
||||
@@ -198,57 +262,61 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
strm += styles[style][6]
|
||||
output = True
|
||||
elif axaxis:
|
||||
if axislabel and axisunitslabel:
|
||||
adivisor = -divisor if i < yaxis else divisor
|
||||
if i == 0:
|
||||
strm += styles[style][4]
|
||||
output = True
|
||||
elif i >= (height - 4):
|
||||
strm += styles[style][10]
|
||||
output = True
|
||||
elif axislabel and axisunitslabel:
|
||||
adivisor = -ydivisor if i < yaxis else ydivisor
|
||||
|
||||
k = yaxis + adivisor
|
||||
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and i >= 4 and not output:
|
||||
while (k >= i if i < yaxis else k < (i + 4)) and i >= 4 and not output:
|
||||
if i <= k and (i + 4) > k:
|
||||
strm += styles[style][7]
|
||||
strm += styles[style][7 if xaxis >= 2 else 5]
|
||||
output = True
|
||||
k += adivisor
|
||||
if not output:
|
||||
if i == 0:
|
||||
strm += styles[style][4]
|
||||
elif i >= (height - 4):
|
||||
strm += styles[style][10]
|
||||
else:
|
||||
strm += styles[style][1]
|
||||
strm += styles[style][1]
|
||||
output = True
|
||||
elif ayaxis:
|
||||
if axislabel and axisunitslabel:
|
||||
adivisor = -divisor if j < xaxis else divisor
|
||||
if j == 0:
|
||||
strm += styles[style][2]
|
||||
output = True
|
||||
elif j >= (width - 2):
|
||||
strm += styles[style][4]
|
||||
output = True
|
||||
elif axislabel and axisunitslabel:
|
||||
adivisor = -xdivisor if j < xaxis else xdivisor
|
||||
|
||||
k = xaxis + adivisor
|
||||
while ((j < xaxis and k >= j) or (j > xaxis and k < (j + 2))) and j < (width - 4) and not output:
|
||||
while (k >= j if j < xaxis else k < (j + 2)) and j < (width - 4) and not output:
|
||||
if j <= k and (j + 2) > k:
|
||||
strm += styles[style][3]
|
||||
strm += styles[style][3 if yaxis <=
|
||||
(height - 4) else 9]
|
||||
output = True
|
||||
k += adivisor
|
||||
if not output:
|
||||
if j == 0:
|
||||
strm += styles[style][2]
|
||||
elif j >= (width - 2):
|
||||
strm += styles[style][4]
|
||||
else:
|
||||
strm += styles[style][0]
|
||||
strm += styles[style][0]
|
||||
output = True
|
||||
elif yaxislabel and xaxislabel and axislabel and axisunitslabel:
|
||||
elif yaxislabel and xaxislabel and axislabel and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0:
|
||||
strm += "0"
|
||||
output = True
|
||||
elif j >= (width - 2) and yaxislabel and axislabel:
|
||||
elif (j >= (width - 2) if xaxis <= (width - 2) else j == 0) and yaxislabel and axislabel:
|
||||
strm += "x"
|
||||
output = True
|
||||
elif yaxislabel and axislabel and axisunitslabel:
|
||||
label = 0.0
|
||||
adivisor = -divisor if j < xaxis else divisor
|
||||
adivisor = -xdivisor if j < xaxis else xdivisor
|
||||
if j < xaxis:
|
||||
j += 2
|
||||
|
||||
k = xaxis + adivisor
|
||||
while ((j < xaxis and k >= j) or (j > xaxis and k < (j + 2))) and j < (width - 2) and not output:
|
||||
while (k >= j if j < xaxis else k < (j + 2)) and j < (width - 2) and not output:
|
||||
if j <= k and (j + 2) > k:
|
||||
label = (k / xscl) + xmin
|
||||
label = ((width if k > width else k)
|
||||
* xstep) + xmin
|
||||
|
||||
output = True
|
||||
k += adivisor
|
||||
@@ -259,7 +327,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if output:
|
||||
output = False
|
||||
|
||||
length, astrm = outputlabel(label)
|
||||
length, astrm = outputfraction(label)
|
||||
length *= 2
|
||||
if (j >= xaxis or (j + length) < (xaxis - 4)) and (j + length) < (width - 2):
|
||||
strm += astrm
|
||||
@@ -271,10 +339,10 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
output = True
|
||||
else:
|
||||
j += 2
|
||||
elif i == 0 and xaxislabel and axislabel:
|
||||
elif (i == 0 if yaxis >= 4 else i >= (height - 4)) and xaxislabel and axislabel:
|
||||
strm += "y"
|
||||
output = True
|
||||
elif (j <= (xaxis - ylabellength) and (j + 2) > (xaxis - ylabellength)) and axislabel and axisunitslabel:
|
||||
elif ylabellength and (xaxislabel if xaxis < 2 else j < (xaxis - ylabellength) and (j + 2) >= (xaxis - ylabellength)) and (yaxis >= 4 or i < (height - 4)) and axislabel and axisunitslabel:
|
||||
strm += ylabelstrm
|
||||
output = True
|
||||
if ylabellength > 2:
|
||||
@@ -299,14 +367,15 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if color:
|
||||
strm += colors[color]
|
||||
|
||||
strm += "\033[1m" + dots[dot] + "\033[22m"
|
||||
strm += dots[dot]
|
||||
|
||||
if color:
|
||||
strm += colors[0]
|
||||
|
||||
j += 2
|
||||
|
||||
strm += "\n"
|
||||
if i < (height - 4):
|
||||
strm += "\n"
|
||||
i += 4
|
||||
|
||||
print(strm)
|
||||
@@ -314,7 +383,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
return 0
|
||||
|
||||
|
||||
def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, 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 aarrays:
|
||||
return 1
|
||||
@@ -323,9 +392,6 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
print("Error: The arrays must have two columns.")
|
||||
return 1
|
||||
|
||||
if color < 0 or color >= len(colors):
|
||||
return 1
|
||||
|
||||
w = shutil.get_terminal_size()
|
||||
|
||||
if height == 0:
|
||||
@@ -334,18 +400,19 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if width == 0:
|
||||
width = w.columns * 2
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin == 0 and xmax == 0:
|
||||
xmin = min(i[0] for aarray in aarrays for i in aarray)
|
||||
@@ -363,10 +430,10 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
print("ymin must be less than ymax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
xscl = width / (xmax - xmin)
|
||||
yscl = height / (ymax - ymin)
|
||||
xaxis = width - (xmax * xscl)
|
||||
yaxis = ymax * yscl
|
||||
xstep = (xmax - xmin) / width
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = width - (xmax / xstep)
|
||||
yaxis = ymax / ystep
|
||||
|
||||
aaarray = [[0 for j in range(height)] for i in range(width)]
|
||||
|
||||
@@ -376,8 +443,8 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
|
||||
for i in aarray:
|
||||
if i[0] >= xmin and i[0] < xmax and i[1] >= ymin and i[1] < ymax:
|
||||
x = int((i[0] * xscl) + xaxis)
|
||||
y = int((yaxis - (i[1] * yscl)) - 1)
|
||||
x = int((i[0] / xstep) + xaxis)
|
||||
y = int((yaxis - (i[1] / ystep)) - 1)
|
||||
|
||||
if aaarray[x][y]:
|
||||
if aaarray[x][y] != acolor:
|
||||
@@ -385,19 +452,18 @@ def arrays(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=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border=border,
|
||||
axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
|
||||
|
||||
def array(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def array(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
"""Convert single array to graph and output"""
|
||||
return arrays(height, width, xmin, xmax, ymin, ymax, [aarray], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
return arrays(height, width, xmin, xmax, ymin, ymax, [
|
||||
aarray], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
|
||||
|
||||
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Convert one or more functions to graph and output"""
|
||||
if color < 0 or color >= len(colors):
|
||||
return 1
|
||||
|
||||
if not afunctions:
|
||||
return 1
|
||||
|
||||
@@ -409,18 +475,19 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
if width == 0:
|
||||
width = w.columns * 2
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
@@ -432,10 +499,11 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
|
||||
rows = width
|
||||
|
||||
xscl = width / (xmax - xmin)
|
||||
yscl = height / (ymax - ymin)
|
||||
xaxis = width - (xmax * xscl)
|
||||
yaxis = ymax * yscl
|
||||
xstep = (xmax - xmin) / width
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = width - (xmax / xstep)
|
||||
yaxis = ymax / ystep
|
||||
xres = 2
|
||||
|
||||
array = [[0 for j in range(height)] for i in range(width)]
|
||||
|
||||
@@ -443,13 +511,13 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
acolor = color + 1 if len(afunctions) == 1 else (j %
|
||||
(len(colors) - 2)) + 3
|
||||
|
||||
for i in (x / 2 for x in range(rows * 2)):
|
||||
x = (i / xscl) + xmin
|
||||
for i in (x / xres for x in range(rows * xres)):
|
||||
x = (i * xstep) + xmin
|
||||
y = function(x)
|
||||
|
||||
if x >= xmin and x < xmax and y >= ymin and y < ymax:
|
||||
ax = int((x * xscl) + xaxis)
|
||||
ay = int((yaxis - (y * yscl)) - 1)
|
||||
ax = int((x / xstep) + xaxis)
|
||||
ay = int((yaxis - (y / ystep)) - 1)
|
||||
|
||||
if array[ax][ay]:
|
||||
if array[ax][ay] != acolor:
|
||||
@@ -457,9 +525,11 @@ 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=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, array, border=border,
|
||||
axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
|
||||
|
||||
def function(height, width, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def function(height, width, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
"""Convert single function to function array and output"""
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, [afunction], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, [
|
||||
afunction], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
|
166
python/tables.py
166
python/tables.py
@@ -8,12 +8,24 @@ import sys
|
||||
import shutil
|
||||
import re
|
||||
import textwrap
|
||||
from enum import IntEnum, auto
|
||||
from wcwidth import wcswidth
|
||||
from typing import List, Optional, Any, Sequence, Callable
|
||||
import locale
|
||||
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
|
||||
|
||||
class style_types(IntEnum):
|
||||
ASCII = 0
|
||||
basic = auto()
|
||||
light = auto()
|
||||
heavy = auto()
|
||||
double = auto()
|
||||
light_dashed = auto()
|
||||
heavy_dashed = auto()
|
||||
|
||||
|
||||
styles = [
|
||||
["-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # ASCII
|
||||
["—", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # Basic
|
||||
@@ -28,25 +40,24 @@ styles = [
|
||||
ansi = re.compile(r'\x1B\[(?:[0-9]+(?:;[0-9]+)*)?m')
|
||||
|
||||
|
||||
def strcol(str: str) -> int:
|
||||
def strcol(astr: str) -> int:
|
||||
"""Returns the number of columns that the given string would take up if printed."""
|
||||
str = ansi.sub('', str)
|
||||
width = wcswidth(str)
|
||||
astr = ansi.sub('', astr)
|
||||
width = wcswidth(astr)
|
||||
if width == -1:
|
||||
print("\nError! wcswidth failed. Nonprintable wide character.", file=sys.stderr)
|
||||
print(
|
||||
"\nError! wcswidth failed. Nonprintable wide character.",
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
return width
|
||||
# return len(str)
|
||||
# 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: bool = False, title: Optional[str] = None, style: int = 2) -> 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:
|
||||
"""Output char array as table"""
|
||||
if not array:
|
||||
return 1
|
||||
|
||||
if not (0 <= style < len(styles)):
|
||||
return 1
|
||||
|
||||
rows = len(array)
|
||||
columns = len(array[0])
|
||||
|
||||
@@ -61,13 +72,14 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
else:
|
||||
width += (2 * padding) * columns
|
||||
|
||||
if width > w.columns:
|
||||
print("The width of the table ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
width, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if check:
|
||||
if width > w.columns:
|
||||
print("The width of the table ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
width, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if title:
|
||||
print(textwrap.fill(title, width=w.columns))
|
||||
print(textwrap.fill(title, width=width))
|
||||
|
||||
strm = ""
|
||||
|
||||
@@ -77,18 +89,22 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
for j in range(columns):
|
||||
strm += styles[style][0] * ((2 * padding) + columnwidth[j])
|
||||
|
||||
if j == (columns - 1):
|
||||
strm += styles[style][4] + "\n"
|
||||
elif cellborder or headerrow or (j == 0 and headercolumn):
|
||||
strm += styles[style][3]
|
||||
else:
|
||||
strm += styles[style][0]
|
||||
if j < (columns - 1):
|
||||
if cellborder or headerrow or (j == 0 and headercolumn):
|
||||
strm += styles[style][3]
|
||||
else:
|
||||
strm += styles[style][0]
|
||||
|
||||
strm += styles[style][4] + "\n"
|
||||
|
||||
for i in range(rows):
|
||||
if tableborder:
|
||||
strm += styles[style][1]
|
||||
|
||||
for j in range(columns):
|
||||
if (j == 0 and tableborder) or (j > 0 and cellborder) or (i == 0 and j > 0 and headerrow) or (j == 1 and headercolumn):
|
||||
if (j > 0 and cellborder) or (i == 0 and j > 0 and headerrow) or (j == 1 and headercolumn):
|
||||
strm += styles[style][1]
|
||||
elif tableborder or (i > 0 and j > 0 and headerrow) or (j > 1 and headercolumn):
|
||||
elif j > 0 and (tableborder or (i > 0 and headerrow) or headercolumn):
|
||||
strm += " "
|
||||
|
||||
awidth = columnwidth[j] - (strcol(array[i][j]) - len(array[i][j]))
|
||||
@@ -102,7 +118,9 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
else:
|
||||
strm += " " * padding
|
||||
|
||||
if alignment:
|
||||
if alignment is None:
|
||||
strm += "{0:{width}}".format(array[i][j], width=awidth)
|
||||
elif alignment:
|
||||
strm += array[i][j].rjust(awidth)
|
||||
else:
|
||||
strm += array[i][j].ljust(awidth)
|
||||
@@ -127,17 +145,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
elif i < (rows - 1) and headercolumn:
|
||||
strm += " " * ((2 * padding) + columnwidth[j])
|
||||
|
||||
if j == (columns - 1):
|
||||
if tableborder:
|
||||
if i == (rows - 1):
|
||||
strm += styles[style][10]
|
||||
elif cellborder or (i == 0 and headerrow):
|
||||
strm += styles[style][7]
|
||||
elif headercolumn:
|
||||
strm += styles[style][1]
|
||||
|
||||
strm += "\n"
|
||||
else:
|
||||
if j < (columns - 1):
|
||||
if i == (rows - 1) and tableborder:
|
||||
if cellborder or (j == 0 and headercolumn):
|
||||
strm += styles[style][9]
|
||||
@@ -153,12 +161,23 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
|
||||
else:
|
||||
strm += " "
|
||||
|
||||
if tableborder:
|
||||
if i == (rows - 1):
|
||||
strm += styles[style][10]
|
||||
elif cellborder or (i == 0 and headerrow):
|
||||
strm += styles[style][7]
|
||||
elif headercolumn:
|
||||
strm += styles[style][1]
|
||||
|
||||
if i < (rows - 1):
|
||||
strm += "\n"
|
||||
|
||||
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: bool = False, title: Optional[str] = None, style: int = 2) -> int:
|
||||
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:
|
||||
"""Convert array to char array and output as table"""
|
||||
if not aarray:
|
||||
return 1
|
||||
@@ -167,7 +186,9 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] =
|
||||
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:
|
||||
@@ -177,50 +198,36 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] =
|
||||
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)]
|
||||
|
||||
i = 0
|
||||
|
||||
if aheaderrow:
|
||||
for j in range(columns):
|
||||
aaarray[i][j] = aheaderrow[j]
|
||||
aaarray[0] = aheaderrow[:columns]
|
||||
|
||||
i += 1
|
||||
for i in range(1 if aheaderrow else 0, rows):
|
||||
ii = i - 1 if aheaderrow else i
|
||||
|
||||
j = 0
|
||||
|
||||
ii = 0
|
||||
for i in range(i, rows):
|
||||
if aheadercolumn:
|
||||
aii = i
|
||||
aaarray[i][0] = aheadercolumn[ii]
|
||||
|
||||
if aheaderrow:
|
||||
aii -= 1
|
||||
j = 1 if aheadercolumn else 0
|
||||
aaarray[i][j:] = map(str, aarray[ii][:columns - j])
|
||||
|
||||
aaarray[i][j] = aheadercolumn[aii]
|
||||
|
||||
j += 1
|
||||
|
||||
jj = 0
|
||||
for j in range(j, columns):
|
||||
aaarray[i][j] = str(aarray[ii][jj])
|
||||
|
||||
jj += 1
|
||||
|
||||
j = 0
|
||||
ii += 1
|
||||
|
||||
return table(aaarray, headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder, cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
return table(aaarray, headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder,
|
||||
cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
|
||||
|
||||
def functions(xmin: float, xmax: float, xscl: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: bool = False, title: Optional[str] = None, style: int = 2) -> 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: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
"""Convert one or more functions to array and output as table"""
|
||||
if not afunctions:
|
||||
return 1
|
||||
@@ -229,11 +236,11 @@ def functions(xmin: float, xmax: float, xscl: float, afunctions: Sequence[Callab
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xscl <= 0:
|
||||
print("xscl must be greater than zero.", file=sys.stderr)
|
||||
if xstep <= 0:
|
||||
print("xstep must be greater than zero.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
rows = int(((xmax - xmin) * xscl)) + 1
|
||||
rows = int(((xmax - xmin) / xstep)) + 1
|
||||
columns = len(afunctions) + 1
|
||||
|
||||
aaheaderrow = ["x", "y"]
|
||||
@@ -241,23 +248,24 @@ def functions(xmin: float, xmax: float, xscl: float, afunctions: Sequence[Callab
|
||||
|
||||
aheaderrow = [""] * columns
|
||||
|
||||
for j in range(columns):
|
||||
if j < (length - 1) or len(afunctions) == 1:
|
||||
aheaderrow[j] = aaheaderrow[j]
|
||||
else:
|
||||
aheaderrow[j] = aaheaderrow[length - 1] + str(j - length + 2)
|
||||
if len(afunctions) == 1:
|
||||
aheaderrow = aaheaderrow
|
||||
else:
|
||||
aheaderrow = 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)]
|
||||
|
||||
for i in range(rows):
|
||||
aarray[i][0] = (i / xscl) + xmin
|
||||
aarray[i][0] = (i * xstep) + xmin
|
||||
|
||||
for j, function in enumerate(afunctions):
|
||||
aarray[i][j + 1] = function(aarray[i][0])
|
||||
aarray[i][1:] = [function(aarray[i][0]) for function in afunctions]
|
||||
|
||||
return array(aarray, aheaderrow, None, headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder, cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
return array(aarray, aheaderrow, None, headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder,
|
||||
cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
|
||||
|
||||
def function(xmin: float, xmax: float, xscl: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: bool = False, title: Optional[str] = None, style: int = 2) -> 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: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
||||
"""Convert single function to array and output as table"""
|
||||
return functions(xmin, xmax, xscl, [afunction], headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder, cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
return functions(xmin, xmax, xstep, [afunction], headerrow=headerrow, headercolumn=headercolumn,
|
||||
tableborder=tableborder, cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style)
|
||||
|
102
python/test.py
102
python/test.py
@@ -21,17 +21,17 @@ columns = 5
|
||||
|
||||
xmin = -10
|
||||
xmax = 10
|
||||
xscl = 2
|
||||
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)]
|
||||
for k in range(len(tables.styles)):
|
||||
tables.array(array, None, None, style=k)
|
||||
for style in tables.style_types:
|
||||
tables.array(array, None, None, style=style)
|
||||
|
||||
array = [[random.random() for j in range(columns)] for i in range(rows)]
|
||||
for k in range(len(tables.styles)):
|
||||
tables.array(array, None, None, style=k)
|
||||
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"],
|
||||
@@ -39,8 +39,9 @@ array = [["Header row/column 1", "Header row 2", "Header row 3", "Header row 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 k in range(len(tables.styles)):
|
||||
tables.array(array, None, None, headerrow=True, headercolumn=True, style=k)
|
||||
for style in tables.style_types:
|
||||
tables.array(array, None, None, headerrow=True,
|
||||
headercolumn=True, style=style)
|
||||
|
||||
print("\nOutput array as table with separate header row and column\n")
|
||||
array = [["Data {0:n}".format(i + j) for j in range(4)]
|
||||
@@ -50,44 +51,52 @@ headerrow = ["Header row/column 1", "Header row 2",
|
||||
headercolumn = ["Header column 2", "Header column 3",
|
||||
"Header column 4", "Header column 5"]
|
||||
|
||||
for k in range(len(tables.styles)):
|
||||
tables.array(array, headerrow, headercolumn, headerrow=True, headercolumn=True, cellborder=True, style=k)
|
||||
tables.array(array, headerrow, headercolumn, headerrow=True, headercolumn=True, style=k)
|
||||
tables.array(array, headerrow[:-1], None, headerrow=True, style=k)
|
||||
tables.array(array, None, [headerrow[0]] + headercolumn[:-1], headercolumn=True, style=k)
|
||||
tables.array(array, None, None, cellborder=True, style=k)
|
||||
tables.array(array, None, None, tableborder=False, style=k)
|
||||
tables.array(array, headerrow, headercolumn, tableborder=False, headerrow=True, headercolumn=True, style=k)
|
||||
tables.array(array, headerrow[:-1], None, tableborder=False, headerrow=True, style=k)
|
||||
tables.array(array, None, [headerrow[0]] + headercolumn[:-1], tableborder=False, headercolumn=True, style=k)
|
||||
tables.array(array, None, None, tableborder=False, cellborder=True, style=k)
|
||||
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[:-1], None, headerrow=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)
|
||||
|
||||
array = [[bool(random.getrandbits(1)) for j in range(columns)]
|
||||
for i in range(rows)]
|
||||
for k in range(len(tables.styles)):
|
||||
tables.array(array, None, None, style=k)
|
||||
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))
|
||||
sortdimension = 0
|
||||
array = sorted(array, key=lambda x: x[sortdimension])
|
||||
for k in range(len(tables.styles)):
|
||||
tables.array(array, None, None, style=k)
|
||||
for style in tables.style_types:
|
||||
tables.array(array, None, None, style=style)
|
||||
|
||||
print("\nOutput single function as table\n")
|
||||
for k in range(len(tables.styles)):
|
||||
tables.function(xmin, xmax, xscl, afunction, headerrow=True, style=k)
|
||||
for k in range(len(tables.styles)):
|
||||
tables.function(xmin, xmax, xscl, lambda x: x + 1, headerrow=True, style=k)
|
||||
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)
|
||||
|
||||
print("\nOutput multiple functions as table\n")
|
||||
for k in range(len(tables.styles)):
|
||||
tables.functions(xmin, xmax, xscl, [
|
||||
function1, function2], headerrow=True, style=k)
|
||||
for k in range(len(tables.styles)):
|
||||
tables.functions(xmin, xmax, xscl, [
|
||||
lambda x: 2 * x, lambda x: x ** 2], headerrow=True, style=k)
|
||||
for style in tables.style_types:
|
||||
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)
|
||||
|
||||
height = 160
|
||||
width = 160
|
||||
@@ -99,24 +108,25 @@ ymax = 20
|
||||
|
||||
print("\nOutput single array as plot\n")
|
||||
array = [range(i, i + 2) for i in range(10)]
|
||||
for k in range(len(graphs.styles)):
|
||||
graphs.array(height, width, xmin, xmax, ymin, ymax, array, style=k)
|
||||
for style in graphs.style_types:
|
||||
graphs.array(height, width, xmin, xmax, ymin, ymax, array, style=style)
|
||||
|
||||
print("\nOutput single function as graph\n")
|
||||
for k in range(len(graphs.styles)):
|
||||
graphs.function(height, width, xmin, xmax, ymin, ymax, afunction, style=k)
|
||||
for k in range(len(graphs.styles)):
|
||||
for style in graphs.style_types:
|
||||
graphs.function(height, width, xmin, xmax, ymin,
|
||||
ymax, lambda x: x + 1, style=k)
|
||||
ymax, afunction, style=style)
|
||||
for style in graphs.style_types:
|
||||
graphs.function(height, width, xmin, xmax, ymin,
|
||||
ymax, lambda x: x + 1, style=style)
|
||||
|
||||
print("\nOutput multiple functions as graph\n")
|
||||
for k in range(len(graphs.styles)):
|
||||
graphs.functions(height, width, xmin, xmax, ymin,
|
||||
ymax, [function1, function2], style=k)
|
||||
for k in range(len(graphs.styles)):
|
||||
for style in graphs.style_types:
|
||||
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=k)
|
||||
lambda x: 2 * x, lambda x: x ** 2], style=style)
|
||||
|
||||
for k in range(len(graphs.styles)):
|
||||
graphs.functions(height, width, -(2 * math.pi), 2 * math.pi, -4,
|
||||
4, [math.sin, math.cos, math.tan], axisunitslabel=False, style=k)
|
||||
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)
|
||||
|
Reference in New Issue
Block a user