<s> from agate . aggregations . has_nulls import HasNulls <EOL> from agate . computations . base import Computation <EOL> from agate . data_types import Date , DateTime , Number , TimeDelta <EOL> from agate . exceptions import DataTypeError <EOL> from agate . warns import warn_null_calculation <EOL> class Change ( Computation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , before_column_name , after_column_name ) : <EOL> self . _before_column_name = before_column_name <EOL> self . _after_column_name = after_column_name <EOL> def get_computed_data_type ( self , table ) : <EOL> before_column = table . columns [ self . _before_column_name ] <EOL> if isinstance ( before_column . data_type , Date ) : <EOL> return TimeDelta ( ) <EOL> elif isinstance ( before_column . data_type , DateTime ) : <EOL> return TimeDelta ( ) <EOL> elif isinstance ( before_column . data_type , TimeDelta ) : <EOL> return TimeDelta ( ) <EOL> elif isinstance ( before_column . data_type , Number ) : <EOL> return Number ( ) <EOL> def validate ( self , table ) : <EOL> before_column = table . columns [ self . _before_column_name ] <EOL> after_column = table . columns [ self . _after_column_name ] <EOL> for data_type in ( Number , Date , DateTime , TimeDelta ) : <EOL> if isinstance ( before_column . data_type , data_type ) : <EOL> if not isinstance ( after_column . data_type , data_type ) : <EOL> raise DataTypeError ( '<STR_LIT>' ) <EOL> if HasNulls ( self . _before_column_name ) . run ( table ) : <EOL> warn_null_calculation ( self , before_column ) <EOL> if HasNulls ( self . _after_column_name ) . run ( table ) : <EOL> warn_null_calculation ( self , after_column ) <EOL> return <EOL> raise DataTypeError ( '<STR_LIT>' ) <EOL> def run ( self , table ) : <EOL> new_column = [ ] <EOL> for row in table . rows : <EOL> before = row [ self . _before_column_name ] <EOL> after = row [ self . _after_column_name ] <EOL> if before and after : <EOL> new_column . append ( after - before ) <EOL> else : <EOL> new_column . append ( None ) <EOL> return new_column </s>
