def calculate_row_delta(row, cached_row, data_key_col, timestamp): assert type(row) is OrderedDict or dict assert type(cached_row) is OrderedDict or dict or None, "cached row must be dict even if empty" delta_row = DeltaRow(data_key_col, timestamp) # Calculate only delta for each value which is not the key column non_key_data = {key: val for (key, val) in row.items() if key != data_key_col} for field_name, measured_value in non_key_data.items(): delta_field = StatCalculator.get_delta_field(cached_row, field_name, measured_value) delta_row.add_field(delta_field) # Append the key # each call to get_delta will result in one row, but it might contain only the key delta_row.add_field(DeltaField(field_name=data_key_col, measured=row[data_key_col])) return delta_row
def test_should_serialize_delta_row_to_dict(self): dr = DeltaRow("a", "timestamp") dr.add_field(DeltaField("a", measured="key value")) dr.add_field(DeltaField("b", measured=100, delta=50, previous=75)) actual = dr.as_dict() self.assertEquals( actual, { "a_measured": "key value", "b_measured": 100, "b_delta": 50, "b_previous": 75, "timestamp": "timestamp", "key_col": "key value", }, )