support py3 and minimally test

This commit is contained in:
Andy Hayden 2015-04-16 13:59:26 -07:00
parent 997f1eadf0
commit 960658e9c5
5 changed files with 60 additions and 7 deletions

14
.travis.yml Normal file
View File

@ -0,0 +1,14 @@
language: python
python:
- "2.6"
- "2.7"
- "pypy"
- "3.3"
- "3.4"
install:
- python setup.py install
script:
- nosetests

View File

@ -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")

View File

@ -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',
],
)

View File

@ -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")

8
tox.ini Normal file
View File

@ -0,0 +1,8 @@
[tox]
envlist=py{26,27,33,34}
[testenv]
commands=
nosetests
deps=
nose