Updated to support and use modern C++ features and added more examples.

This commit is contained in:
Teal Dulcet
2022-07-14 01:33:38 -07:00
parent 37e257c5ee
commit 274f949079
11 changed files with 1123 additions and 380 deletions

View File

@@ -2,20 +2,20 @@
### Usage
Requires Python 3.5 or greater and the [wcwidth library](https://pypi.org/project/wcwidth/), which users can install with: `pip3 install wcwidth`.
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.
Complete versions of all of the examples below and more can be found in the [test.py](test.py) file.
Run with: `python3 test.py`.
#### Output char array as table
#### Output str array as table
```py
import tables
# Set array
tables.array(array, None, None, headerrow=True, headercolumn=True)
tables.array(array, headerrow=True, headercolumn=True)
```
Table cells can contain [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters), but not newlines and tabs.
@@ -42,9 +42,9 @@ Output same as example above.
```py
import tables
# Set array
# Set array, can be any sequence data type
tables.array(array, None, None)
tables.array(array)
```
![](../images/array%20to%20table.png)
@@ -60,7 +60,7 @@ sortdimension = 0 # Column to sort by
array = sorted(array, key=lambda x: x[sortdimension])
tables.array(array, None, None)
tables.array(array)
```
![](../images/sorted%20array%20to%20table.png)
@@ -82,6 +82,22 @@ tables.function(xmin, xmax, xscl, afunction, headerrow=True)
![](../images/function%20to%20table.png)
#### Output lambda function as table
```py
import tables
xmin = -10
xmax = 10
xscl = 2
afunction = lambda x: x + 1
tables.function(xmin, xmax, xscl, afunction, headerrow=True)
```
Output same as example above.
#### Output multiple functions as table
```py
@@ -105,6 +121,23 @@ tables.functions(xmin, xmax, xscl, functions, headerrow=True)
![](../images/multiple%20functions%20to%20table.png)
#### Output multiple lambda functions as table
```py
import tables
xmin = -10
xmax = 10
xscl = 2
# 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)
```
Output same as example above.
### Options
#### Header row
@@ -182,7 +215,7 @@ Values:
### Usage
Requires Python 3.5 or greater and the [wcwidth library](https://pypi.org/project/wcwidth/), which users can install with: `pip3 install wcwidth`.
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.
Complete versions of all of the examples below and more can be found in the [test.py](test.py) file.
@@ -190,7 +223,7 @@ Run with: `python3 test.py`.
If `height` is `0`, it will be set to the current height of the terminal (number of rows times four). If `width` is `0`, it will be set to the current width of the terminal (number of columns times two).
#### Output array as plot
#### Output single array as plot
```py
import graphs
@@ -203,7 +236,7 @@ xmax = 20
ymin = -20
ymax = 20
# Set array
# Set array, can be any sequence data type, but must have exactly two columns
graphs.array(height, width, xmin, xmax, ymin, ymax, array)
```
@@ -212,6 +245,8 @@ If `xmin` and `xmax` are both `0`, they will be set to the respective minimum an
![](../images/array%20to%20plot.png)
Use `graphs.arrays()` to plot multiple arrays, which can be of different sizes.
#### Output single function as graph
```py
@@ -233,6 +268,26 @@ graphs.function(height, width, xmin, xmax, ymin, ymax, afunction)
![](../images/function%20to%20graph.png)
#### Output lambda function as graph
```py
import graphs
height = 160
width = 160
xmin = -20
xmax = 20
ymin = -20
ymax = 20
afunction = lambda x: x + 1
graphs.function(height, width, xmin, xmax, ymin, ymax, afunction)
```
Output same as example above.
#### Output multiple functions as graph
```py
@@ -260,6 +315,27 @@ graphs.functions(height, width, xmin, xmax, ymin, ymax, functions)
![](../images/multiple%20functions%20to%20graph.png)
#### Output multiple lambda functions as graph
```py
import graphs
height = 160
width = 160
xmin = -20
xmax = 20
ymin = -20
ymax = 20
# 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]
graphs.functions(height, width, xmin, xmax, ymin, ymax, functions)
```
Output same as example above.
### Options
#### Border/Axis

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Teal Dulcet, CS546
@@ -113,7 +113,7 @@ def outputlabel(label: float) -> 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=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: int = 2, title: Optional[str] = None) -> int:
"""Output graph"""
if not array:
return 1
@@ -175,7 +175,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
adivisor = -divisor if i < yaxis else divisor
k = yaxis + adivisor
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and (i >= 4 or not axislabel) and not output:
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and i >= 4 and not output:
if (i <= k and (i + 4) > k):
label = ymax - (k / yscl)
@@ -202,7 +202,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
adivisor = -divisor if i < yaxis else divisor
k = yaxis + adivisor
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and (i >= 4 or not axislabel) and not output:
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and i >= 4 and not output:
if i <= k and (i + 4) > k:
strm += styles[style][7]
output = True
@@ -220,7 +220,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
adivisor = -divisor if j < xaxis else divisor
k = xaxis + adivisor
while ((j < xaxis and k >= j) or (j > xaxis and k < (j + 2))) and (j < (width - 4) or not axislabel) and not output:
while ((j < xaxis and k >= j) or (j > xaxis and k < (j + 2))) and j < (width - 4) and not output:
if j <= k and (j + 2) > k:
strm += styles[style][3]
output = True
@@ -314,7 +314,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: int = 2, color: int = 2, title: Optional[str] = None) -> int:
"""Convert one or more arrays to graph and output"""
if not aarrays:
return 1
@@ -328,6 +328,12 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
w = shutil.get_terminal_size()
if height == 0:
height = w.lines * 4
if width == 0:
width = w.columns * 2
aheight = height // 4
awidth = width // 2
@@ -365,7 +371,8 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
aaarray = [[0 for j in range(height)] for i in range(width)]
for j, aarray in enumerate(aarrays):
acolor = color + 1 if len(aarrays) == 1 else (j % (len(colors) - 2)) + 3
acolor = color + 1 if len(aarrays) == 1 else (j %
(len(colors) - 2)) + 3
for i in aarray:
if i[0] >= xmin and i[0] < xmax and i[1] >= ymin and i[1] < ymax:
@@ -381,17 +388,17 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
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: int = 2, color: int = 2, 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)
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: int = 2, color: int = 2, title: Optional[str] = None) -> int:
"""Convert one or more functions to graph and output"""
if color < 0 or color >= len(colors):
return 1
if len(afunctions) == 0:
if not afunctions:
return 1
w = shutil.get_terminal_size()
@@ -402,8 +409,8 @@ 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
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(
@@ -453,6 +460,6 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
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: int = 2, color: int = 2, 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)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Teal Dulcet, CS546
@@ -39,7 +39,7 @@ def strcol(str: str) -> int:
# return len(str)
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: bool = False, title: Optional[str] = None, style: int = 2) -> int:
"""Output char array as table"""
if not array:
return 1
@@ -158,7 +158,7 @@ def table(array: List[List[str]], headerrow: bool=False, headercolumn: bool=Fals
return 0
def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]], aheadercolumn: Optional[Sequence[Any]], 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: bool = False, title: Optional[str] = None, style: int = 2) -> int:
"""Convert array to char array and output as table"""
if not aarray:
return 1
@@ -220,9 +220,9 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]],
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, 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:
"""Convert one or more functions to array and output as table"""
if len(afunctions) == 0:
if not afunctions:
return 1
if xmin >= xmax:
@@ -258,6 +258,6 @@ def functions(xmin: float, xmax: float, xscl: float, afunctions: Sequence[Callab
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, 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:
"""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)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Teal Dulcet, CS546
@@ -78,11 +78,16 @@ for k in range(len(tables.styles)):
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)
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)
height = 160
width = 160
@@ -92,20 +97,26 @@ xmax = 20
ymin = -20
ymax = 20
print("\nOutput array as plot\n")
array = [[i + j for j in range(2)] for i in range(10)]
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)
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)):
graphs.function(height, width, xmin, xmax, ymin,
ymax, lambda x: x + 1, style=k)
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)):
graphs.functions(height, width, xmin, xmax, ymin, ymax, [
lambda x: 2 * x, lambda x: x ** 2], style=k)
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)
graphs.functions(height, width, -(2 * math.pi), 2 * math.pi, -4,
4, [math.sin, math.cos, math.tan], axisunitslabel=False, style=k)