<s> """<STR_LIT>""" <EOL> from abc import ABCMeta , abstractproperty <EOL> from weakref import WeakValueDictionary <EOL> from numpy import dtype as dtype_class <EOL> from six import with_metaclass <EOL> from zipline . errors import ( <EOL> DTypeNotSpecified , <EOL> WindowedInputToWindowedTerm , <EOL> NotDType , <EOL> TermInputsNotSpecified , <EOL> TermOutputsEmpty , <EOL> UnsupportedDType , <EOL> WindowLengthNotSpecified , <EOL> ) <EOL> from zipline . lib . adjusted_array import can_represent_dtype <EOL> from zipline . utils . memoize import lazyval <EOL> from zipline . utils . numpy_utils import ( <EOL> bool_dtype , <EOL> default_missing_value_for_dtype , <EOL> ) <EOL> from zipline . utils . sentinel import sentinel <EOL> NotSpecified = sentinel ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> NotSpecifiedType = type ( NotSpecified ) <EOL> class Term ( with_metaclass ( ABCMeta , object ) ) : <EOL> """<STR_LIT>""" <EOL> dtype = NotSpecified <EOL> domain = NotSpecified <EOL> missing_value = NotSpecified <EOL> params = ( ) <EOL> _term_cache = WeakValueDictionary ( ) <EOL> def __new__ ( cls , <EOL> domain = domain , <EOL> dtype = dtype , <EOL> missing_value = missing_value , <EOL> * args , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if domain is NotSpecified : <EOL> domain = cls . domain <EOL> if dtype is NotSpecified : <EOL> dtype = cls . dtype <EOL> if missing_value is NotSpecified : <EOL> missing_value = cls . missing_value <EOL> dtype , missing_value = cls . validate_dtype ( <EOL> cls . __name__ , <EOL> dtype , <EOL> missing_value , <EOL> ) <EOL> params = cls . _pop_params ( kwargs ) <EOL> identity = cls . static_identity ( <EOL> domain = domain , <EOL> dtype = dtype , <EOL> missing_value = missing_value , <EOL> params = params , <EOL> * args , ** kwargs <EOL> ) <EOL> try : <EOL> return cls . _term_cache [ identity ] <EOL> except KeyError : <EOL> new_instance = cls . _term_cache [ identity ] = super ( Term , cls ) . __new__ ( cls ) . _init ( <EOL> domain = domain , <EOL> dtype = dtype , <EOL> missing_value = missing_value , <EOL> params = params , <EOL> * args , ** kwargs <EOL> ) <EOL> return new_instance <EOL> @ classmethod <EOL> def _pop_params ( cls , kwargs ) : <EOL> """<STR_LIT>""" <EOL> param_values = [ ] <EOL> for key in cls . params : <EOL> try : <EOL> value = kwargs . pop ( key ) <EOL> hash ( key ) <EOL> param_values . append ( value ) <EOL> except KeyError : <EOL> raise TypeError ( <EOL> "<STR_LIT>" . format ( <EOL> typename = cls . __name__ , <EOL> name = key <EOL> ) <EOL> ) <EOL> except TypeError : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> typename = cls . __name__ , <EOL> name = key , <EOL> value = value , <EOL> ) <EOL> ) <EOL> return tuple ( zip ( cls . params , param_values ) ) <EOL> @ staticmethod <EOL> def validate_dtype ( termname , dtype , missing_value ) : <EOL> """<STR_LIT>""" <EOL> if dtype is NotSpecified : <EOL> raise DTypeNotSpecified ( termname = termname ) <EOL> try : <EOL> dtype = dtype_class ( dtype ) <EOL> except TypeError : <EOL> raise NotDType ( dtype = dtype , termname = termname ) <EOL> if not can_represent_dtype ( dtype ) : <EOL> raise UnsupportedDType ( dtype = dtype , termname = termname ) <EOL> if missing_value is NotSpecified : <EOL> missing_value = default_missing_value_for_dtype ( dtype ) <EOL> return dtype , missing_value <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ classmethod <EOL> def static_identity ( cls , domain , dtype , missing_value , params ) : <EOL> """<STR_LIT>""" <EOL> return ( cls , domain , dtype , missing_value , params ) <EOL> def _init ( self , domain , dtype , missing_value , params ) : <EOL> """<STR_LIT>""" <EOL> self . domain = domain <EOL> self . dtype = dtype <EOL> self . missing_value = missing_value <EOL> for name , value in params : <EOL> if hasattr ( self , name ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> name = name , <EOL> value = getattr ( self , name ) , <EOL> ) <EOL> ) <EOL> self . params = dict ( params ) <EOL> self . _subclass_called_super_validate = False <EOL> self . _validate ( ) <EOL> assert self . _subclass_called_super_validate , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> del self . _subclass_called_super_validate <EOL> return self <EOL> def _validate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _subclass_called_super_validate = True <EOL> @ abstractproperty <EOL> def inputs ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ abstractproperty <EOL> def windowed ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ abstractproperty <EOL> def mask ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ abstractproperty <EOL> def dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class AssetExists ( Term ) : <EOL> """<STR_LIT>""" <EOL> dtype = bool_dtype <EOL> dataset = None <EOL> inputs = ( ) <EOL> dependencies = { } <EOL> mask = None <EOL> windowed = False <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> class LoadableTerm ( Term ) : <EOL> """<STR_LIT>""" <EOL> windowed = False <EOL> @ lazyval <EOL> def dependencies ( self ) : <EOL> return { self . mask : <NUM_LIT:0> } <EOL> class ComputableTerm ( Term ) : <EOL> """<STR_LIT>""" <EOL> inputs = NotSpecified <EOL> outputs = NotSpecified <EOL> window_length = NotSpecified <EOL> mask = NotSpecified <EOL> def __new__ ( cls , <EOL> inputs = inputs , <EOL> outputs = outputs , <EOL> window_length = window_length , <EOL> mask = mask , <EOL> * args , ** kwargs ) : <EOL> if inputs is NotSpecified : <EOL> inputs = cls . inputs <EOL> if inputs is not NotSpecified : <EOL> inputs = tuple ( inputs ) <EOL> if outputs is NotSpecified : <EOL> outputs = cls . outputs <EOL> if outputs is not NotSpecified : <EOL> outputs = tuple ( outputs ) <EOL> if mask is NotSpecified : <EOL> mask = cls . mask <EOL> if mask is NotSpecified : <EOL> mask = AssetExists ( ) <EOL> if window_length is NotSpecified : <EOL> window_length = cls . window_length <EOL> return super ( ComputableTerm , cls ) . __new__ ( <EOL> cls , <EOL> inputs = inputs , <EOL> outputs = outputs , <EOL> mask = mask , <EOL> window_length = window_length , <EOL> * args , ** kwargs <EOL> ) <EOL> def _init ( self , inputs , outputs , window_length , mask , * args , ** kwargs ) : <EOL> self . inputs = inputs <EOL> self . outputs = outputs <EOL> self . window_length = window_length <EOL> self . mask = mask <EOL> return super ( ComputableTerm , self ) . _init ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def static_identity ( cls , <EOL> inputs , <EOL> outputs , <EOL> window_length , <EOL> mask , <EOL> * args , <EOL> ** kwargs ) : <EOL> return ( <EOL> super ( ComputableTerm , cls ) . static_identity ( * args , ** kwargs ) , <EOL> inputs , <EOL> outputs , <EOL> window_length , <EOL> mask , <EOL> ) <EOL> def _validate ( self ) : <EOL> super ( ComputableTerm , self ) . _validate ( ) <EOL> if self . inputs is NotSpecified : <EOL> raise TermInputsNotSpecified ( termname = type ( self ) . __name__ ) <EOL> if not self . outputs : <EOL> raise TermOutputsEmpty ( termname = type ( self ) . __name__ ) <EOL> if self . window_length is NotSpecified : <EOL> raise WindowLengthNotSpecified ( termname = type ( self ) . __name__ ) <EOL> if self . mask is NotSpecified : <EOL> raise AssertionError ( "<STR_LIT>" . format ( term = self ) ) <EOL> if self . window_length : <EOL> for child in self . inputs : <EOL> if child . windowed : <EOL> raise WindowedInputToWindowedTerm ( parent = self , child = child ) <EOL> def _compute ( self , inputs , dates , assets , mask ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ lazyval <EOL> def windowed ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> self . window_length is not NotSpecified <EOL> and self . window_length > <NUM_LIT:0> <EOL> ) <EOL> @ lazyval <EOL> def dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> extra_input_rows = max ( <NUM_LIT:0> , self . window_length - <NUM_LIT:1> ) <EOL> out = { } <EOL> for term in self . inputs : <EOL> out [ term ] = extra_input_rows <EOL> out [ self . mask ] = <NUM_LIT:0> <EOL> return out <EOL> def __repr__ ( self ) : <EOL> return ( <EOL> "<STR_LIT>" <EOL> ) . format ( <EOL> type = type ( self ) . __name__ , <EOL> inputs = self . inputs , <EOL> window_length = self . window_length , <EOL> ) </s>
