mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-04 13:03:26 +08:00
DEP: Removes unnecessary identifier_cache from asset_finder
The identifier cache's usage was nearly identical to using lookup_generic, so this commit removes identifier-keyed caching and modifies anything that uses it.
This commit is contained in:
@@ -22,6 +22,7 @@ from unittest import TestCase
|
||||
|
||||
from datetime import (
|
||||
timedelta,
|
||||
datetime
|
||||
)
|
||||
import pickle
|
||||
import pprint
|
||||
@@ -542,9 +543,12 @@ class AssetFinderTestCase(TestCase):
|
||||
# Build a finder that is allowed to assign sids
|
||||
finder = AssetFinder(metadata=metadata, allow_sid_assignment=True)
|
||||
|
||||
# Verify that Assets were built
|
||||
play = finder.retrieve_asset_by_identifier('PLAY')
|
||||
# Verify that Assets were built and different sids were assigned
|
||||
play = finder.lookup_symbol('PLAY', datetime.now())
|
||||
msft = finder.lookup_symbol('MSFT', datetime.now())
|
||||
self.assertEqual('PLAY', play.symbol)
|
||||
self.assertIsNotNone(play.sid)
|
||||
self.assertNotEqual(play.sid, msft.sid)
|
||||
|
||||
def test_sid_assignment_failure(self):
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ from six import with_metaclass, string_types
|
||||
|
||||
from zipline.errors import (
|
||||
ConsumeAssetMetaDataError,
|
||||
IdentifierNotFound,
|
||||
InvalidAssetType,
|
||||
MultipleSymbolsFound,
|
||||
SidAssignmentError,
|
||||
@@ -68,7 +67,6 @@ class AssetFinder(object):
|
||||
self.cache = {}
|
||||
self.sym_cache = {}
|
||||
self.future_chains_cache = {}
|
||||
self.identifier_cache = {}
|
||||
self.fuzzy_match = {}
|
||||
|
||||
# This flag controls if the AssetFinder is allowed to generate its own
|
||||
@@ -108,15 +106,6 @@ class AssetFinder(object):
|
||||
else:
|
||||
raise SidNotFound(sid=sid)
|
||||
|
||||
def retrieve_asset_by_identifier(self, identifier):
|
||||
if isinstance(identifier, Asset):
|
||||
return identifier
|
||||
asset = self.identifier_cache.get(identifier)
|
||||
if asset is not None:
|
||||
return asset
|
||||
else:
|
||||
raise IdentifierNotFound(identifier=identifier)
|
||||
|
||||
@staticmethod
|
||||
def _lookup_symbol_in_infos(infos, as_of_date):
|
||||
"""
|
||||
@@ -342,7 +331,6 @@ class AssetFinder(object):
|
||||
self.cache = {}
|
||||
self.sym_cache = {}
|
||||
self.future_chains_cache = {}
|
||||
self.identifier_cache = {}
|
||||
self.fuzzy_match = {}
|
||||
|
||||
for identifier, row in self.metadata_cache.items():
|
||||
@@ -350,7 +338,6 @@ class AssetFinder(object):
|
||||
|
||||
# Insert asset into the various caches
|
||||
self.cache[asset.sid] = asset
|
||||
self.identifier_cache[identifier] = asset
|
||||
|
||||
if asset.symbol is not '':
|
||||
self.sym_cache.setdefault(asset.symbol, []).append(asset)
|
||||
|
||||
@@ -224,16 +224,6 @@ Asset with sid '{sid}' was not found.
|
||||
""".strip()
|
||||
|
||||
|
||||
class IdentifierNotFound(ZiplineError):
|
||||
"""
|
||||
Raised when a retrieve_asset_by_identifier() call contains a non-existent
|
||||
identifier.
|
||||
"""
|
||||
msg = """
|
||||
Asset with identifier '{identifier}' was not found.
|
||||
""".strip()
|
||||
|
||||
|
||||
class InvalidAssetType(ZiplineError):
|
||||
"""
|
||||
Raised when an AssetFinder tries to build an Asset with an invalid
|
||||
|
||||
@@ -18,6 +18,7 @@ Tools to generate data sources.
|
||||
"""
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import datetime
|
||||
|
||||
from zipline.gens.utils import hash_args
|
||||
|
||||
@@ -49,10 +50,9 @@ class DataFrameSource(DataSource):
|
||||
# Remap sids based on the trading environment
|
||||
self.identifiers = kwargs.get('sids', self.data.columns)
|
||||
env.update_asset_finder(identifiers=self.identifiers)
|
||||
self.data.columns = [
|
||||
env.asset_finder.retrieve_asset_by_identifier(identifier).sid
|
||||
for identifier in self.data.columns
|
||||
]
|
||||
self.data.columns, _ = env.asset_finder.lookup_generic(
|
||||
self.data.columns, datetime.datetime.now()
|
||||
)
|
||||
self.sids = self.data.columns
|
||||
|
||||
# Hash_value for downstream sorting.
|
||||
@@ -128,10 +128,9 @@ class DataPanelSource(DataSource):
|
||||
# Remap sids based on the trading environment
|
||||
self.identifiers = kwargs.get('sids', self.data.items)
|
||||
env.update_asset_finder(identifiers=self.identifiers)
|
||||
self.data.items = [
|
||||
env.asset_finder.retrieve_asset_by_identifier(identifier).sid
|
||||
for identifier in self.data.items
|
||||
]
|
||||
self.data.items, _ = env.asset_finder.lookup_generic(
|
||||
self.data.items, datetime.datetime.now()
|
||||
)
|
||||
self.sids = self.data.items
|
||||
|
||||
# Hash_value for downstream sorting.
|
||||
|
||||
@@ -137,13 +137,13 @@ class SpecificEquityTrades(object):
|
||||
set(event.sid for event in self.event_list)
|
||||
)
|
||||
env.update_asset_finder(identifiers=self.identifiers)
|
||||
self.sids = [
|
||||
env.asset_finder.retrieve_asset_by_identifier(identifier).sid
|
||||
for identifier in self.identifiers
|
||||
]
|
||||
assets_by_identifier = {}
|
||||
for identifier in self.identifiers:
|
||||
assets_by_identifier[identifier] = env.asset_finder.\
|
||||
lookup_generic(identifier, datetime.now())[0]
|
||||
self.sids = [asset.sid for asset in assets_by_identifier.values()]
|
||||
for event in self.event_list:
|
||||
event.sid = env.asset_finder.\
|
||||
retrieve_asset_by_identifier(event.sid).sid
|
||||
event.sid = assets_by_identifier[event.sid].sid
|
||||
|
||||
else:
|
||||
# Unpack config dictionary with default values.
|
||||
@@ -161,10 +161,11 @@ class SpecificEquityTrades(object):
|
||||
|
||||
self.identifiers = kwargs.get('sids', [1, 2])
|
||||
env.update_asset_finder(identifiers=self.identifiers)
|
||||
self.sids = [
|
||||
env.asset_finder.retrieve_asset_by_identifier(identifier).sid
|
||||
for identifier in self.identifiers
|
||||
]
|
||||
assets_by_identifier = {}
|
||||
for identifier in self.identifiers:
|
||||
assets_by_identifier[identifier] = env.asset_finder.\
|
||||
lookup_generic(identifier, datetime.now())[0]
|
||||
self.sids = [asset.sid for asset in assets_by_identifier.values()]
|
||||
|
||||
# Hash_value for downstream sorting.
|
||||
self.arg_string = hash_args(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user