diff --git a/zipline/assets/assets.py b/zipline/assets/assets.py index 2962d952..1f26d9c9 100644 --- a/zipline/assets/assets.py +++ b/zipline/assets/assets.py @@ -85,19 +85,6 @@ class AssetFinder(object): self.populate_cache() - def _next_free_sid(self): - if len(self.cache) > 0: - return max(self.cache.keys()) + 1 - return 0 - - def _assign_sid(self, identifier): - if hasattr(identifier, '__int__'): - return identifier.__int__() - if not self.allow_sid_assignment: - raise SidAssignmentError(identifier=identifier) - if isinstance(identifier, string_types): - return self._next_free_sid() - def retrieve_asset(self, sid, default_none=False): if isinstance(sid, Asset): return sid @@ -290,16 +277,6 @@ class AssetFinder(object): def _spawn_asset(self, identifier, **kwargs): - # Check if the sid is declared - try: - kwargs['sid'] - pass - except KeyError: - # If the identifier is not a sid, assign one - kwargs['sid'] = self._assign_sid(identifier) - # Update the metadata object with the new sid - self.insert_metadata(identifier=identifier, sid=kwargs['sid']) - # If the file_name is in the kwargs, it will be used as the symbol try: kwargs['symbol'] = kwargs.pop('file_name') @@ -545,6 +522,21 @@ class AssetFinder(object): continue entry[key] = value + # Check if the sid is declared + try: + entry['sid'] + except KeyError: + # If the identifier is not a sid, assign one + if hasattr(identifier, '__int__'): + entry['sid'] = identifier.__int__() + else: + if self.allow_sid_assignment: + # Assign the sid the value of its insertion order. + # This assumes that we are assigning values to all assets. + entry['sid'] = len(self.metadata_cache) + else: + raise SidAssignmentError(identifier=identifier) + self.metadata_cache[identifier] = entry def consume_identifiers(self, identifiers):