add missing imports for to_str function

This commit is contained in:
2019-11-15 16:11:30 +01:00
parent 1de007a285
commit b0db7ab3dc
2 changed files with 130 additions and 0 deletions

125
pylib/drawblock.py Normal file
View File

@@ -0,0 +1,125 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def histogram(f, x=None):
"""Histogram chart with block symbols.
dots:
,_,
|8|
|7|
|6|
|5|
|4|
|3|
|2|
|1|
```
▁▂▃▄▅▆▇█
12345678
"""
from collections import defaultdict
chars = defaultdict(lambda: defaultdict(int))
# w/o comma no 2d tuple
pixel_map = ((0x2581,), # Lower one eighth block, also used for value 0
(0x2582,), # Lower one quarter block
(0x2583,), # Lower three eighths block
(0x2584,), # Lower half block
(0x2585,), # Lower five eighths block
(0x2586,), # Lower three quarters block
(0x2587,), # Lower seven eighths block
(0x2588,)) # Full block
#print(pixel_map)
pixel_rows = 8
pixel_cols = 1
def normalize(coord):
coord_type = type(coord)
if coord_type == int:
return coord
if coord_type == float:
return int(round(coord))
raise TypeError("Unsupported coordinate type <{0}>".format(type(coord)))
def get_pos(x, y):
"""Convert x, y to cols, rows"""
return x // 1, y // pixel_rows
def set(x, y):
"""Set a pixel of the :class:`Canvas` object.
:param x: x coordinate of the pixel
:param y: y coordinate of the pixel
"""
x = normalize(x)
y = normalize(y)
col, row = get_pos(x, y)
if type(chars[row][col]) != int:
return
chars[row][col] |= pixel_map[y % pixel_rows][x % pixel_cols]
# fill also below the tip
if row > 0:
for i in range(row):
chars[i][col] |= pixel_map[pixel_rows-1][x % pixel_cols]
def rows(min_x=None, min_y=None, max_x=None, max_y=None):
"""Returns a list of the current :class:`Canvas` object lines.
:param min_x: (optional) minimum x coordinate of the canvas
:param min_y: (optional) minimum y coordinate of the canvas
:param max_x: (optional) maximum x coordinate of the canvas
:param max_y: (optional) maximum y coordinate of the canvas
"""
if not chars.keys():
return []
minrow = min_y // pixel_rows if min_y is not None else min(chars.keys())
maxrow = (max_y - 1) // pixel_rows if max_y is not None else max(chars.keys())
mincol = min_x // 1 if min_x is not None else min(min(x.keys()) for x in chars.values())
maxcol = (max_x - 1) // 1 if max_x is not None else max(max(x.keys()) for x in chars.values())
result = []
for rownum in range(minrow, maxrow+1):
if not rownum in chars:
result.append('')
continue
maxcol = (max_x - 1) // 1 if max_x is not None else max(chars[rownum].keys())
row = []
for x in range(mincol, maxcol+1):
char = chars[rownum].get(x)
if not char:
row.append(chr(0x20))
#row.append(chr(0x2588))
elif type(char) != int:
row.append(char)
else:
row.append(chr(char))
result.append(''.join(row))
return result
def frame(min_x=None, min_y=None, max_x=None, max_y=None):
"""String representation of the current :class:`Canvas` object pixels.
:param min_x: (optional) minimum x coordinate of the canvas
:param min_y: (optional) minimum y coordinate of the canvas
:param max_x: (optional) maximum x coordinate of the canvas
:param max_y: (optional) maximum y coordinate of the canvas
"""
import os
result = os.linesep.join(reversed(rows(min_x, min_y, max_x, max_y)))
return result # py2: ret.encode('utf-8')
if x is None:
for xi, y in enumerate(f):
set(xi, y)
else:
for xi, y in zip(x, f):
set(xi, y)
return frame()

View File

@@ -11,6 +11,7 @@
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de> .. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
""" """
import math import math
from pylib.data import seq
from pylib.mathematics import lcm from pylib.mathematics import lcm
def transformation(f, scale_vertical=1, scale_horizontal=1, def transformation(f, scale_vertical=1, scale_horizontal=1,
@@ -385,6 +386,8 @@ def to_str(f, x=None, x_0=0, x_1=1, t=None, h=10, w=80, density=1, char_set="lin
window_factor = w/(x_1-x_0) window_factor = w/(x_1-x_0)
frame = "" frame = ""
if char_set in ["line", "braille"]: if char_set in ["line", "braille"]:
import drawille
pixels_horizontal = 2 pixels_horizontal = 2
pixels_vertical = 4 pixels_vertical = 4
a = -(h*pixels_vertical-1) a = -(h*pixels_vertical-1)
@@ -399,6 +402,8 @@ def to_str(f, x=None, x_0=0, x_1=1, t=None, h=10, w=80, density=1, char_set="lin
#frame = canvas.frame(min_x=a*pixel_per_char, min_y=1, max_x=b*pixel_per_char, max_y=21) #frame = canvas.frame(min_x=a*pixel_per_char, min_y=1, max_x=b*pixel_per_char, max_y=21)
frame = canvas.frame() frame = canvas.frame()
elif char_set in ["histogram", "block"]: elif char_set in ["histogram", "block"]:
import drawblock
pixels_horizontal = 1 pixels_horizontal = 1
pixels_vertical = 8 pixels_vertical = 8
a = h*pixels_vertical-1 a = h*pixels_vertical-1