From 960658e9c5a8fc2ee58e7427adfb3280933743fe Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Thu, 16 Apr 2015 13:59:26 -0700 Subject: [PATCH] support py3 and minimally test --- .travis.yml | 14 ++++++++++++++ googlefinance/__init__.py | 16 ++++++++++------ setup.py | 12 +++++++++++- tests/test_googlefinance.py | 17 +++++++++++++++++ tox.ini | 8 ++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 .travis.yml create mode 100644 tests/test_googlefinance.py create mode 100644 tox.ini diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..77919ba --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: python + +python: + - "2.6" + - "2.7" + - "pypy" + - "3.3" + - "3.4" + +install: + - python setup.py install + +script: + - nosetests diff --git a/googlefinance/__init__.py b/googlefinance/__init__.py index 17e0f17..0a3d70c 100644 --- a/googlefinance/__init__.py +++ b/googlefinance/__init__.py @@ -1,7 +1,11 @@ -from urllib2 import Request, urlopen import json import sys +try: + from urllib.request import Request, urlopen +except ImportError: # python 2 + from urllib2 import Request, urlopen + __author__ = 'Hongtao Cai' googleFinanceKeyToFullName = { @@ -48,16 +52,16 @@ def getQuotes(symbols): get real-time quotes (index, last trade price, last trade time, etc) for stocks, using google api: http://finance.google.com/finance/info?client=ig&q=symbols Unlike python package 'yahoo-finance' (15 min delay), There is no delay for NYSE and NASDAQ stocks in 'googlefinance' package. - + example: quotes = getQuotes('AAPL') return: [{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'129.09', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'129.09', u'Yield': u'1.46', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'0.47', u'StockSymbol': u'AAPL', u'ID': u'22144'}] - + quotes = getQuotes(['AAPL', 'GOOG']) return: [{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'129.09', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'129.09', u'Yield': u'1.46', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'0.47', u'StockSymbol': u'AAPL', u'ID': u'22144'}, {u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'571.34', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'571.34', u'Yield': u'', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'', u'StockSymbol': u'GOOG', u'ID': u'304466804484872'}] - + :param symbols: a single symbol or a list of stock symbols :return: real-time quotes list ''' @@ -75,6 +79,6 @@ if __name__ == '__main__': symbols = symbols.split(',') try: - print json.dumps(getQuotes(symbols), indent=2) + print(json.dumps(getQuotes(symbols), indent=2)) except: - print "Fail" + print("Fail") diff --git a/setup.py b/setup.py index 43c3200..7e43d99 100644 --- a/setup.py +++ b/setup.py @@ -9,5 +9,15 @@ setup( url = 'https://github.com/hongtaocai/googlefinance', # use the URL to the github repo download_url = 'https://github.com/hongtaocai/googlefinance/tarball/0.4', # I'll explain this in a second keywords = ['googlefinance', 'stock', 'price' , 'finance', 'google', 'real-time'], # arbitrary keywords - classifiers = [], + classifiers = [ + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + ], ) diff --git a/tests/test_googlefinance.py b/tests/test_googlefinance.py new file mode 100644 index 0000000..3dabe9c --- /dev/null +++ b/tests/test_googlefinance.py @@ -0,0 +1,17 @@ +import googlefinance +import unittest + +class TestQuotes(unittest.TestCase): + + def test_one_symbol(self): + appl = googlefinance.getQuotes('GOOG')[0] + self.assertEqual(appl["Index"], "NASDAQ") + self.assertEqual(appl["StockSymbol"], "GOOG") + + def test_symbols(self): + quotes = googlefinance.getQuotes(['GOOG', 'VIE:BKS']) + self.assertEqual(quotes[0]["Index"], "NASDAQ") + self.assertEqual(quotes[0]["StockSymbol"], "GOOG") + self.assertEqual(quotes[1]["Index"], "VIE") + self.assertEqual(quotes[1]["StockSymbol"], "BKS") + diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..45e4f3e --- /dev/null +++ b/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist=py{26,27,33,34} + +[testenv] +commands= + nosetests +deps= + nose