add tui module and example, add contex manager cd and the method run_file in helper

import vendor modules only once during import

change text color for the method cad_wireframe in geometry_plot_pylab and add hide key
This commit is contained in:
2020-01-13 10:25:33 +01:00
parent 4fc4903dc2
commit 033cb92dc4
28 changed files with 1848 additions and 165 deletions

View File

@@ -10,16 +10,15 @@
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
from mpl_toolkits.mplot3d import Axes3D
assert Axes3D
import pylab
from pylib.data import seq, unique_list
from pylib.function import (
sample_half_open, sample_half_open_seq,
b_spline_basis, b_spline_knots, b_spline_curve_with_knots)
from pylib.helper import timeit
from mpl_toolkits.mplot3d import Axes3D
assert Axes3D
import pylab
def b_spline_basis_functions(knots, pmax=None):
m = len(knots) - 1 # m = 3
@@ -27,7 +26,7 @@ def b_spline_basis_functions(knots, pmax=None):
pmax = m
u = seq(knots[0], knots[-1], (knots[-1]-knots[0])/50)
Nij = [[[b_spline_basis(knots, i, j)(uj) for uj in u] for
i in range(m-j)] for j in range(pmax+1)]
i in range(m-j)] for j in range(pmax+1)]
Nj = [[sum(i) for i in zip(*Nij[:][j])] for j in range(pmax+1)]
return u, Nij, Nj
@@ -180,14 +179,14 @@ def example5():
(52.5444045251438, 42.8799268641912, -0.372905635604393),
(52.7547311828766, 42.9295775362814, -0.373148784408123)]
Uu = (0.0228378692889125,
0.0649893839528174,
0.130414237399236,
0.195679174703882,
0.260898113556269,
0.326065686455556,
0.391167888702703,
0.456323551413636,
0.521155975846261)
0.0649893839528174,
0.130414237399236,
0.195679174703882,
0.260898113556269,
0.326065686455556,
0.391167888702703,
0.456323551413636,
0.521155975846261)
mult = (4, 2, 2, 2, 2, 2, 2, 2, 4)
U = [Uu[i] for i in range(len(Uu)) for j in range(mult[i])]
@@ -205,9 +204,9 @@ def example5():
C = b_spline_curve_with_knots(p, P, U)
Cu = sample_half_open(C, U[0], U[-1])
plot_b_spline_curve_with_knots(
Cu = Cu,
CU = sample_half_open_seq(C, Uu) if knots_cp else None,
P = P if knots_cp else None,
Cu=Cu,
CU=sample_half_open_seq(C, Uu) if knots_cp else None,
P=P if knots_cp else None,
figure_id='N5 C')
pylab.gca().set_zlim3d([-1, 0])
@@ -239,14 +238,14 @@ def example6():
(52.5444045251438, 42.8799268641912, -0.372905635604393),
(52.7547311828766, 42.9295775362814, -0.373148784408123)]
Uu = (0.0228378692889125,
0.0649893839528174,
0.130414237399236,
0.195679174703882,
0.260898113556269,
0.326065686455556,
0.391167888702703,
0.456323551413636,
0.521155975846261)
0.0649893839528174,
0.130414237399236,
0.195679174703882,
0.260898113556269,
0.326065686455556,
0.391167888702703,
0.456323551413636,
0.521155975846261)
mult = (4, 2, 2, 2, 2, 2, 2, 2, 4)
U = [Uu[i] for i in range(len(Uu)) for j in range(mult[i])]
@@ -275,86 +274,86 @@ def example6():
def example7():
P = [(-69.99999999999, 6.23746531921, 119.99999999999),
(-69.99999999999, 5.72567326271, 120.0),
(-70.0, 5.2192123782, 120.0),
(-70.0, 4.718082665681, 120.0),
(-70.0, 4.222284125153, 120.0),
(-70.0, 3.731816756617, 120.0),
(-70.0, 3.246680560071, 120.0),
(-70.0, 2.766875535516, 120.0),
(-70.0, 2.292401682953, 120.0),
(-70.0, 1.82325900238, 120.0),
(-70.0, 1.359447493799, 120.0),
(-70.0, 0.900967157208, 120.0),
(-70.0, 0.447817992609, 120.0),
(-69.99999999999, -1.642767914458, 119.99999999999),
(-69.95823302652, -3.284128624962, 119.96005936915),
(-69.87482030195, -4.920567121601, 119.88028704462),
(-69.7502001661, -6.548567099244, 119.76113869291),
(-69.58513326705, -8.164660004507, 119.60341679526),
(-69.38069590753, -9.765475979336, 119.40825949383),
(-69.13826152984, -11.34779669959, 119.17711816806),
(-68.85947036564, -12.90861011093, 118.91172378914),
(-68.54618721656, -14.44516705927, 118.61404198996),
(-68.20044740283, -15.95503981856, 118.28621691953),
(-67.82439084537, -17.43618251348, 117.93050381741),
(-67.42018430873, -18.88699343893, 117.54919035943),
(-66.38758251206, -22.29350540161, 116.57795367665),
(-65.73413448053, -24.21919416036, 115.96554908806),
(-65.03556391343, -26.07956263873, 115.31370192302),
(-64.29648522575, -27.87744521951, 114.62724902124),
(-63.52042791056, -29.61397650413, 113.91024118834),
(-62.71277001846, -31.2871331673, 113.16847249966),
(-61.87901674025, -32.89613508726, 112.4077395496),
(-61.02226890253, -34.44424317029, 111.63151855295),
(-60.14386048877, -35.93622309031, 110.841629897),
(-59.24623340527, -37.37387103089, 110.04091803203),
(-58.33313678246, -38.756819007, 109.23340236149),
(-57.40516397009, -40.08965381341, 108.42014822445),
(-55.01642244455, -43.34968716212, 106.34685334397),
(-53.53813210335, -45.21002358427, 105.08230356996),
(-52.03458081272, -46.96355473328, 103.81650090025),
(-50.50977333246, -48.61777575741, 102.55504546095),
(-48.96692322622, -50.17949288551, 101.30260097008),
(-47.40801072014, -51.65517742458, 100.06277532323),
(-45.83395649691, -53.05076391735, 98.838417665337),
(-44.24537288755, -54.37123987567, 97.632189028576),
(-42.64319994423, -55.62048722024, 96.446976549361),
(-41.02853017217, -56.80167054344, 95.285780596074),
(-39.40157208613, -57.91802439479, 94.15110945761),
(-37.76099941739, -58.97317114245, 93.044667943398),
(-33.51657488475, -61.52870256173, 90.285109332808),
(-30.89322834847, -62.94313830978, 88.677041723595),
(-28.23238814008, -64.22251443102, 87.15321189152),
(-25.55002271787, -65.36600940426, 85.732620462342),
(-22.80465037824, -66.39367032451, 84.407541386373),
(-20.07652968241, -67.27260606721, 83.231827543651),
(-17.27816204332, -68.0519427367, 82.167075623263),
(-14.44812618974, -68.69718956814, 81.25538726298),
(-11.58521735637, -69.2186257426, 80.51004437835),
(-8.697808521544, -69.60868640648, 79.943540692506),
(-5.807821595725, -69.86923851767, 79.563401742733),
(-2.906442029202, -70.00000000001, 79.372539331945),
(0.372271370757, -69.99999999999, 79.372539331937),
(0.762465017147, -69.99999999999, 79.372539331936),
(1.17058093917, -69.99999999999, 79.372539331935),
(1.596619136825, -69.99999999999, 79.372539331934),
(2.040579610114, -69.99999999999, 79.372539331932),
(2.502462359036, -69.99999999999, 79.372539331931),
(2.982267383591, -69.99999999998, 79.37253933193),
(3.479994683778, -69.99999999998, 79.372539331929),
(3.995644259599, -69.99999999998, 79.372539331927),
(4.529216111052, -69.99999999998, 79.372539331926),
(5.080710238139, -69.99999999998, 79.372539331925),
(5.650126640858, -69.99999999997, 79.372539331923),
(6.23746531921, -69.99999999997, 79.372539331922)]
(-69.99999999999, 5.72567326271, 120.0),
(-70.0, 5.2192123782, 120.0),
(-70.0, 4.718082665681, 120.0),
(-70.0, 4.222284125153, 120.0),
(-70.0, 3.731816756617, 120.0),
(-70.0, 3.246680560071, 120.0),
(-70.0, 2.766875535516, 120.0),
(-70.0, 2.292401682953, 120.0),
(-70.0, 1.82325900238, 120.0),
(-70.0, 1.359447493799, 120.0),
(-70.0, 0.900967157208, 120.0),
(-70.0, 0.447817992609, 120.0),
(-69.99999999999, -1.642767914458, 119.99999999999),
(-69.95823302652, -3.284128624962, 119.96005936915),
(-69.87482030195, -4.920567121601, 119.88028704462),
(-69.7502001661, -6.548567099244, 119.76113869291),
(-69.58513326705, -8.164660004507, 119.60341679526),
(-69.38069590753, -9.765475979336, 119.40825949383),
(-69.13826152984, -11.34779669959, 119.17711816806),
(-68.85947036564, -12.90861011093, 118.91172378914),
(-68.54618721656, -14.44516705927, 118.61404198996),
(-68.20044740283, -15.95503981856, 118.28621691953),
(-67.82439084537, -17.43618251348, 117.93050381741),
(-67.42018430873, -18.88699343893, 117.54919035943),
(-66.38758251206, -22.29350540161, 116.57795367665),
(-65.73413448053, -24.21919416036, 115.96554908806),
(-65.03556391343, -26.07956263873, 115.31370192302),
(-64.29648522575, -27.87744521951, 114.62724902124),
(-63.52042791056, -29.61397650413, 113.91024118834),
(-62.71277001846, -31.2871331673, 113.16847249966),
(-61.87901674025, -32.89613508726, 112.4077395496),
(-61.02226890253, -34.44424317029, 111.63151855295),
(-60.14386048877, -35.93622309031, 110.841629897),
(-59.24623340527, -37.37387103089, 110.04091803203),
(-58.33313678246, -38.756819007, 109.23340236149),
(-57.40516397009, -40.08965381341, 108.42014822445),
(-55.01642244455, -43.34968716212, 106.34685334397),
(-53.53813210335, -45.21002358427, 105.08230356996),
(-52.03458081272, -46.96355473328, 103.81650090025),
(-50.50977333246, -48.61777575741, 102.55504546095),
(-48.96692322622, -50.17949288551, 101.30260097008),
(-47.40801072014, -51.65517742458, 100.06277532323),
(-45.83395649691, -53.05076391735, 98.838417665337),
(-44.24537288755, -54.37123987567, 97.632189028576),
(-42.64319994423, -55.62048722024, 96.446976549361),
(-41.02853017217, -56.80167054344, 95.285780596074),
(-39.40157208613, -57.91802439479, 94.15110945761),
(-37.76099941739, -58.97317114245, 93.044667943398),
(-33.51657488475, -61.52870256173, 90.285109332808),
(-30.89322834847, -62.94313830978, 88.677041723595),
(-28.23238814008, -64.22251443102, 87.15321189152),
(-25.55002271787, -65.36600940426, 85.732620462342),
(-22.80465037824, -66.39367032451, 84.407541386373),
(-20.07652968241, -67.27260606721, 83.231827543651),
(-17.27816204332, -68.0519427367, 82.167075623263),
(-14.44812618974, -68.69718956814, 81.25538726298),
(-11.58521735637, -69.2186257426, 80.51004437835),
(-8.697808521544, -69.60868640648, 79.943540692506),
(-5.807821595725, -69.86923851767, 79.563401742733),
(-2.906442029202, -70.00000000001, 79.372539331945),
(0.372271370757, -69.99999999999, 79.372539331937),
(0.762465017147, -69.99999999999, 79.372539331936),
(1.17058093917, -69.99999999999, 79.372539331935),
(1.596619136825, -69.99999999999, 79.372539331934),
(2.040579610114, -69.99999999999, 79.372539331932),
(2.502462359036, -69.99999999999, 79.372539331931),
(2.982267383591, -69.99999999998, 79.37253933193),
(3.479994683778, -69.99999999998, 79.372539331929),
(3.995644259599, -69.99999999998, 79.372539331927),
(4.529216111052, -69.99999999998, 79.372539331926),
(5.080710238139, -69.99999999998, 79.372539331925),
(5.650126640858, -69.99999999997, 79.372539331923),
(6.23746531921, -69.99999999997, 79.372539331922)]
Uu = (-3.932977778518,
1.445001333941e-14,
14.427668850884,
34.626262491172,
65.717581403178,
114.40570375563,
120.64191760942)
1.445001333941e-14,
14.427668850884,
34.626262491172,
65.717581403178,
114.40570375563,
120.64191760942)
mult = (14, 12, 12, 12, 12, 12, 14)
U = [Uu[i] for i in range(len(Uu)) for j in range(mult[i])]
@@ -376,3 +375,4 @@ def example7():
if __name__ == "__main__":
example7()
pylab.show()

193
examples/tui.py Executable file
View File

@@ -0,0 +1,193 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Example tui.
:Date: 2020-01-12
.. module:: tui
:platform: *nix, Windows
:synopsis: Example tui.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
import curses
import locale
from pylib.data import seq
from pylib.function import sine_wave, cosine_wave, hypotrochoid, to_str
from pylib.tui import TUI
locale.setlocale(locale.LC_ALL, '')
CODE = locale.getpreferredencoding()
def standard_text(window):
window.text(
"1 - Show test page\n" +
"2 - Show Hypotrochoid example\n" +
"h - Show help page\n" +
"q - Exit", 1, 2)
def help_text(window, tui, str_width_height):
screen_height, screen_width = window.getmaxyx()
last_screen_row = screen_height - 1
tui.color_table() # print colors
window.text("screen width and height", 1, last_screen_row-3)
window.text("\u2502", 3, last_screen_row-2) # \u2502
window.text("\u2502", 3, last_screen_row-1) # \u2193 v
window.text("last pressed key as Unicode code point", len(str_width_height)+2, last_screen_row-2)
window.text("\u2502", 1+len(str_width_height)+1, last_screen_row-1)
num_reloads = "number of screen reloads\u256e"
# screen_width-1-len(num_reloads.encode('utf-16'))//2
window.text(num_reloads, screen_width-1-len(num_reloads), last_screen_row-1)
def test_text(window):
window.text("Test page", 1, 2)
def full_text(window, t):
#window.clear()
screen_height, screen_width = window.getmaxyx()
# Sine
f = sine_wave(A=0.5, k=0.1, D=0.5)
frame = to_str(f, x_0=0, x_1=1, h=screen_height-2, w=screen_width, t=t)
# Turtle
import drawille
turtle = drawille.Turtle()
for _ in range(36):
turtle.right(10 + t)
for _ in range(0, 36):
turtle.right(10)
turtle.forward(8)
frame = turtle.frame()
# size for forward 1: width 11+1, height 6
# size for forward 2: width 23+1, height 12
# size for forward 5: width 57+1, height 30
# size for forward 6: width 69+1, height 36
# size for forward 10: width 114+1, height 58
# n 12*n 6*n
# Hypotrochoid
demo = 2
c = drawille.Canvas()
R = 20
r = 6
d = 30
if demo == 1:
r = int(t/50)%19+1
x, y, theta = hypotrochoid(R, r, d)
for i in seq(0, theta[1]*0.98, theta[1]/10000):
c.set(x(i+t), y(i+t))
a = R+d+1 # R-r+d+1
frame = c.frame(min_x=-a, max_x=a, min_y=-a, max_y=a)
if demo == 2:
d = int(t)%21
x, y, theta = hypotrochoid(R, r, d)
for i in seq(0, theta[1]*0.98, theta[1]/5000):
c.set(x(i+t), y(i+t))
a = 2*R # R-r+d+1
frame = c.frame(min_x=-a, max_x=a, min_y=-a, max_y=a)
window.text(frame, padding_top=1, color_pair=10)
window.text("R=%2s r=%2s d=%2s" % (R, r, d), 1, int(a/2)+2)
def win_charts_line(window, f_list, x_0, x_1, w, t, color_pairs, densities):
win_chart_line, _ = window.textbox(height=8, width=52, y=9, x=5, borders=True)
# width 100 pixel / 2 pixels/column = 50 columns
for f, color_pair, density in zip(f_list, color_pairs, densities):
frame = to_str(f, x_0=x_0, x_1=x_1, h=6, w=w, t=t, density=density)
win_chart_line.text(frame, color_pair=color_pair)
def win_chart_histogram(window, f, x_0, x_1, w, t):
newwin2, _ = window.textbox(height=4, width=52, y=9+8, x=5, borders=True)
#frame2 = function_frame(f, x_0=x_0, x_1=x_1, h=2, w=w, t=t, char_set="histogram")
frame2 = to_str(f, x_0=x_0, x_1=x_1, h=2, w=w, t=t, char_set="histogram")
newwin2.text(frame2, color_pair=3)
frame3 = to_str(f, x_0=x_0, x_1=x_1, h=2, w=w, t=t, density=0.5, char_set="histogram")
newwin2.text(frame3, color_pair=4)
def main(delay=1):
"""
no window change for mouse events and resize events.
"""
tui = TUI(delay)
initscr = tui.initscr
try:
tui.last_pressed_ch = 0 # getch() Note that the integer returned does not have to be in ASCII range: function keys, keypad keys and so on return numbers higher than 256.
tui.last_pressed_mouse_ch = 0
i = 1
while True:
tui.clear()
screen_height, screen_width = initscr.getmaxyx()
str_width_height = str(screen_width) + 'x' + str(screen_height) # width and height of the screen
footer_left = [str_width_height, str(tui.last_pressed_ch)] # and last pressed key as char integer ( Unicode code point)
i += 1 # number of screen reloads
initscr.border("Python curses in action!", footer_left, str(i))
if tui.last_pressed_ch == ord('1'):
test_text(initscr)
elif tui.last_pressed_ch == ord('2'):
full_text(initscr, i)
elif tui.last_pressed_ch == ord('h'):
help_text(initscr, tui, str_width_height)
elif tui.last_pressed_ch == ord('q'):
break
else:
standard_text(initscr)
x_0 = 0
x_1 = 1 # TODO
w = 50
t = i/100
# define function normed to the range of [0, 1]
y_1 = sine_wave(A=.5, k=.1, D=.5)
y_1_2 = sine_wave(A=.5, k=.1, f=1+1/3, phi=30, D=.5, degree=True)
y_1_3 = sine_wave(A=.5, k=.1, f=1+2/3, phi=60, D=.5, degree=True)
y_2 = cosine_wave(A=.5, k=.1, f=2, D=.5)
win_charts_line(initscr, [y_1, y_1_2, y_1_3, y_2], x_0, x_1, w, t, [209, 215, 221, 227], [5, 5, 5, 5])
#win_charts_line(initscr, [y_1, y_1_2, y_1_3, y_2], x_0, x_1, w, t, [10, 7, 7, 7], [5, 1, 1, 1])
#y_2 = cosine_wave(A=0.5, k=0.2, f=2, D=0.5)
#win_charts_line(initscr, [y_1, y_2], x_0, x_1, w, t, [10, 7], [5, 1])
win_chart_histogram(initscr, y_1, x_0, x_1, w, t)
# mouse event has occurred
if tui.last_pressed_mouse_ch == curses.KEY_MOUSE:
try:
_, mx, my, _, _ = curses.getmouse()
initscr.text("Mouse pos " + str(mx) + " " + str(my), 1, 6)
initscr.text(initscr.instr(my, mx, 5), mx, my, color_pair=5)
except curses.error:
pass
# std content
# cursor position (last addstr is cursor position)
if tui.cursor_visibility > 0:
initscr.text("", 1, 6)
tui.refresh() # update the screen
tui.getch()
except Exception as e:
import traceback
print('\n'.join(traceback.format_exception(*sys.exc_info())))
finally:
tui.end()
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
main(sys.argv[1])
else:
main()