def test_preorder_traversal(self): clist, traversal = \ super(_TestActiveListContainerBase, self).\ test_preorder_traversal() descend = lambda x: not x._is_heterogeneous_container clist[1].deactivate() self.assertEqual([None, '[0]', '[2]'], [ c.name for c in pmo.preorder_traversal( clist, active=True, descend=descend) ]) self.assertEqual( [id(clist), id(clist[0]), id(clist[2])], [ id(c) for c in pmo.preorder_traversal( clist, active=True, descend=descend) ]) clist[1].deactivate(shallow=False) self.assertEqual([c.name for c in traversal if c.active], [ c.name for c in pmo.preorder_traversal( clist, active=True, descend=descend) ]) self.assertEqual([id(c) for c in traversal if c.active], [ id(c) for c in pmo.preorder_traversal( clist, active=True, descend=descend) ]) clist.deactivate() self.assertEqual(len(list(pmo.preorder_traversal(clist, active=True))), 0) self.assertEqual(len(list(pmo.generate_names(clist, active=True))), 0)
def test_preorder_traversal(self): ctuple, traversal = \ super(_TestActiveTupleContainerBase, self).\ test_preorder_traversal() ctuple[1].deactivate() self.assertEqual( [None, '[0]', '[2]'], [c.name for c in pmo.preorder_traversal(ctuple, active=True)]) self.assertEqual( [id(ctuple), id(ctuple[0]), id(ctuple[2])], [id(c) for c in pmo.preorder_traversal(ctuple, active=True)]) ctuple[1].deactivate(shallow=False) self.assertEqual( [c.name for c in traversal if c.active], [c.name for c in pmo.preorder_traversal(ctuple, active=True)]) self.assertEqual( [id(c) for c in traversal if c.active], [id(c) for c in pmo.preorder_traversal(ctuple, active=True)]) ctuple.deactivate() self.assertEqual( len(list(pmo.preorder_traversal(ctuple, active=True))), 0) self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0)
def test_preorder_traversal(self): ctuple, traversal = \ super(_TestActiveTupleContainerBase, self).\ test_preorder_traversal() ctuple[1].deactivate() self.assertEqual([None, '[0]', '[2]'], [c.name for c in pmo.preorder_traversal( ctuple, active=True)]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[2])], [id(c) for c in pmo.preorder_traversal( ctuple, active=True)]) ctuple[1].deactivate(shallow=False) self.assertEqual([c.name for c in traversal if c.active], [c.name for c in pmo.preorder_traversal( ctuple, active=True)]) self.assertEqual([id(c) for c in traversal if c.active], [id(c) for c in pmo.preorder_traversal( ctuple, active=True)]) ctuple.deactivate() self.assertEqual(len(list(pmo.preorder_traversal(ctuple, active=True))), 0) self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0)
def test_preorder_traversal_descend_check(self): csubtuple = self._container_type([self._ctype_factory()]) ctuple = self._container_type( [self._ctype_factory(), csubtuple, self._ctype_factory()]) traversal = [] traversal.append(ctuple) traversal.append(ctuple[0]) traversal.append(ctuple[1]) traversal.append(ctuple[1][0]) traversal.append(ctuple[2]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return False descend.seen = [] order = list(pmo.preorder_traversal(ctuple, descend=descend)) self.assertEqual(len(order), 1) self.assertIs(order[0], ctuple) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], ctuple) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) return ctuple, traversal
def test_preorder_traversal_descend_check(self): traversal = [] clist = self._container_type() traversal.append(clist) clist.append(self._ctype_factory()) traversal.append(clist[-1]) clist.append(self._container_type()) traversal.append(clist[-1]) clist[1].append(self._ctype_factory()) traversal.append(clist[1][-1]) clist.append(self._ctype_factory()) traversal.append(clist[-1]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return False descend.seen = [] order = list(pmo.preorder_traversal(clist, descend=descend)) self.assertEqual(len(order), 1) self.assertIs(order[0], clist) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], clist) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return not x._is_heterogeneous_container descend.seen = [] order = list(pmo.preorder_traversal(clist, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return not x._is_heterogeneous_container descend.seen = [] order = list(pmo.preorder_traversal(clist, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) return clist, traversal
def test_preorder_traversal(self): A = numpy.ones((3,3)) m = block() m.c = matrix_constraint(A) m.v = variable() m.V = variable_list() m.V.append(variable()) m.B = block_list() m.B.append(block()) m.B[0].c = matrix_constraint(A) m.B[0].v = variable() m.B[0].V = variable_list() m.B[0].V.append(variable()) m.b = block() m.b.c = constraint_dict() m.b.c[None] = matrix_constraint(A) m.b.c[1] = matrix_constraint(A) m.b.c[2] = constraint() m.b.c[3] = constraint_list() # don't visit things below a matrix constraint # (e.g., cases where we want to handle it in bulk) def no_mc_descend(x): if isinstance(x, matrix_constraint): return False return True cnt = 0 for obj in pmo.preorder_traversal(m, ctype=IConstraint, descend=no_mc_descend): self.assertTrue(type(obj.parent) is not matrix_constraint) self.assertTrue((obj.ctype is block._ctype) or \ (obj.ctype is constraint._ctype)) cnt += 1 self.assertEqual(cnt, 11) cnt = 0 mc_child_cnt = 0 for obj in pmo.preorder_traversal(m, ctype=IConstraint): self.assertTrue((obj.ctype is block._ctype) or \ (obj.ctype is constraint._ctype)) if type(obj.parent) is matrix_constraint: mc_child_cnt += 1 cnt += 1 self.assertEqual(cnt, 23) self.assertEqual(mc_child_cnt, 12) self.assertEqual( len(list(m.components(ctype=IConstraint))), 13)
def test_component_data_objects_hack(self): model = _model.clone() self.assertEqual( [str(obj) for obj in model.component_data_objects()], [str(obj) for obj in model.components()]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IVariable)], [str(obj) for obj in model.components(ctype=IVariable)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IConstraint)], [str(obj) for obj in model.components(ctype=IConstraint)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IBlock)], [str(obj) for obj in model.components(ctype=IBlock)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IJunk)], [str(obj) for obj in model.components(ctype=IJunk)]) for item in pmo.preorder_traversal(model): item.deactivate() self.assertEqual( [str(obj) for obj in model.component_data_objects(active=True)], [str(obj) for obj in model.components(active=True)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IVariable, active=True)], [str(obj) for obj in model.components(ctype=IVariable, active=True)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IConstraint, active=True)], [str(obj) for obj in model.components(ctype=IConstraint, active=True)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IBlock, active=True)], [str(obj) for obj in model.components(ctype=IBlock, active=True)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IJunk, active=True)], [str(obj) for obj in model.components(ctype=IJunk, active=True)]) item.activate()
def generate_cids(model, prefix=(), **kwds): """Generate forward and reverse mappings between model components and deterministic, unique identifiers that are safe to serialize or use as dictionary keys.""" object_to_cid = pmo.ComponentMap() cid_to_object = collections.OrderedDict() if hasattr(pmo, "preorder_traversal"): # pragma:nocover fn = lambda *args, **kwds: pmo.preorder_traversal(model, *args, **kwds) else: # pragma:nocover fn = model.preorder_traversal try: fn(return_key=True) except TypeError: traversal = fn(**kwds) obj_ = six.next(traversal) assert obj_ is model object_to_cid[model] = prefix cid_to_object[prefix] = model for obj in traversal: parent = obj.parent key = obj.storage_key cid_ = object_to_cid[obj] = object_to_cid[parent] + (key, ) cid_to_object[cid_] = obj else: # pragma:nocover traversal = fn(return_key=True, **kwds) obj_ = six.next(traversal)[1] assert obj_ is model object_to_cid[model] = prefix cid_to_object[prefix] = model for key, obj in traversal: parent = obj.parent cid_ = object_to_cid[obj] = object_to_cid[parent] + (key, ) cid_to_object[cid_] = obj return object_to_cid, cid_to_object
def test_preorder_traversal(self): csubtuple = self._container_type([self._ctype_factory()]) ctuple = self._container_type( [self._ctype_factory(), csubtuple, self._ctype_factory()]) traversal = [] traversal.append(ctuple) traversal.append(ctuple[0]) traversal.append(ctuple[1]) traversal.append(ctuple[1][0]) traversal.append(ctuple[2]) self.assertEqual([c.name for c in traversal], [c.name for c in pmo.preorder_traversal(ctuple)]) self.assertEqual([id(c) for c in traversal], [id(c) for c in pmo.preorder_traversal(ctuple)]) return ctuple, traversal
def test_preorder_traversal(self): traversal = [] cdict = self._container_type() traversal.append(cdict) cdict[0] = self._ctype_factory() traversal.append(cdict[0]) cdict[1] = self._container_type() traversal.append(cdict[1]) cdict[1][0] = self._ctype_factory() traversal.append(cdict[1][0]) cdict[2] = self._ctype_factory() traversal.append(cdict[2]) descend = lambda x: not x._is_heterogeneous_container self.assertEqual( [c.name for c in traversal], [c.name for c in pmo.preorder_traversal(cdict, descend=descend)]) self.assertEqual( [id(c) for c in traversal], [id(c) for c in pmo.preorder_traversal(cdict, descend=descend)]) return cdict, traversal
def test_preorder_traversal(self): csubtuple = self._container_type( [self._ctype_factory()]) ctuple = self._container_type( [self._ctype_factory(), csubtuple, self._ctype_factory()]) traversal = [] traversal.append(ctuple) traversal.append(ctuple[0]) traversal.append(ctuple[1]) traversal.append(ctuple[1][0]) traversal.append(ctuple[2]) self.assertEqual([c.name for c in traversal], [c.name for c in pmo.preorder_traversal(ctuple)]) self.assertEqual([id(c) for c in traversal], [id(c) for c in pmo.preorder_traversal(ctuple)]) return ctuple, traversal
def test_preorder_traversal(self): cdict, traversal = \ super(_TestActiveDictContainerBase, self).\ test_preorder_traversal() descend = lambda x: not x._is_heterogeneous_container cdict[1].deactivate() self.assertEqual([None, '[0]', '[2]'], [c.name for c in pmo.preorder_traversal( cdict, active=True, descend=descend)]) self.assertEqual([id(cdict),id(cdict[0]),id(cdict[2])], [id(c) for c in pmo.preorder_traversal( cdict, active=True, descend=descend)]) cdict[1].deactivate(shallow=False) self.assertEqual([c.name for c in traversal if c.active], [c.name for c in pmo.preorder_traversal( cdict, active=True, descend=descend)]) self.assertEqual([id(c) for c in traversal if c.active], [id(c) for c in pmo.preorder_traversal( cdict, active=True, descend=descend)]) cdict.deactivate() self.assertEqual(len(list(pmo.preorder_traversal(cdict, active=True))), 0) self.assertEqual(len(list(pmo.generate_names(cdict, active=True))), 0)
def test_component_data_objects_hack(self): model = _model.clone() self.assertEqual([str(obj) for obj in model.component_data_objects()], [str(obj) for obj in model.components()]) self.assertEqual([ str(obj) for obj in model.component_data_objects(ctype=IVariable) ], [str(obj) for obj in model.components(ctype=IVariable)]) self.assertEqual([ str(obj) for obj in model.component_data_objects(ctype=IConstraint) ], [str(obj) for obj in model.components(ctype=IConstraint)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IBlock)], [str(obj) for obj in model.components(ctype=IBlock)]) self.assertEqual( [str(obj) for obj in model.component_data_objects(ctype=IJunk)], [str(obj) for obj in model.components(ctype=IJunk)]) for item in pmo.preorder_traversal(model): item.deactivate() self.assertEqual([ str(obj) for obj in model.component_data_objects(active=True) ], [str(obj) for obj in model.components(active=True)]) self.assertEqual([ str(obj) for obj in model.component_data_objects(ctype=IVariable, active=True) ], [ str(obj) for obj in model.components(ctype=IVariable, active=True) ]) self.assertEqual([ str(obj) for obj in model.component_data_objects(ctype=IConstraint, active=True) ], [ str(obj) for obj in model.components(ctype=IConstraint, active=True) ]) self.assertEqual([ str(obj) for obj in model.component_data_objects(ctype=IBlock, active=True) ], [ str(obj) for obj in model.components(ctype=IBlock, active=True) ]) self.assertEqual([ str(obj) for obj in model.component_data_objects(ctype=IJunk, active=True) ], [ str(obj) for obj in model.components(ctype=IJunk, active=True) ]) item.activate()
def test_preorder_traversal(self): traversal = [] cdict = self._container_type() traversal.append(cdict) cdict[0] = self._ctype_factory() traversal.append(cdict[0]) cdict[1] = self._container_type() traversal.append(cdict[1]) cdict[1][0] = self._ctype_factory() traversal.append(cdict[1][0]) cdict[2] = self._ctype_factory() traversal.append(cdict[2]) descend = lambda x: not x._is_heterogeneous_container self.assertEqual([c.name for c in traversal], [c.name for c in pmo.preorder_traversal( cdict, descend=descend)]) self.assertEqual([id(c) for c in traversal], [id(c) for c in pmo.preorder_traversal( cdict, descend=descend)]) return cdict, traversal
def test_preorder_traversal_descend_check(self): ctuple, traversal = \ super(_TestActiveTupleContainerBase, self).\ test_preorder_traversal_descend_check() ctuple[1].deactivate() def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual([None, '[0]', '[2]'], [c.name for c in order]) self.assertEqual([id(ctuple), id(ctuple[0]), id(ctuple[2])], [id(c) for c in order]) if ctuple.ctype._is_heterogeneous_container: self.assertEqual([None, '[0]', '[2]'], [c.name for c in descend.seen]) self.assertEqual( [id(ctuple), id(ctuple[0]), id(ctuple[2])], [id(c) for c in descend.seen]) else: self.assertEqual([None], [c.name for c in descend.seen]) self.assertEqual([id(ctuple)], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual([None, '[0]', '[1]', '[2]'], [c.name for c in order]) self.assertEqual( [id(ctuple), id(ctuple[0]), id(ctuple[1]), id(ctuple[2])], [id(c) for c in order]) if ctuple.ctype._is_heterogeneous_container: self.assertEqual([None, '[0]', '[1]', '[2]'], [c.name for c in descend.seen]) self.assertEqual( [id(ctuple), id(ctuple[0]), id(ctuple[1]), id(ctuple[2])], [id(c) for c in descend.seen]) else: self.assertEqual([None, '[1]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple), id(ctuple[1])], [id(c) for c in descend.seen]) ctuple[1].deactivate(shallow=False) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual([c.name for c in traversal if c.active], [c.name for c in order]) self.assertEqual([id(c) for c in traversal if c.active], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c.active and \ c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c.active and \ c._is_container], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual([None, '[0]', '[1]', '[2]'], [c.name for c in order]) self.assertEqual( [id(ctuple), id(ctuple[0]), id(ctuple[1]), id(ctuple[2])], [id(c) for c in order]) if ctuple.ctype._is_heterogeneous_container: self.assertEqual([None, '[0]', '[1]', '[2]'], [c.name for c in descend.seen]) self.assertEqual( [id(ctuple), id(ctuple[0]), id(ctuple[1]), id(ctuple[2])], [id(c) for c in descend.seen]) else: self.assertEqual([None, '[1]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple), id(ctuple[1])], [id(c) for c in descend.seen]) ctuple.deactivate() def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual(len(descend.seen), 0) self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], ctuple) ctuple.deactivate(shallow=False) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual(len(descend.seen), 0) self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list( pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], ctuple)
def test_preorder_traversal_descend_check(self): traversal = [] cdict = self._container_type() traversal.append(cdict) cdict[0] = self._ctype_factory() traversal.append(cdict[0]) cdict[1] = self._container_type() traversal.append(cdict[1]) cdict[1][0] = self._ctype_factory() traversal.append(cdict[1][0]) cdict[2] = self._ctype_factory() traversal.append(cdict[2]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return False descend.seen = [] order = list(pmo.preorder_traversal(cdict, descend=descend)) self.assertEqual(len(order), 1) self.assertIs(order[0], cdict) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], cdict) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return not x._is_heterogeneous_container descend.seen = [] order = list(pmo.preorder_traversal(cdict, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return not x._is_heterogeneous_container descend.seen = [] order = list(pmo.preorder_traversal(cdict, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) return cdict, traversal
def test_preorder_traversal_descend_check(self): ctuple, traversal = \ super(_TestActiveTupleContainerBase, self).\ test_preorder_traversal_descend_check() ctuple[1].deactivate() def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual([None, '[0]', '[2]'], [c.name for c in order]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[2])], [id(c) for c in order]) if ctuple.ctype._is_heterogeneous_container: self.assertEqual([None, '[0]', '[2]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[2])], [id(c) for c in descend.seen]) else: self.assertEqual([None], [c.name for c in descend.seen]) self.assertEqual([id(ctuple)], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual([None,'[0]','[1]','[2]'], [c.name for c in order]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])], [id(c) for c in order]) if ctuple.ctype._is_heterogeneous_container: self.assertEqual([None,'[0]','[1]','[2]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])], [id(c) for c in descend.seen]) else: self.assertEqual([None,'[1]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple),id(ctuple[1])], [id(c) for c in descend.seen]) ctuple[1].deactivate(shallow=False) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual([c.name for c in traversal if c.active], [c.name for c in order]) self.assertEqual([id(c) for c in traversal if c.active], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c.active and \ c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c.active and \ c._is_container], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual([None,'[0]','[1]','[2]'], [c.name for c in order]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])], [id(c) for c in order]) if ctuple.ctype._is_heterogeneous_container: self.assertEqual([None,'[0]','[1]','[2]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple),id(ctuple[0]),id(ctuple[1]),id(ctuple[2])], [id(c) for c in descend.seen]) else: self.assertEqual([None,'[1]'], [c.name for c in descend.seen]) self.assertEqual([id(ctuple),id(ctuple[1])], [id(c) for c in descend.seen]) ctuple.deactivate() def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual(len(descend.seen), 0) self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], ctuple) ctuple.deactivate(shallow=False) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=True, descend=descend)) self.assertEqual(len(descend.seen), 0) self.assertEqual(len(list(pmo.generate_names(ctuple, active=True))), 0) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return x.active descend.seen = [] order = list(pmo.preorder_traversal(ctuple, active=None, descend=descend)) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], ctuple)
def test_preorder_traversal_descend_check(self): csubtuple = self._container_type( [self._ctype_factory()]) ctuple = self._container_type( [self._ctype_factory(), csubtuple, self._ctype_factory()]) traversal = [] traversal.append(ctuple) traversal.append(ctuple[0]) traversal.append(ctuple[1]) traversal.append(ctuple[1][0]) traversal.append(ctuple[2]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return False descend.seen = [] order = list(pmo.preorder_traversal(ctuple, descend=descend)) self.assertEqual(len(order), 1) self.assertIs(order[0], ctuple) self.assertEqual(len(descend.seen), 1) self.assertIs(descend.seen[0], ctuple) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) def descend(x): self.assertTrue(x._is_container) descend.seen.append(x) return True descend.seen = [] order = list(pmo.preorder_traversal(ctuple, descend=descend)) self.assertEqual([c.name for c in traversal], [c.name for c in order]) self.assertEqual([id(c) for c in traversal], [id(c) for c in order]) self.assertEqual([c.name for c in traversal if c._is_container], [c.name for c in descend.seen]) self.assertEqual([id(c) for c in traversal if c._is_container], [id(c) for c in descend.seen]) return ctuple, traversal