/
figure.py
68 lines (50 loc) · 1.66 KB
/
figure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
#
from .hist import hist
from .plot import plot
from .table import table
from .helpers import create_padding_tuple
def figure(*args, **kwargs):
return Figure(*args, **kwargs)
class Figure(object):
def __init__(self, width=None, padding=0):
self._content = []
self._width = width
self._subfigures = None
self._padding = create_padding_tuple(padding)
return
def aprint(self, string):
self._content.append(string.split("\n"))
return
def show(self):
print(self.get_string())
return
def get_string(self, remove_trailing_whitespace=True):
lines = []
padding_lr = self._padding[1] + self._padding[3]
if self._width is None:
width = max([len(line) for c in self._content for line in c])
width += padding_lr
else:
width = self._width
# Top padding
lines += self._padding[0] * [" " * width]
pr = " " * self._padding[1]
pl = " " * self._padding[3]
lines += [
pl + line[: width - padding_lr] + pr for c in self._content for line in c
]
# Bottom padding
lines += self._padding[2] * [" " * width]
if remove_trailing_whitespace:
lines = [line.rstrip() for line in lines]
return "\n".join(lines)
def hist(self, *args, **kwargs):
self._content.append(hist(*args, **kwargs))
return
def plot(self, *args, **kwargs):
self._content.append(plot(*args, **kwargs))
return
def table(self, *args, **kwargs):
self._content.append(table(*args, **kwargs))
return