-
Notifications
You must be signed in to change notification settings - Fork 0
/
diviewer.py
executable file
·115 lines (85 loc) · 4.05 KB
/
diviewer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/python
import logging
import datetime
if __name__ == "__main__":
from engine.support_class import LogMaster
from engine.stock_chomp import StockChomper
from engine.cliparse import parsecli
from engine import numeric, symbolic
from engine.spiral import draw_spiral, pretty_print_grid
from engine.img_gen import string_to_png
else:
from .engine.support_class import LogMaster
from .engine.stock_chomp import StockChomper
from .engine.cliparse import parsecli
from .engine import numeric, symbolic
from .engine.spiral import draw_spiral, pretty_print_grid
from .engine.img_gen import string_to_png
class DivergenceViewer(LogMaster):
def __init__(self, cli, loglevel=logging.DEBUG):
self.setLogger(self.__class__.__name__, loglevel)
self.cliparams = cli
self.datefrom = datetime.datetime.strptime(cli.beginning, "%Y-%m-%d").date()
self.stocksource = StockChomper(datefrom=self.datefrom,
loglevel=loglevel)
self.hashlist = []
self.symbolist = []
@property
def stockdata(self):
return self.stocksource.stock_cache
def load_stock_data(self):
self.logger.debug("Loading stock data")
self.stocksource.download()
for datum in self.stockdata:
self.logger.debug(str(datum))
def hash_to_symbol(self, digest):
return symbolic.hash2symbol(digest, nihon=not self.cliparams.roman, color=self.cliparams.color)
def extend_symbol_list(self, symbolist, hashlist):
symbolist.append(symbolic.separator)
symbolist.extend(list(map(self.hash_to_symbol, hashlist)))
def partitionize(self):
self.logger.debug("Building hash-chain")
self.hashlist = numeric.hashchain(self.stockdata)
#alphabet = "⬤アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワガギグゲゴザジズゼゾダデドバビブベボパピプペポ"
#print(",".join(map(lambda h: str(alphabet.index(symbolic.hash2symbol(h, nihon=not self.cliparams.roman, color=self.cliparams.color))), self.hashlist)))
for digest in self.hashlist:
self.logger.debug(digest)
self.logger.debug("Partitioning")
big_part, medium_part, small_part = numeric.partition(len(self.stockdata))
self.logger.debug("(big/medium/small): (%d / %d / %d)" % (len(big_part), len(medium_part), len(small_part)))
self.symbolist = []
self.extend_symbol_list(self.symbolist, map(lambda index: self.hashlist[index], big_part))
self.extend_symbol_list(self.symbolist, map(lambda index: self.hashlist[index], medium_part))
self.extend_symbol_list(self.symbolist, map(lambda index: self.hashlist[index], small_part))
self.logger.debug(",".join(list(map(self.hash_to_symbol, self.hashlist))))
def calculate(self):
self.load_stock_data()
if len(self.stockdata) == 0:
self.logger.critical("Could not load stock data (the initial date is %s, try setting a more recent one.)" % self.datefrom.isoformat())
exit(-1)
self.partitionize()
def gen_visual_string(self):
grid = draw_spiral(self.symbolist)
return pretty_print_grid(grid,
self.datefrom.isoformat(),
datetime.date.today().isoformat()) # [last element][first 8 characters]
def visualize(self):
print(self.gen_visual_string())
def visualize_png(self):
string_to_png(self.gen_visual_string(), self.cliparams.image_output, font=self.cliparams.font)
class objectview():
def __init__(self, d):
self.__dict__ = d
if __name__ == "__main__":
cli = parsecli()
loglevel = logging.INFO
if cli.debug:
loglevel = logging.DEBUG
elif cli.quiet:
loglevel = logging.ERROR
div = DivergenceViewer(cli, loglevel=loglevel)
div.calculate()
if cli.image_output != "no":
div.visualize_png()
else:
div.visualize()