From b228a1e21c6bb804368a423422e2a6cc0c9fd425 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sat, 12 Apr 2014 11:36:39 +0800 Subject: [PATCH 01/11] Add on offline Exception. --- grs/fetch_data.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/grs/fetch_data.py b/grs/fetch_data.py index b6b59cd..caf3206 100644 --- a/grs/fetch_data.py +++ b/grs/fetch_data.py @@ -34,6 +34,10 @@ class StockNoError(Exception): """ Exception for stock_no not in TWSE or OTC list. """ pass +class OfflineConnection(Exception): + """ Exception for no connection. """ + pass + class FetchData(object): ''' FetchData ''' @@ -459,7 +463,10 @@ def __new__(cls, stock_no, mons=3, twse=False, otc=False): raise StockNoError stock_proxy.__init__() - cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse) - stock_proxy._load_data(cls.__raw_data) + try: + cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse) + stock_proxy._load_data(cls.__raw_data) + except urllib2.URLError: + raise OfflineConnection, u'IN OFFLINE, NO DATA FETCH.' return stock_proxy From 55dbb731eee04732caffabe93b7d917880fdcc9e Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sat, 12 Apr 2014 14:23:03 +0800 Subject: [PATCH 02/11] Raise class. --- grs/fetch_data.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/grs/fetch_data.py b/grs/fetch_data.py index caf3206..2c97f69 100644 --- a/grs/fetch_data.py +++ b/grs/fetch_data.py @@ -29,14 +29,16 @@ from datetime import datetime from dateutil.relativedelta import relativedelta +class Error(Exception): + ''' Exception base ''' -class StockNoError(Exception): + +class StockNoError(Error): """ Exception for stock_no not in TWSE or OTC list. """ - pass -class OfflineConnection(Exception): + +class OfflineConnection(Error): """ Exception for no connection. """ - pass class FetchData(object): @@ -460,13 +462,13 @@ def __new__(cls, stock_no, mons=3, twse=False, otc=False): stock_proxy = type('Stock', (OTCFetch, SimpleAnalytics), {})() twse = False else: - raise StockNoError + raise StockNoError() stock_proxy.__init__() try: cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse) stock_proxy._load_data(cls.__raw_data) except urllib2.URLError: - raise OfflineConnection, u'IN OFFLINE, NO DATA FETCH.' + raise OfflineConnection(), u'IN OFFLINE, NO DATA FETCH.' return stock_proxy From 388352e0324347f0852543031ed03fe47efe1422 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sat, 12 Apr 2014 14:29:09 +0800 Subject: [PATCH 03/11] Add error.py --- grs/error.py | 34 ++++++++++++++++++++++++++++++++++ grs/fetch_data.py | 13 ++----------- 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 grs/error.py diff --git a/grs/error.py b/grs/error.py new file mode 100644 index 0000000..d5abc32 --- /dev/null +++ b/grs/error.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +'''' grs Exception ''' +# Copyright (c) 2012, 2013, 2014 Toomore Chiang, http://toomore.net/ +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + + +class Error(Exception): + ''' Exception base ''' + + +class StockNoError(Error): + """ Exception for stock_no not in TWSE or OTC list. """ + + +class OfflineConnection(Error): + """ Exception for no connection. """ + diff --git a/grs/fetch_data.py b/grs/fetch_data.py index 2c97f69..b63b1ab 100644 --- a/grs/fetch_data.py +++ b/grs/fetch_data.py @@ -24,22 +24,13 @@ import logging import random import urllib2 +from .error import OfflineConnection +from .error import StockNoError from .twseno import OTCNo from .twseno import TWSENo from datetime import datetime from dateutil.relativedelta import relativedelta -class Error(Exception): - ''' Exception base ''' - - -class StockNoError(Error): - """ Exception for stock_no not in TWSE or OTC list. """ - - -class OfflineConnection(Error): - """ Exception for no connection. """ - class FetchData(object): ''' FetchData ''' From f9d83322257c861d702740f0177e3658cbfa88e2 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sat, 12 Apr 2014 14:31:25 +0800 Subject: [PATCH 04/11] Rename. --- grs/error.py | 2 +- grs/fetch_data.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/grs/error.py b/grs/error.py index d5abc32..200b399 100644 --- a/grs/error.py +++ b/grs/error.py @@ -29,6 +29,6 @@ class StockNoError(Error): """ Exception for stock_no not in TWSE or OTC list. """ -class OfflineConnection(Error): +class ConnectionError(Error): """ Exception for no connection. """ diff --git a/grs/fetch_data.py b/grs/fetch_data.py index b63b1ab..bbdaf73 100644 --- a/grs/fetch_data.py +++ b/grs/fetch_data.py @@ -24,7 +24,7 @@ import logging import random import urllib2 -from .error import OfflineConnection +from .error import ConnectionError from .error import StockNoError from .twseno import OTCNo from .twseno import TWSENo @@ -460,6 +460,6 @@ def __new__(cls, stock_no, mons=3, twse=False, otc=False): cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse) stock_proxy._load_data(cls.__raw_data) except urllib2.URLError: - raise OfflineConnection(), u'IN OFFLINE, NO DATA FETCH.' + raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.' return stock_proxy From 78f680117efeeb14dfc3886fddcd75d8439d4c7c Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sat, 12 Apr 2014 14:38:56 +0800 Subject: [PATCH 05/11] Update version. --- README.rst | 7 ++++++- docs/source/index.rst | 5 ++++- grs/__init__.py | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index db32b4d..c73abce 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ grs 台灣上市上櫃股票價格擷取 ----------------------------- :Authors: Toomore Chiang -:Version: 0.5.2 of 2014/04/12 +:Version: 0.5.3 of 2014/04/12 :Python Version: Python 2.7, PyPy :Docs: http://grs-docs.toomore.net/ @@ -302,6 +302,11 @@ Quick Start Change Logs ----------------------------- +0.5.3 2014/04/12 +==================================== + +- 修正:... + 0.5.2 2014/04/12 ==================================== diff --git a/docs/source/index.rst b/docs/source/index.rst index 49a9608..b34a7c9 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -35,7 +35,7 @@ grs 台灣上市上櫃股票價格擷取 ----------------------------- :Authors: Toomore Chiang -:Version: 0.5.2 of 2014/04/12 +:Version: 0.5.3 of 2014/04/12 :Python Version: Python 2.7, PyPy :Docs: http://grs-docs.toomore.net/ @@ -80,6 +80,9 @@ Feature Change Logs ----------------------------- +* 0.5.3 2014/04/12 + - 修正:... + * 0.5.2 2014/04/12 - 修正:字串判斷使用 `basestring`. diff --git a/grs/__init__.py b/grs/__init__.py index f820496..d63f605 100644 --- a/grs/__init__.py +++ b/grs/__init__.py @@ -21,7 +21,7 @@ # THE SOFTWARE. __title__ = 'grs' -__version__ = '0.5.2' +__version__ = '0.5.3' __author__ = 'Toomore Chiang' __license__ = 'MIT' __copyright__ = 'Copyright (C) 2012, 2013, 2014 Toomore Chiang' From e0033f2b2a9668b565322f77f3267a35a8acc98f Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sat, 12 Apr 2014 15:04:13 +0800 Subject: [PATCH 06/11] Add error docs page. --- docs/source/error.rst | 8 ++++++++ grs/error.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 docs/source/error.rst diff --git a/docs/source/error.rst b/docs/source/error.rst new file mode 100644 index 0000000..723b5ee --- /dev/null +++ b/docs/source/error.rst @@ -0,0 +1,8 @@ +grs Errors and Exceptions +========================== + + +Errors and Exceptions +--------------------------- +.. automodule:: grs.error + :members: diff --git a/grs/error.py b/grs/error.py index 200b399..97325df 100644 --- a/grs/error.py +++ b/grs/error.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -'''' grs Exception ''' +''' grs Exception ''' # Copyright (c) 2012, 2013, 2014 Toomore Chiang, http://toomore.net/ # # Permission is hereby granted, free of charge, to any person obtaining a copy From 8e745aef27588a733f0b383768169ce3c55ddc45 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sun, 13 Apr 2014 19:19:31 +0800 Subject: [PATCH 07/11] Add `ConnectionError` into realtime. --- grs/realtime.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/grs/realtime.py b/grs/realtime.py index d197afe..3f37fff 100644 --- a/grs/realtime.py +++ b/grs/realtime.py @@ -25,6 +25,7 @@ import logging import random import urllib2 +from .error import ConnectionError def covstr(strings): @@ -46,10 +47,14 @@ class RealtimeStock(object): def __init__(self, no): assert isinstance(no, basestring), '`no` must be a string' self.__raw = '' - page = urllib2.urlopen( - 'http://mis.tse.com.tw/data/{0}.csv?r={1}'.format( + try: + page = urllib2.urlopen( + 'http://mis.tse.com.tw/data/{0}.csv?r={1}'.format( no, random.randrange(1, 10000)) - ) + ) + except urllib2.URLError: + raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.' + logging.info('twsk no %s', no) reader = csv.reader(page) for i in reader: @@ -154,9 +159,13 @@ def __init__(self): 代碼可以參考:http://goristock.appspot.com/API#apiweight """ self.__raw = {} - page = urllib2.urlopen( - 'http://mis.tse.com.tw/data/TSEIndex.csv?r=%s'.format( + try: + page = urllib2.urlopen( + 'http://mis.tse.com.tw/data/TSEIndex.csv?r=%s'.format( random.randrange(1, 10000))) + except urllib2.URLError: + raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.' + reader = csv.reader(page) for i in reader: if len(i): From 46ef905d6cf13e32c8e14618fac1da1f5ffe785e Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sun, 13 Apr 2014 19:56:43 +0800 Subject: [PATCH 08/11] Fixed urllib2 str format. --- grs/realtime.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/grs/realtime.py b/grs/realtime.py index d197afe..22415a2 100644 --- a/grs/realtime.py +++ b/grs/realtime.py @@ -47,9 +47,8 @@ def __init__(self, no): assert isinstance(no, basestring), '`no` must be a string' self.__raw = '' page = urllib2.urlopen( - 'http://mis.tse.com.tw/data/{0}.csv?r={1}'.format( - no, random.randrange(1, 10000)) - ) + 'http://mis.tse.com.tw/data/%s.csv?r=%s' % (no, + random.randrange(1, 10000))) logging.info('twsk no %s', no) reader = csv.reader(page) for i in reader: @@ -155,8 +154,8 @@ def __init__(self): """ self.__raw = {} page = urllib2.urlopen( - 'http://mis.tse.com.tw/data/TSEIndex.csv?r=%s'.format( - random.randrange(1, 10000))) + 'http://mis.tse.com.tw/data/TSEIndex.csv?r=%s' % random.randrange( + 1, 10000)) reader = csv.reader(page) for i in reader: if len(i): From cbb3b302b6cd7d24246c71c703623d4b950d0a46 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sun, 13 Apr 2014 20:04:25 +0800 Subject: [PATCH 09/11] Fixed str format style. --- grs/realtime.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/grs/realtime.py b/grs/realtime.py index 22415a2..926c8bf 100644 --- a/grs/realtime.py +++ b/grs/realtime.py @@ -98,8 +98,8 @@ def real(self): 'time': self.__raw[2], # 取得時間 'max': self.__raw[3], # 漲停價 'min': self.__raw[4], # 跌停價 - 'unch': '{:.2f}'.format(unch), # 昨日收盤價 - 'pp': '{:.2f}'.format((covstr(self.__raw[8]) - unch) / unch * 100), + 'unch': '%.2f' % unch, # 昨日收盤價 + 'pp': '%.2f' % ((covstr(self.__raw[8]) - unch) / unch * 100), # 漲跌幅 % 'o': self.__raw[5], # 開盤價 'h': self.__raw[6], # 當日最高價 @@ -130,10 +130,8 @@ def real(self): result['crosspic'] = ("http://chart.apis.google.com/chart?" + "chf=bg,s,ffffff&chs=20x50&cht=ls" + - "&chd=t1:0,0,0|0,{},0|0,{},0|0,{},0|0,{},0" + - "&chds={},{}&chm=F,,1,1:4,20").format( - result['h'], result['c'], result['o'], result['l'], result['l'], - result['h']) + "&chd=t1:0,0,0|0,%(h)s,0|0,%(c)s,0|0,%(o)s,0|0,%(l)s,0" + + "&chds=%(l)s,%(h)s&chm=F,,1,1:4,20") % result result['top5buy'].sort() result['top5sell'].sort() From f3aa57c43f7206442611b1ddc98e5b808ade8202 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Thu, 17 Apr 2014 08:04:06 +0800 Subject: [PATCH 10/11] Update docs. --- README.rst | 7 ++++--- docs/source/index.rst | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index c73abce..2371043 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ grs 台灣上市上櫃股票價格擷取 ----------------------------- :Authors: Toomore Chiang -:Version: 0.5.3 of 2014/04/12 +:Version: 0.5.3 of 2014/04/17 :Python Version: Python 2.7, PyPy :Docs: http://grs-docs.toomore.net/ @@ -302,10 +302,11 @@ Quick Start Change Logs ----------------------------- -0.5.3 2014/04/12 +0.5.3 2014/04/17 ==================================== -- 修正:... +- 修正:離線時的錯誤訊息 +- 修正:`realtime` str format. 0.5.2 2014/04/12 ==================================== diff --git a/docs/source/index.rst b/docs/source/index.rst index b34a7c9..9ab53ba 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -35,7 +35,7 @@ grs 台灣上市上櫃股票價格擷取 ----------------------------- :Authors: Toomore Chiang -:Version: 0.5.3 of 2014/04/12 +:Version: 0.5.3 of 2014/04/17 :Python Version: Python 2.7, PyPy :Docs: http://grs-docs.toomore.net/ @@ -80,8 +80,9 @@ Feature Change Logs ----------------------------- -* 0.5.3 2014/04/12 - - 修正:... +* 0.5.3 2014/04/17 + - 修正:離線時的錯誤訊息 + - 修正:`realtime` str format. * 0.5.2 2014/04/12 - 修正:字串判斷使用 `basestring`. From 7b21b9ca51ce9e3b1a166c1120ab5d8806eae33e Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Thu, 17 Apr 2014 08:26:10 +0800 Subject: [PATCH 11/11] Add error link into docs index. --- docs/source/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/index.rst b/docs/source/index.rst index 9ab53ba..63ab3d7 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -75,6 +75,7 @@ Feature 時間、倒數處理 grs.TWTime/Countdown 盤中即時資訊擷取 grs.RealtimeStock/RealtimeWeight 四大買賣點判斷 grs.BestFourPoint + 其他錯誤訊息處理 grs.error Change Logs