forked from quandl/quandl-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce access to point in time API (quandl#172)
* quandl: introduce point in time api methods Update quandl-python to retrieve point in time data via API. Similar to datatables, point in time allows callers to retrieve versioned records for datatables for a given date or date interval. Signed-off-by: Jamie Couture <[email protected]> * Add documentation for point in time Update developer documentation to provide examples on how to use point in time. Signed-off-by: Jamie Couture <[email protected]> * quandl-python: 3.6.0 Signed-off-by: Jamie Couture <[email protected]> Co-authored-by: Ruan Carlos <[email protected]>
- Loading branch information
1 parent
c3efa8d
commit 2c45a24
Showing
9 changed files
with
328 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from quandl.model.point_in_time import PointInTime | ||
from quandl.errors.quandl_error import LimitExceededError | ||
from .api_config import ApiConfig | ||
from .message import Message | ||
from quandl.errors.quandl_error import InvalidRequestError | ||
import warnings | ||
import copy | ||
|
||
|
||
def get_point_in_time(datatable_code, **options): | ||
validate_pit_options(options) | ||
pit_options = {} | ||
|
||
# Remove the PIT params/keys from the options to not send it as a query params | ||
for k in ['interval', 'date', 'start_date', 'end_date']: | ||
if k in options.keys(): | ||
pit_options[k] = options.pop(k) | ||
|
||
if 'paginate' in options.keys(): | ||
paginate = options.pop('paginate') | ||
else: | ||
paginate = None | ||
|
||
data = None | ||
page_count = 0 | ||
while True: | ||
next_options = copy.deepcopy(options) | ||
next_data = PointInTime(datatable_code, pit=pit_options).data(params=next_options) | ||
|
||
if data is None: | ||
data = next_data | ||
else: | ||
data.extend(next_data) | ||
|
||
if page_count >= ApiConfig.page_limit: | ||
raise LimitExceededError( | ||
Message.WARN_DATA_LIMIT_EXCEEDED % (datatable_code, | ||
ApiConfig.api_key | ||
) | ||
) | ||
|
||
next_cursor_id = next_data.meta['next_cursor_id'] | ||
|
||
if next_cursor_id is None: | ||
break | ||
elif paginate is not True and next_cursor_id is not None: | ||
warnings.warn(Message.WARN_PAGE_LIMIT_EXCEEDED, UserWarning) | ||
break | ||
|
||
page_count = page_count + 1 | ||
options['qopts.cursor_id'] = next_cursor_id | ||
return data.to_pandas() | ||
|
||
|
||
def validate_pit_options(options): | ||
if 'interval' not in options.keys(): | ||
raise InvalidRequestError('option `interval` is required') | ||
|
||
if options['interval'] not in ['asofdate', 'from', 'between']: | ||
raise InvalidRequestError('option `interval` is invalid') | ||
|
||
if options['interval'] in ['from', 'between']: | ||
if 'start_date' not in options.keys() or 'end_date' not in options.keys(): | ||
raise InvalidRequestError('options `start_date` and `end_date` are required') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from quandl.operations.get import GetOperation | ||
from quandl.operations.list import ListOperation | ||
from .data import Data | ||
from .model_base import ModelBase | ||
from datetime import date | ||
|
||
import logging | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
class PointInTime(GetOperation, ListOperation, ModelBase): | ||
def data(self, **options): | ||
if not options: | ||
options = {'params': {}} | ||
return Data.page(self, **options) | ||
|
||
def default_path(self): | ||
return "%s/:id/%s" % (self.lookup_key(), self.pit_url(),) | ||
|
||
def lookup_key(self): | ||
return 'pit' | ||
|
||
def pit_url(self): | ||
interval = self.options['pit']['interval'] | ||
if interval in ['asofdate']: | ||
if 'date' not in self.options['pit'].keys(): | ||
date_replace = date.today() | ||
else: | ||
date_replace = self.options['pit']['date'] | ||
return "%s/%s" % (interval, date_replace, ) | ||
else: | ||
start_date = self.options['pit']['start_date'] | ||
end_date = self.options['pit']['end_date'] | ||
if interval == 'between': | ||
return "%s/%s/%s" % (interval, start_date, end_date, ) | ||
else: | ||
return "%s/%s/to/%s" % (interval, start_date, end_date, ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
VERSION = '3.5.3' | ||
VERSION = '3.6.0' |
Oops, something went wrong.