def setUp(self): cells.reset() self.x = cells.Cell(None, name="x", value=1) self.b = cells.Cell(None, name="b", rule=lambda s, p: (p or 40) + 2) self.a = cells.Cell( None, name="a", rule=lambda s, p: self.b.getvalue() * self.x.getvalue())
def setUp(self): cells.reset() self.x = cells.Cell(None, name="x", value=21) self.captured_notify_flag = None def a_rule(s, p): self.captured_notify_flag = self.x.notifying return self.x.getvalue() * 2 self.a = cells.Cell(None, name="a", rule=a_rule)
def testA_AlternateEqualityTester(self): """5. A cell must allow an alternate function to be passed which tests for equality of the previous & new values.""" x = cells.Cell(None, name="x", value=5, unchanged_if=lambda old, new: abs(old - new) < 5) a = cells.Cell(None, name="a", rule=lambda s, p: x.getvalue() * 2) self.assertTrue(a.getvalue() == 10) x.set(7) # will *not* set, since |5-7| < 5 self.assertTrue(a.getvalue() == 10) # and so no propogation happens x.set(11) # will set, since |5-11| > 5 self.assertTrue(a.getvalue() == 22)
def testB_DelWorks(self): """6. A cell must be garbage collected appropriately; that is, other cells must not prevent it from being GCd because of internal references. """ import weakref self.x = cells.Cell(None, value=3) a = cells.Cell(None, rule=lambda s, p: self.x.getvalue() + 1) ref_x = weakref.ref(self.x) a.getvalue() del (self.x) self.assertFalse(ref_x())
def check_merge(cell1, cell2, rotations, interface, mask, params): if cell1.stats["Area"] <= 0 or cell2.stats["Area"] <= 0: # check if both cells exist return False # check if any cell is small enough for automatic merge if cell1.stats["Area"] < params.cell_force_merge_below or \ cell2.stats["Area"] < params.cell_force_merge_below: return True # check if dividing cells if params.merge_dividing_cells and \ interface >= params.merge_min_interface: tmp = cells.Cell(0) tmp.outline.extend(cell1.outline) tmp.outline.extend(cell2.outline) tmp.lines.extend(cell1.lines) tmp.lines.extend(cell2.lines) tmp.stats["Area"] = cell1.stats["Area"] + cell2.stats["Area"] tmp.compute_axes(rotations, mask.shape) tmpshort = tmp.stats["Width"] maxshort = max(cell1.stats["Width"], cell2.stats["Width"]) if tmpshort <= maxshort * params.merge_length_tolerance: return True else: return False else: return False
def test_ReturnWithoutRecalculation(self): """A cell does not recalculate itself if it is current """ cells.cellenv.curr = None x = cells.Cell(None, name="x", rule=lambda s, p: (p or 39) + 3) self.assertTrue( x.getvalue() == x.getvalue()) # should not calculate twice
def testC_QueriedCellRecalculates(self): """Rule 3 part c: A cell B which calls changed cell X, must recalculate when queried""" # x <-- a --> b # ^__________/ # we're gonna have to jury-rig this, since we can't let b # recalculate first naturally self.b = cells.Cell(None, name="b", rule=lambda s, p: (p or 2) * self.x.getvalue()) self.b.add_calls(self.x) # set up dependencies for b by hand self.b.add_called_by(self.a) self.a.add_calls(self.b, self.x) # and for a self.x.add_called_by(self.a, self.b) # .. and x # run a fake x.set(3) in the desired order: cells.cellenv.dp += 1 # self.x.value = 3 # TODO: verify everything's done here self.x.changed_dp = cells.cellenv.dp self.x.dp = cells.cellenv.dp self.a.updatecell() # causes b.updatecell() to run self.assertTrue(self.b.value == 6) # which causes b's rule to run self.x.changed = False # but now that x's change has propogated, further updates on a: self.a.updatecell() # which will call b.updatecell(), won't cause b.run() self.assertTrue(self.b.value == 6)
def test_3_ChangingKeyValuePropogates(self): def y_rule(model, prev): return self.x.keys() y = cells.Cell(None, name="y", rule=y_rule) y.getvalue() # establish deps self.failUnless(y.getvalue() == []) # no keys in x yet self.x['foo'] = 'blah blah' # cause propogation self.failUnless(y.getvalue() == ['foo'])
def add_cells_at_location(self, x, y, mass=.3, energy=None, color=None, x_vel=0, y_vel=0): self.cell_list.append(cells.Cell(x, y, mass, energy, x_vel, y_vel))
def runTest(self): environment = Environment( ) #environment already initialized in test.py # test that environment is a singleton self.assertTrue(Environment() is environment) # test that environment initializes properly self.assertEquals(len(environment.cell_list), 10) self.assertEquals(len(environment.food_set), 10) self.assertTrue(environment.width > 0) self.assertTrue(environment.height > 0) # test that cells are within bounds for cell in environment.cell_list: self.assertTrue( cell.pos.x >= 0 and cell.pos.x <= environment.width and cell.pos.y >= 0 and cell.pos.y <= environment.height, "Cell location out of bounds.") # ..and food is within bounds for f in environment.food_set: self.assertTrue( f.pos.x >= 0 and f.pos.x <= environment.width and f.pos.y >= 0 and f.pos.y <= environment.height, "Food location out of bounds.") environment.cell_list = [] # test that a cell will find and eat food underneath it c = cells.Cell(environment.width / 2, environment.height / 2) environment.cell_list.append(c) food_count = len(environment.food_set) environment.food_set.add( food.Food(environment.width / 2, environment.height / 2)) environment.tick() # test that food list count was decremented after food is eaten self.assertEqual(len(environment.food_set), food_count) # test that food inside the cell is eaten environment.food_set.add( food.Food(environment.width / 2 + c.radius - 0.000001, environment.height / 2)) environment.tick() self.assertEqual(len(environment.food_set), food_count) # test that food outside the cell is not eaten environment.food_set.add( food.Food(environment.width / 2 + c.radius + 0.000001, environment.height / 2)) environment.tick() self.assertEqual(len(environment.food_set), food_count + 1) # test that add_cells adds the right number of cells num_cells = len(environment.cell_list) add_cells_count = random.randint(0, 100) environment.add_cells(add_cells_count) self.assertEqual( len(environment.cell_list) - add_cells_count, num_cells)
def create_cell(): """ Создаёт новый эелемент "Клетка" и добавляет его в список объектов и списки интерфейса Параметр нового объекта запрашивается у пользователя """ size = simpledialog.askinteger('Размер клетки', 'Введите размер клетки:') try: new_cell = cells.Cell(size) cell_list.append(new_cell) renew_list_boxes() messagebox.showinfo('Создание клетки', f'Создана новая клетка:\n{str(new_cell)}') except ValueError as e: messagebox.showerror('Ошибка', e) except TypeError: messagebox.showinfo('Отмена', 'Операция отменена')
def setUp(self): # h i j # | | | # v v v # a --> b --> c # | | # \-- > x <---/ cells.reset() self.runlog = [] self.x = cells.Cell(None, name="x", value=5) def anon_rule(name, getfrom): def rule(s, p): self.runlog.append(name) return getfrom.getvalue() + (p or 0) return rule self.c = cells.Cell(None, name="c", rule=anon_rule('c', self.x)) self.b = cells.Cell(None, name="b", rule=anon_rule('b', self.c)) def a_rule(s, p): self.runlog.append("a") return self.b.getvalue() + self.x.getvalue() + (p or 0) self.a = cells.Cell(None, name="a", rule=a_rule) self.h = cells.Cell(None, name="h", rule=anon_rule('h', self.a)) self.i = cells.Cell(None, name="i", rule=anon_rule('i', self.b)) self.j = cells.Cell(None, name="j", rule=anon_rule('j', self.c)) # build dependencies self.h.getvalue() self.i.getvalue() self.j.getvalue() self.runlog = [] # run an x.set(3) in the desired order: self.x.propogation_list = lambda s, e: [self.a, self.c] self.c.propogation_list = lambda s, e: [self.b, self.j] self.b.propogation_list = lambda s, e: [self.a, self.i] self.x.set(3)
def add_cells(cell_count): ''' Add cell_count number of cells of random colors at random locations to the world''' for i in range(cell_count): World.cell_list.append( cells.Cell(random.uniform(0, World.width), random.uniform(0, World.height)))
def setUp(self): cells.reset() self.x = cells.Cell(None, name="x", value=21)
def new_cell(self, x, y): return cells.Cell(x, y)
def propogate(self, propogate_first=None): """ propogate(self, propogate_first=None) -> None Propogates an updatecell command to the set of cells which call this cell. @param propogate_first: If cell C{A} asks cell C{B} to update, and cell C{B}'s value changes (causing a C{propogate} call), it must propogate to C{A} first, before any of the other cells which call C{B}. """ if cells.cellenv.curr_propogator: _debug(self.name, "propogating. Old propogator was", cells.cellenv.curr_propogator.name) else: _debug(self.name, "propogating. No old propogator") prev_propogator = cells.cellenv.curr_propogator cells.cellenv.curr_propogator = self self.changed_dp = cells.cellenv.dp self.notifying = True if self.owner: self.owner._run_observers(self) # first, notify the 'propogate_first' cell if propogate_first: # append everything but the propogate_first cell onto the deferred # propogation FIFO cells.cellenv.queued_updates.extend( Cell.propogation_list(self, propogate_first)) _debug( self.name, "first propogating to", propogate_first.name, "then adding", [ cell.name for cell in Cell.propogation_list(self, propogate_first) ], "to deferred") _debug(self.name, "asking", propogate_first.name, "to update first") propogate_first.updatecell() _debug(self.name, "finished propogating to first update", propogate_first.name) else: _debug(self.name, "propogating to", [ cell.name for cell in Cell.propogation_list(self, propogate_first) ]) # weird for testing for cell in Cell.propogation_list(self, propogate_first): if cell.lazy: _debug(self.name, "saw", cell.name, ", but it's lazy -- not updating") else: _debug(self.name, "asking", cell.name, "to update") cell.updatecell(self) self.notifying = False cells.cellenv.curr_propogator = prev_propogator if cells.cellenv.curr_propogator: _debug(self.name, "finished propogating; switching to propogating", str(cells.cellenv.curr_propogator.name)) else: _debug(self.name, "finished propogating. No old propogator") # run deferred stuff if no cell is currently propogating if not cells.cellenv.curr_propogator: # first, updates: # okay, this is a little hacky: cells.cellenv.curr_propogator = cells.Cell( None, name="queued propogation dummy") _debug("no cell propogating! running deferred updates.") to_update = cells.cellenv.queued_updates cells.cellenv.queued_updates = [] for cell in to_update: if cell.lazy: _debug(self.name, "saw", cell.name, ", but it's lazy -- not running deferred updated") else: _debug("Running deferred update on", cell.name) cell.updatecell(self) cells.cellenv.curr_propogator = None # next, deferred set-ish commands: _debug("running deferred sets") to_set = cells.cellenv.deferred_sets cells.cellenv.deferred_sets = [] for cell, cmdargtuple in to_set: cmd, argtuple = cmdargtuple args, kwargs = argtuple _debug("running deferred", cmd, "on", cell.name) args, kwargs = argtuple getattr(cell, cmd)(*args, **kwargs)
def add_cell_at_location(pos): """Add a cell at location""" World.cell_list.append(cells.Cell(pos.x, pos.y))
def add_cells(self, cell_count): for i in range(cell_count): self.cell_list.append( cells.Cell(random.uniform(0, self.width), random.uniform(0, self.height)))