-
Notifications
You must be signed in to change notification settings - Fork 2
/
yahooStock.py
executable file
·88 lines (68 loc) · 2.45 KB
/
yahooStock.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from YahooTWStock import YahooTWStock
import time
import os
import sys
# from terminaltables import AsciiTable
from terminaltables import SingleTable
class StockTable():
def __init__(self, parent=None):
self.load()
pass
def load(self):
self.yahoo = YahooTWStock()
self.yahoo.loadFromStockCsv('stock.csv')
# stockIdList = ['2330', '2317', '2002', '1301', '2412', '2891', '0050', '0051', '0056', '00646']
# self.yahoo.addStockIdList(stockIdList)
self.initDataAndTable()
def initDataAndTable(self):
self.data = []
self.data.append(['股票代號', '股票名稱', '股價'])
for i in range(self.yahoo.size):
self.data.append([str(self.yahoo.id(i)), 'none', '0'])
# self.table = AsciiTable(self.data)
self.table = SingleTable(self.data)
def updateTable(self, i):
self.table.table_data[i + 1][0] = str(self.data[i + 1][0])
self.table.table_data[i + 1][1] = str(self.data[i + 1][1])
self.table.table_data[i + 1][2] = str(self.data[i + 1][2])
pass
def updateData(self, i, id, name, price):
self.data[i + 1][0] = id
self.data[i + 1][1] = name
self.data[i + 1][2] = price
def updateStock(self, i, id, name, price):
# print('updateStock %d %s %s %f' % (i, id, name, price))
self.updateData(i, id, name, price)
self.updateTable(i)
def refresh(self):
self.run()
def run(self):
for i in range(self.yahoo.size):
self.yahoo.refresh(i)
# print('%6s | %s | %.2f' % (yahoo.id(i), yahoo[i].name, yahoo[i].price))
self.updateStock(i, self.yahoo.id(i), self.yahoo.name(i), self.yahoo.price(i))
def showTable(self):
print(self.table.table)
def clearScreen():
# print('\n' * 80) # prints 80 line breaks, Faking Clear Screen (for PyCharm)
if os.name == 'nt':
os.system('cls') # on windows
else:
os.system('clear') # on linux / os x
if __name__ == '__main__':
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding('utf8')
stockTable = StockTable()
clearScreen()
stockTable.showTable()
while (1):
print('refresh...')
stockTable.refresh()
clearScreen()
stockTable.showTable()
for i in range(5):
print('wait %d sec to refresh...' % (5-i))
time.sleep(1)