ENH: Coerce user input with API method decorator

Previously we have capitalized input strings at different levels in
our code: in the user-facing API methods and in the asset finder.
This commit moves input string capitalization exclusively to the API
method to which the string was supplied. Specifically, the string is
capitalized by a preprocess API method decorator. The preprocess
decorator passes the input string to the newly defined
ensure_upper_case() method, which returns a TypeError if the argument
supplied is not a string.

ensure_upper_case() is defined in a new file, zipline/utils/input_validation.py.
The existing expect_types() method is also moved there.

Various tests in tests/test_assets.py are modified to account for the
fact that the asset finder method lookup_symol() no longer capitalizes
its supplied argument.
This commit is contained in:
Stewart Douglas
2015-10-08 15:41:33 -04:00
parent 69b734129f
commit 4e2039c9b0
8 changed files with 202 additions and 118 deletions
+6 -1
View File
@@ -85,6 +85,7 @@ from zipline.utils.api_support import (
require_not_initialized,
ZiplineAPI,
)
from zipline.utils.input_validation import ensure_upper_case
from zipline.utils.cache import CachedObject, Expired
import zipline.utils.events
from zipline.utils.events import (
@@ -95,6 +96,7 @@ from zipline.utils.events import (
)
from zipline.utils.factory import create_simulation_parameters
from zipline.utils.math_utils import tolerant_equals
from zipline.utils.preprocess import preprocess
import zipline.protocol
from zipline.protocol import Event
@@ -738,6 +740,7 @@ class TradingAlgorithm(object):
self._recorded_vars[name] = value
@api_method
@preprocess(symbol_str=ensure_upper_case)
def symbol(self, symbol_str):
"""
Default symbol lookup for any source that directly maps the
@@ -770,6 +773,7 @@ class TradingAlgorithm(object):
return self.asset_finder.retrieve_asset(a_sid)
@api_method
@preprocess(symbol=ensure_upper_case)
def future_symbol(self, symbol):
""" Lookup a futures contract with a given symbol.
@@ -792,6 +796,7 @@ class TradingAlgorithm(object):
return self.asset_finder.lookup_future_symbol(symbol)
@api_method
@preprocess(root_symbol=ensure_upper_case)
def future_chain(self, root_symbol, as_of_date=None):
""" Look up a future chain with the specified parameters.
@@ -823,7 +828,7 @@ class TradingAlgorithm(object):
return FutureChain(
asset_finder=self.asset_finder,
get_datetime=self.get_datetime,
root_symbol=root_symbol.upper(),
root_symbol=root_symbol,
as_of_date=as_of_date
)