def create_changed(self, buffer_created=True, buffer_updated=False, buffer_deleted=False, manage_columns=True): self.changes = ChangeManager( buffer_created=buffer_created, buffer_updated=buffer_updated, buffer_deleted=buffer_deleted, manage_columns=manage_columns, )
def test_changemanager(self): cm = ChangeManager(buffer_updated=True,buffer_deleted=True,manage_columns=True) self.assertEqual(cm.last_update(), 0) self.assertEqual(cm.created_length(), 0) self.assertEqual(cm.updated_length(), 0) self.assertEqual(cm.deleted_length(), 0) df = pd.DataFrame({'a': [ 1, 2, 3], Module.UPDATE_COLUMN: [ 1, 1, 1 ]}) now = 1 cm.update(now, df) self.assertEqual(cm.last_update(), now) self.assertEqual(cm.next_created(),slice(0, 3)) self.assertEqual(cm.updated_length(), 0) self.assertEqual(cm.deleted_length(), 0) self.assertTrue(cm.column_changes.created.equals(df.columns)) self.assertTrue(cm.column_changes.kept.equals(NIL_INDEX)) self.assertTrue(cm.column_changes.deleted.equals(NIL_INDEX)) now = 2 df = df.append(pd.DataFrame({'a': [ 4], Module.UPDATE_COLUMN: [ now ]}), ignore_index=True) cm.update(now, df) self.assertEqual(cm.last_update(), now) self.assertEqual(cm.next_created(), slice(3,4)) self.assertEqual(cm.updated_length(), 0) self.assertEqual(cm.deleted_length(), 0) self.assertTrue(cm.column_changes.created.equals(NIL_INDEX)) self.assertTrue(cm.column_changes.kept.equals(df.columns)) self.assertTrue(cm.column_changes.deleted.equals(NIL_INDEX)) now = 3 df = df.append(pd.DataFrame({'a': [ 5], Module.UPDATE_COLUMN: [ now ]}), ignore_index=True) cm.update(now, df) self.assertEqual(cm.last_update(), now) self.assertEqual(cm.next_created(),slice(4, 5)) self.assertEqual(cm.updated_length(), 0) self.assertEqual(cm.deleted_length(), 0) self.assertTrue(cm.column_changes.created.equals(NIL_INDEX)) self.assertTrue(cm.column_changes.kept.equals(df.columns)) self.assertTrue(cm.column_changes.deleted.equals(NIL_INDEX)) now = 4 df2 = df[df.index != 2] # remove index==2 cm.update(now, df2) self.assertEqual(cm.last_update(), now) self.assertEqual(cm.created_length(), 0) self.assertEqual(cm.updated_length(), 0) self.assertEqual(cm.next_deleted(),slice(2,3)) self.assertTrue(cm.column_changes.created.equals(NIL_INDEX)) self.assertTrue(cm.column_changes.kept.equals(df.columns)) self.assertTrue(cm.column_changes.deleted.equals(NIL_INDEX))
class DataFrameSlot(Slot): def __init__(self, output_module, output_name, input_module, input_name): super(DataFrameSlot, self).__init__(output_module, output_name, input_module, input_name) self.changes = None def last_update(self): if self.changes: return self.changes.last_update() return super(DataFrameSlot, self).last_update() def create_changed(self, buffer_created=True, buffer_updated=False, buffer_deleted=False, manage_columns=True): self.changes = ChangeManager( buffer_created=buffer_created, buffer_updated=buffer_updated, buffer_deleted=buffer_deleted, manage_columns=manage_columns, ) def update(self, run_number, buffer_created=True, buffer_updated=False, buffer_deleted=False, manage_columns=True): if self.changes is None: self.create_changed( buffer_created=buffer_created, buffer_updated=buffer_updated, buffer_deleted=buffer_deleted, manage_columns=manage_columns, ) with self.lock: df = self.data() return self.changes.update(run_number, df) def reset(self): if self.changes is not None: self.changes.reset() def flush_buffers(self): return self.changes.flush_buffers() def flush_created(self): return self.changes.flush_created() def next_created(self, n=None, as_slice=True): return self.changes.next_created(n, as_slice) def has_created(self): if self.changes: return self.changes.has_created() return False def created_length(self): if self.changes: return self.changes.created_length() return 0 def flush_updated(self): return self.changes.flush_updated() def next_updated(self, n=None, as_slice=True): return self.changes.next_updated(n, as_slice) def has_updated(self): if self.changes: return self.changes.has_updated() return False def updated_length(self): return self.changes.updated_length() def flush_deleted(self): return self.changes.flush_deleted() def next_deleted(self, n=None, as_slice=True): return self.changes.next_deleted(n, as_slice) def has_deleted(self): if self.changes: return self.changes.has_deleted() return False def deleted_length(self): return self.changes.deleted_length() def next_state(self): if self.changes: return self.changes.next_state() else: return Module.state_blocked @property def column_changes(self): if self.changes: return self.changes.column_changes else: return None @property def last_index(self): if self.changes: return self.changes.index else: return [] @property def changemanager(self): if self.changes is None: self.changes = self.create_changed() return self.changes