Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move get_raw_rows_name from SimpleAnalytics to FetchData , since __raw_r... #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions grs/best_buy_or_sell.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ def best_four_point_to_buy(self):
(self.best_buy_1() or self.best_buy_2() or self.best_buy_3() or \
self.best_buy_4()):
if self.best_buy_1():
result.append(self.best_buy_1.__doc__.strip().decode('utf-8'))
result.append(self.best_buy_1.__doc__.strip())
if self.best_buy_2():
result.append(self.best_buy_2.__doc__.strip().decode('utf-8'))
result.append(self.best_buy_2.__doc__.strip())
if self.best_buy_3():
result.append(self.best_buy_3.__doc__.strip().decode('utf-8'))
result.append(self.best_buy_3.__doc__.strip())
if self.best_buy_4():
result.append(self.best_buy_4.__doc__.strip().decode('utf-8'))
result.append(self.best_buy_4.__doc__.strip())
result = ', '.join(result)
else:
result = False
Expand All @@ -146,13 +146,13 @@ def best_four_point_to_sell(self):
(self.best_sell_1() or self.best_sell_2() or self.best_sell_3() or \
self.best_sell_4()):
if self.best_sell_1():
result.append(self.best_sell_1.__doc__.strip().decode('utf-8'))
result.append(self.best_sell_1.__doc__.strip())
if self.best_sell_2():
result.append(self.best_sell_2.__doc__.strip().decode('utf-8'))
result.append(self.best_sell_2.__doc__.strip())
if self.best_sell_3():
result.append(self.best_sell_3.__doc__.strip().decode('utf-8'))
result.append(self.best_sell_3.__doc__.strip())
if self.best_sell_4():
result.append(self.best_sell_4.__doc__.strip().decode('utf-8'))
result.append(self.best_sell_4.__doc__.strip())
result = ', '.join(result)
else:
result = False
Expand Down
69 changes: 36 additions & 33 deletions grs/fetch_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import logging
import random
import urllib3
import types
from .error import ConnectionError
from .error import StockNoError
from .twseno import OTCNo
from .twseno import TWSENo
from cStringIO import StringIO
from io import StringIO
from datetime import datetime
from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -79,7 +80,24 @@ def info(self):
:returns: (股票代碼, 股票名稱)
"""
return self.__info
@property
def get_raw_rows_name(self):
""" 原始檔案的欄位名稱

0. 日期
1. 成交股數
2. 成交金額
3. 開盤價
4. 最高價(續)
5. 最低價
6. 收盤價
7. 漲跌價差
8. 成交筆數

:rtype: list
"""
result = [i for i in self.__raw_rows_name]
return result
def to_list(self, csv_file):
""" 串接每日資料 舊→新

Expand All @@ -98,7 +116,7 @@ def to_list(self, csv_file):
if self._twse:
if tolist:
self.__info = (tolist[0][0].split(' ')[1],
tolist[0][0].split(' ')[2].decode('cp950'))
tolist[0][0].split(' ')[2])
self.__raw_rows_name = tolist[1]
return tuple(tolist[2:])
return tuple([])
Expand Down Expand Up @@ -164,7 +182,8 @@ def fetch_data(self, stock_no, nowdatetime):

logging.info(url)
result = GRETAI_CONNECTIONS.urlopen('GET', url)
csv_files = csv.reader(StringIO(result.data))
resultdataStr = str(result.data,'cp950')
csv_files = csv.reader(StringIO(resultdataStr))
self.__url.append(GRETAI_HOST + url)
return csv_files

Expand Down Expand Up @@ -204,7 +223,8 @@ def fetch_data(self, stock_no, nowdatetime):
'rand': random.randrange(1, 1000000)}
logging.info(url)
result = TWSE_CONNECTIONS.urlopen('GET', url)
csv_files = csv.reader(StringIO(result.data))
resultdataStr = str(result.data,'cp950')
csv_files = csv.reader(StringIO(resultdataStr))
self.__url.append(TWSE_HOST + url)
return csv_files

Expand Down Expand Up @@ -242,24 +262,7 @@ def get_raw_rows(self, rows=6):
"""
return self.__serial_price(rows)

@property
def get_raw_rows_name(self):
""" 原始檔案的欄位名稱

0. 日期
1. 成交股數
2. 成交金額
3. 開盤價
4. 最高價(續)
5. 最低價
6. 收盤價
7. 漲跌價差
8. 成交筆數

:rtype: list
"""
result = [i.decode('cp950') for i in self.__raw_rows_name]
return result


def plus_mons(self, month):
""" 新增擴充月份資料
Expand Down Expand Up @@ -474,31 +477,31 @@ def __init__(self, stock_no, mons=3, twse=False, otc=False):
pass

def __new__(cls, stock_no, mons=3, twse=False, otc=False):
assert isinstance(stock_no, basestring), '`stock_no` must be a string'
assert isinstance(stock_no, str), '`stock_no` must be a string'
assert not twse == otc == True, 'Only `twse` or `otc` to be True'

if twse and not otc:
stock_proxy = type('Stock', (TWSEFetch, SimpleAnalytics), {})()
stock_proxy = types.new_class('Stock', (TWSEFetch, SimpleAnalytics))
twse = True
elif not twse and otc:
stock_proxy = type('Stock', (OTCFetch, SimpleAnalytics), {})()
stock_proxy = types.new_class('Stock', (OTCFetch, SimpleAnalytics))
twse = False
elif stock_no in TWSENo().all_stock_no:
stock_proxy = type('Stock', (TWSEFetch, SimpleAnalytics), {})()
stock_proxy = types.new_class('Stock', (TWSEFetch, SimpleAnalytics))
twse = True
elif stock_no in OTCNo().all_stock_no:
stock_proxy = type('Stock', (OTCFetch, SimpleAnalytics), {})()
stock_proxy = types.new_class('Stock', (OTCFetch, SimpleAnalytics))
twse = False
else:
raise StockNoError()

stock_proxy.__init__()
stockproxy = stock_proxy()
try:
cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse)
stock_proxy._load_data(cls.__raw_data)
stockproxy.__raw_data = stockproxy.serial_fetch(stock_no, mons, twse)
stockproxy._load_data(stockproxy.__raw_data)
except urllib3.exceptions.HTTPError:
raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.'
raise ConnectionError()('IN OFFLINE, NO DATA FETCH.')
except Exception as e:
print e
print(e)

return stock_proxy
return stockproxy
8 changes: 4 additions & 4 deletions grs/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class RealtimeStock(object):
:rtype: dict
"""
def __init__(self, no):
assert isinstance(no, basestring), '`no` must be a string'
assert isinstance(no, str), '`no` must be a string'
self.__raw = ''
try:
page = TSE_CONNECTIONS.urlopen('GET', '/data/%s.csv?r=%s' % (no,
random.randrange(1, 10000))).data
except urllib3.exceptions.HTTPError:
raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.'
raise ConnectionError()('IN OFFLINE, NO DATA FETCH.')

logging.info('twsk no %s', no)
reader = csv.reader(page.split('\r\n'))
Expand Down Expand Up @@ -99,7 +99,7 @@ def real(self):
try:
unch = sum([covstr(self.__raw[3]), covstr(self.__raw[4])]) / 2
result = {
'name': unicode(self.__raw[36].replace(' ', ''), 'cp950'),
'name': str(self.__raw[36].replace(' ', ''), 'cp950'),
'no': self.__raw[0],
'range': self.__raw[1], # 漲跌價
'time': self.__raw[2], # 取得時間
Expand Down Expand Up @@ -162,7 +162,7 @@ def __init__(self):
page = TSE_CONNECTIONS.urlopen('GET',
'/data/TSEIndex.csv?r=%s' % random.randrange(1, 10000)).data
except urllib3.exceptions.HTTPError:
raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.'
raise ConnectionError()('IN OFFLINE, NO DATA FETCH.')

reader = csv.reader(page.split('\r\n'))
for i in reader:
Expand Down
4 changes: 2 additions & 2 deletions grs/realtime2.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def make_format(raw):
best_ask_volume = [int(v) for v in i['f'].split('_')[:-1]]
best_bid_volume = [int(v) for v in i['g'].split('_')[:-1]]

data[i['c']]['best_ask_list'] = zip(best_ask_price, best_ask_volume)
data[i['c']]['best_bid_list'] = zip(best_bid_price, best_bid_volume)
data[i['c']]['best_ask_list'] = list(zip(best_ask_price, best_ask_volume))
data[i['c']]['best_bid_list'] = list(zip(best_bid_price, best_bid_volume))
data[i['c']]['best_ask_price'] = best_ask_price[0]
data[i['c']]['best_ask_volume'] = best_ask_volume[0]
data[i['c']]['best_bid_price'] = best_bid_price[0]
Expand Down
16 changes: 8 additions & 8 deletions grs/twseno.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def importcsv(self):
result = {}
for i in csv_data:
try:
result[i[0]] = str(i[1]).decode('utf-8')
result[i[0]] = str(i[1])
except ValueError:
if i[0] == 'UPDATE':
self.last_update = str(i[1]).decode('utf-8')
self.last_update = str(i[1])
else:
pass
return result
Expand All @@ -60,7 +60,7 @@ def __industry_code(self):
csv_data = csv.reader(csv_file)
result = {}
for i in csv_data:
result[i[0]] = i[1].decode('utf-8')
result[i[0]] = i[1]
return result

def __loadindcomps(self):
Expand All @@ -73,10 +73,10 @@ def __loadindcomps(self):
for i in csv_data:
if check_words.match(i[2]):
try:
result[i[2]].append(i[0].decode('utf-8'))
result[i[2]].append(i[0])
except (ValueError, KeyError):
try:
result[i[2]] = [i[0].decode('utf-8')]
result[i[2]] = [i[0]]
except KeyError:
pass
return result
Expand Down Expand Up @@ -125,15 +125,15 @@ def all_stock_no(self):

:rtype: list
"""
return self.__allstockno.keys()
return list(self.__allstockno.keys())

@property
def all_stock_name(self):
""" 回傳股票名稱

:rtype: list
"""
return self.__allstockno.values()
return list(self.__allstockno.values())

@property
def industry_code(self):
Expand Down Expand Up @@ -200,4 +200,4 @@ def __init__(self):
t = TWSENo()
#t = OTCNo()
t_list = t.get_stock_list()
print t_list
print(t_list)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
license=grs.__license__,
keywords="Taiwan Stock Exchange taipei twse otc gretai " + \
"台灣 台北 股市 即時 上市 上櫃",
install_requires=['python-dateutil==1.5', 'ujson', 'urllib3'],
install_requires=['python-dateutil==2.2', 'ujson', 'urllib3'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
Expand Down
10 changes: 5 additions & 5 deletions test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_stock_value(self):
def test_twse_no():
twse_no = grs.TWSENo()
assert isinstance(twse_no.all_stock, dict)
result = twse_no.search(u'中')
result = twse_no.search('中')
# 1701 中化
assert '1701' in result
result = twse_no.searchbyno(17)
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_realtime():
real_time = grs.RealtimeStock('0050')
assert real_time.real['no'] == '0050'
try:
real_time = grs.RealtimeStock(0050)
real_time = grs.RealtimeStock(0o050)
except AssertionError:
pass

Expand All @@ -107,9 +107,9 @@ def test_countdown():
@staticmethod
def test_taiwan_50():
stock = grs.Stock('0050')
assert u'台灣50' == stock.info[1]
assert '台灣50' == stock.info[1]
try:
stock = grs.Stock(0050)
stock = grs.Stock(0o050)
except AssertionError:
pass

Expand Down Expand Up @@ -162,7 +162,7 @@ def test_stock_value(self):
def test_otc_no():
otc_no = grs.OTCNo()
assert isinstance(otc_no.all_stock, dict)
result = otc_no.search(u'華')
result = otc_no.search('華')
# 8446 華研
assert '8446' in result
result = otc_no.searchbyno(46)
Expand Down
Loading