Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix microoptimizations #1568

Merged
merged 3 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion zipline/assets/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ def retrieve_asset(self, sid, default_none=False):
Retrieve the Asset for a given sid.
"""
try:
return self._asset_cache[sid]
asset = self._asset_cache[sid]
if asset is None and not default_none:
raise SidsNotFound(sids=[sid])
return asset
except KeyError:
return self.retrieve_all((sid,), default_none=default_none)[0]

Expand Down
32 changes: 19 additions & 13 deletions zipline/data/data_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def get_spot_value(self, asset, field, dt, data_frequency):
return None

if data_frequency == "daily":
return self._get_daily_data(asset, field, session_label)
return self._get_daily_spot_value(asset, field, session_label)
else:
if field == "last_traded":
return self.get_last_traded_dt(asset, dt, 'minute')
Expand Down Expand Up @@ -629,7 +629,7 @@ def _get_minute_spot_value(self, asset, column, dt, ffill=False):
dt, "minute", spot_value=result
)

def _get_daily_data(self, asset, column, dt):
def _get_daily_spot_value(self, asset, column, dt):
reader = self._get_pricing_reader('daily')
if column == "last_traded":
last_traded_dt = reader.get_last_traded_dt(asset, dt)
Expand Down Expand Up @@ -703,24 +703,27 @@ def _get_history_daily_window(self, assets, end_dt, bar_count,
columns=assets
)

def _get_history_daily_window_data(
self, assets, days_for_window, end_dt, field_to_use):
ends_at_midnight = end_dt.hour == 0 and end_dt.minute == 0
def _get_history_daily_window_data(self,
assets,
days_for_window,
end_dt,
field_to_use):
ends_at_midnight = (0 == end_dt.hour == end_dt.minute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: could we move 0 to the far rhs?


if ends_at_midnight:
# two cases where we use daily data for the whole range:
# 1) the history window ends at midnight utc.
# 2) the last desired day of the window is after the
# last trading day, use daily data for the whole range.
return self._get_daily_window_for_sids(
return self._get_daily_window_data(
assets,
field_to_use,
days_for_window,
extra_slot=False
)
else:
# minute mode, requesting '1d'
daily_data = self._get_daily_window_for_sids(
daily_data = self._get_daily_window_data(
assets,
field_to_use,
days_for_window[0:-1]
Expand Down Expand Up @@ -788,7 +791,7 @@ def _get_history_minute_window(self, assets, end_dt, bar_count,
if minutes_for_window[0] < self._first_trading_minute:
self._handle_minute_history_out_of_bounds(bar_count)

asset_minute_data = self._get_minute_window_for_assets(
asset_minute_data = self._get_minute_window_data(
assets,
field_to_use,
minutes_for_window,
Expand Down Expand Up @@ -899,7 +902,7 @@ def get_history_window(self, assets, end_dt, bar_count, frequency, field,
df.loc[normed_index > asset.end_date, asset] = nan
return df

def _get_minute_window_for_assets(self, assets, field, minutes_for_window):
def _get_minute_window_data(self, assets, field, minutes_for_window):
"""
Internal method that gets a window of adjusted minute data for an asset
and specified date range. Used to support the history API method for
Expand All @@ -909,8 +912,8 @@ def _get_minute_window_for_assets(self, assets, field, minutes_for_window):

Parameters
----------
asset : Asset
The asset whose data is desired.
assets : iterable[Asset]
The assets whose data is desired.

field: string
The specific field to return. "open", "high", "close_price", etc.
Expand All @@ -928,8 +931,11 @@ def _get_minute_window_for_assets(self, assets, field, minutes_for_window):
field,
False)

def _get_daily_window_for_sids(
self, assets, field, days_in_window, extra_slot=True):
def _get_daily_window_data(self,
assets,
field,
days_in_window,
extra_slot=True):
"""
Internal method that gets a window of adjusted daily data for a sid
and specified date range. Used to support the history API method for
Expand Down
3 changes: 3 additions & 0 deletions zipline/testing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ def get_spot_value(self, asset, field, dt, data_frequency):
# otherwise just return a fixed value
return int(asset)

# XXX: These aren't actually the methods that are used by the superclasses,
# so these don't do anything, and this class will likely produce unexpected
# results for history().
def _get_daily_window_for_sid(self, asset, field, days_in_window,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just remove this method if it isn't used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured the original intent here would be more helpful for whoever eventually gets bitten by this. I just happened to notice that these aren't used because I was looking at renaming methods in data portal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk

extra_slot=True):
return np.arange(days_in_window, dtype=np.float64)
Expand Down