pep8 corrections and changed all object names

This commit is contained in:
Bharat Kalluri
2017-10-09 09:00:37 +05:30
parent e566177e4e
commit 25a05d4aa4
2 changed files with 26 additions and 15 deletions

View File

@@ -79,6 +79,10 @@ class TestTickerPrices(TestCase):
assert len(tickers) > 1 assert len(tickers) > 1
assert all(ticker['assetType'] == 'Stock' for ticker in tickers) assert all(ticker['assetType'] == 'Stock' for ticker in tickers)
def test_ticker_metadata_for_object(self):
data = self._client.get_ticker_metadata("GOOGL", fmt='object')
assert len(data.name) > 1
# tiingo/news # tiingo/news
class TestNews(TestCase): class TestNews(TestCase):

View File

@@ -81,7 +81,7 @@ class TiingoClient(RestClient):
return [row for row in reader return [row for row in reader
if row.get('assetType') == 'Stock'] if row.get('assetType') == 'Stock']
def get_ticker_metadata(self, ticker,fmt='json'): def get_ticker_metadata(self, ticker, fmt='json'):
"""Return metadata for 1 ticker """Return metadata for 1 ticker
Use TiingoClient.list_tickers() to get available options Use TiingoClient.list_tickers() to get available options
@@ -90,11 +90,13 @@ class TiingoClient(RestClient):
""" """
url = "tiingo/daily/{}".format(ticker) url = "tiingo/daily/{}".format(ticker)
response = self._request('GET', url) response = self._request('GET', url)
if fmt=='json': data = response.json()
return response.json() if fmt == 'json':
elif fmt=='object': return data
elif fmt == 'object':
# inspired by https://stackoverflow.com/a/15882054 # inspired by https://stackoverflow.com/a/15882054
return json.loads(json.dumps(response.json()), object_hook=lambda d: namedtuple('X', d.keys())(*d.values())) return json.loads(json.dumps(data),
object_hook=lambda d: namedtuple('Ticker', d.keys())(*d.values()))
def get_ticker_price(self, ticker, def get_ticker_price(self, ticker,
startDate=None, endDate=None, startDate=None, endDate=None,
@@ -134,7 +136,8 @@ class TiingoClient(RestClient):
# NEWS FEEDS # NEWS FEEDS
# tiingo/news # tiingo/news
def get_news(self, tickers=[], tags=[], sources=[], startDate=None, def get_news(self, tickers=[], tags=[], sources=[], startDate=None,
endDate=None, limit=100, offset=0, sortBy="publishedDate",fmt='json'): endDate=None, limit=100, offset=0, sortBy="publishedDate",
fmt='json'):
"""Return list of news articles matching given search terms """Return list of news articles matching given search terms
https://api.tiingo.com/docs/tiingo/news https://api.tiingo.com/docs/tiingo/news
@@ -161,12 +164,14 @@ class TiingoClient(RestClient):
'endDate': endDate 'endDate': endDate
} }
response = self._request('GET', url, params=params) response = self._request('GET', url, params=params)
if fmt=='json': data = response.json()
return response.json() if fmt == 'json':
elif fmt=='object': return data
return json.loads(json.dumps(response.json()), object_hook=lambda d: namedtuple('X', d.keys())(*d.values())) elif fmt == 'object':
return json.loads(json.dumps(data),
object_hook=lambda d: namedtuple('NewsArticle', d.keys())(*d.values()))
def get_bulk_news(self, file_id=None,fmt='json'): def get_bulk_news(self, file_id=None, fmt='json'):
"""Only available to institutional clients. """Only available to institutional clients.
If ID is NOT provided, return array of available file_ids. If ID is NOT provided, return array of available file_ids.
If ID is provided, provides URL which you can use to download your If ID is provided, provides URL which you can use to download your
@@ -178,7 +183,9 @@ class TiingoClient(RestClient):
url = "tiingo/news/bulk_download" url = "tiingo/news/bulk_download"
response = self._request('GET', url) response = self._request('GET', url)
if fmt=='json': data = response.json()
return response.json() if fmt == 'json':
elif fmt=='object': return data
return json.loads(json.dumps(response.json()), object_hook=lambda d: namedtuple('X', d.keys())(*d.values())) elif fmt == 'object':
return json.loads(json.dumps(data),
object_hook=lambda d: namedtuple('BulkDownload', d.keys())(*d.values()))