def test_OLD_beforeChild_acceptChildResult_afterChild(self): counts = [0,0,0] def before(node, child): counts[0] += 1 if type(child) in nonpyomo_leaf_types \ or not child.is_expression_type(): return False, None def accept(node, data, child_result): counts[1] += 1 def after(node, child): counts[2] += 1 os = StringIO() with LoggingIntercept(os, 'pyomo'): walker = StreamBasedExpressionVisitor( beforeChild=before, acceptChildResult=accept, afterChild=after) self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the " "beforeChild() method", os.getvalue().replace('\n',' ')) self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the " "acceptChildResult() method", os.getvalue().replace('\n',' ')) self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the " "afterChild() method", os.getvalue().replace('\n',' ')) ans = walker.walk_expression(self.e) m = self.m self.assertEqual(ans, None) self.assertEquals(counts, [9,9,9])
def render_html(unid, uri, username, srcstore): """ Render the document tree naively, as HTML. Note: this is just for fun. You should develop your own rendering interface, possibly modifying the document tree before rendering it. """ sr = srcstore.get(username, [unid], ('doctree',)) assert len(sr) == 1 src = sr[0] doctree = src['doctree'] os = StringIO.StringIO() if doctree is None: print >> os, u'Content-type:', 'text/plain' print >> os print >> os, u'(No document tree in sources upload.)' return os.getvalue() scheme, netloc, path, parameters, query, fragid = urlparse.urlparse(uri) settings = {'embed_stylesheet': False, 'output_encoding': 'unicode'} parts = docutils.core.publish_parts( reader_name='doctree', source_class=docutils.io.DocTreeInput, source=doctree, source_path='test', writer_name='html', settings_overrides=settings) print >> os, pages_header print >> os, navig(uri, unid) os.write(parts['html_body']) print >> os, pages_footer return os.getvalue()
def test_expand_multiple_indexed(self): m = ConcreteModel() m.x = Var([1, 2], domain=Binary) m.y = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.a1 = Var([1, 2]) m.a2 = Var([1, 2]) m.b1 = Var() m.b2 = Var() m.ECON2 = Connector() m.ECON2.add(m.a1, 'x') m.ECON2.add(m.b1, 'y') m.ECON1 = Connector() m.ECON1.add(m.a2, 'x') m.ECON1.add(m.b2, 'y') # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON1) m.d = Constraint(expr=m.ECON2 == m.ECON1) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 3) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 9) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - a2[1] : 0.0 : True 2 : 0.0 : x[2] - a2[2] : 0.0 : True 3 : 0.0 : y - b2 : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """d.expanded : Size=3, Index=d.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : a1[1] - a2[1] : 0.0 : True 2 : 0.0 : a1[2] - a2[2] : 0.0 : True 3 : 0.0 : b1 - b2 : 0.0 : True """)
def test_expand_multiple_indexed(self): m = ConcreteModel() m.x = Var([1,2], domain=Binary) m.y = Var(bounds=(1,3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.a1 = Var([1,2]) m.a2 = Var([1,2]) m.b1 = Var() m.b2 = Var() m.ECON2 = Connector() m.ECON2.add(m.a1,'x') m.ECON2.add(m.b1,'y') m.ECON1 = Connector() m.ECON1.add(m.a2,'x') m.ECON1.add(m.b2,'y') # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr= m.CON == m.ECON1) m.d = Constraint(expr= m.ECON2 == m.ECON1) m.nocon = Constraint(expr = m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 3) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 9) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """c.expanded : Size=3, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - a2[1] : 0.0 : True 2 : 0.0 : x[2] - a2[2] : 0.0 : True 3 : 0.0 : y - b2 : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """d.expanded : Size=3, Index=d.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : a1[1] - a2[1] : 0.0 : True 2 : 0.0 : a1[2] - a2[2] : 0.0 : True 3 : 0.0 : b1 - b2 : 0.0 : True """)
def test_old_beforeChild(self): def before(node, child): if type(child) in nonpyomo_leaf_types \ or not child.is_expression_type(): return False, [child] os = StringIO() with LoggingIntercept(os, 'pyomo'): walker = StreamBasedExpressionVisitor(beforeChild=before) self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the beforeChild() " "method", os.getvalue().replace('\n', ' ')) ans = walker.walk_expression(self.e) m = self.m ref = [[[m.x], [2]], [m.y], [[m.z], [[m.x], [m.y]]]] self.assertEqual(str(ans), str(ref)) ans = walker.walk_expression(m.x) ref = [] self.assertEqual(str(ans), str(ref)) ans = walker.walk_expression(2) ref = [] self.assertEqual(str(ans), str(ref))
def to_string(self): from cStringIO import StringIO os = StringIO() self.display(output_stream=os) string = os.getvalue() os.close() return string
def render_extracted(unid, stored_unid, uri, username, conn, tables): """ Render information that was extracted from this document. Note: this is just for fun. You should develop your own rendering interface, possibly modifying the document tree before rendering it. """ os = StringIO.StringIO() print >> os, pages_header if unid is None: print >> os, navig_index(uri) else: print >> os, navig(uri, unid) print >> os, '<h1>Extracted Information</h1>' print >> os, '<h4>Contents</h4>' print >> os, '<ul id="#toc">' for tablename in tables: print >> os, ('<li><a href="#table-%s">%s</a></li>' % (tablename, tablename)) print >> os, '</ul>' for tablename in tables: os.write(dump_table(conn, tablename, uri, unid, stored_unid)) print >> os, pages_footer return os.getvalue()
def test_expand_indexed(self): m = ConcreteModel() m.x = Var([1,2]) m.y = Var() m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr= m.CON == 1) m.nocon = Constraint(expr = m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 5) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """c.expanded : Size=3, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 1.0 : x[1] : 1.0 : True 2 : 1.0 : x[2] : 1.0 : True 3 : 1.0 : y : 1.0 : True """)
def test_expand_empty_expression(self): m = ConcreteModel() m.x = Var() m.y = Var() m.CON = Connector() m.CON.add(-m.x, 'x') m.CON.add(1 + m.y, 'y') m.ECON = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON) m.nocon = Constraint(expr=m.x == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 4) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=2, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : -1 * x - ECON.auto.x : 0.0 : True 2 : 0.0 : 1 + y - ECON.auto.y : 0.0 : True """)
def test_nested_GDP_with_deactivate(self): m = ConcreteModel() m.x = Var(bounds=(0, 1)) @m.Disjunct([0, 1]) def disj(disj, _): @disj.Disjunct(['A', 'B']) def nested(n_disj, _): pass # Blank nested disjunct return disj m.choice = Disjunction(expr=[m.disj[0], m.disj[1]]) m.c = Constraint(expr=m.x ** 2 + m.disj[1].nested['A'].indicator_var >= 1) m.disj[0].indicator_var.fix(1) m.disj[1].deactivate() m.disj[0].nested['A'].indicator_var.fix(1) m.disj[0].nested['B'].deactivate() m.disj[1].nested['A'].indicator_var.set_value(1) m.disj[1].nested['B'].deactivate() m.o = Objective(expr=m.x) TransformationFactory('gdp.fix_disjuncts').apply_to(m) os = StringIO() m.write(os, format='gams', io_options=dict(solver='dicopt')) self.assertIn("USING minlp", os.getvalue())
def test_expand_indexed(self): m = ConcreteModel() m.x = Var([1, 2]) m.y = Var() m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == 1) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 5) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 1.0 : x[1] : 1.0 : True 2 : 1.0 : x[2] : 1.0 : True 3 : 1.0 : y : 1.0 : True """)
def test_expand_empty_expression(self): m = ConcreteModel() m.x = Var() m.y = Var() m.CON = Connector() m.CON.add(-m.x, 'x') m.CON.add(1 + m.y, 'y') m.ECON = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr= m.CON == m.ECON) m.nocon = Constraint(expr = m.x == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 4) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """c.expanded : Size=2, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : - x - ECON.auto.x : 0.0 : True 2 : 0.0 : 1 + y - ECON.auto.y : 0.0 : True """)
def owl_string(self, idnent, space=2): os = StringIO.StringIO() def write(indent_level, line): os.write(' ' * (indent + indent_level) * space) os.write(line + os.linesep) write(0, '<owl:NamedIndividual rdf:about="%s">' % self.id) if self.type is not None: write(1, '<rdf:type rdf:resource="%s"/>' % self.type) for p in self.properties: if p.data_type is not None: write(1, '<%s rdf:datatype="%s">%s</%s>' % (p.tag, p.data_type, p.content, p.tag)) elif p.resource is not None: write(1, '<%s rdf:resource="%s"/>' % (p.tag, p.resource)) else: write(1, '<%s>%s</%s>' % (p.tag, p.content, p.tag)) for k, v in self.static_resources.items(): write(1, '<knowrob:%s rdf:resource="%s"/>' % (k, v)) for k, v in self.static_properties.items(): write(1, '<knowrob:%s rdf:datatype="&xsd;string>%s</knowrob:%s>' % (k, v, k)) write(0, '</owl:NamedIndividual>') return os.getvalue()
def test_solver_arg(self): m = ConcreteModel() m.x = Var() m.c = Constraint(expr=m.x == 2) m.o = Objective(expr=m.x) os = StringIO() m.write(os, format="gams", io_options=dict(solver="gurobi")) self.assertIn("option lp=gurobi", os.getvalue())
def render_source(unid, uri, username, srcstore): """ Render a basic page that dumps all the information available for a source upload. """ sr = srcstore.get(username, [unid], ('unid', 'filename', 'username', 'time', 'digest', 'errors', 'doctree', 'source', 'encoding')) assert len(sr) == 1 src = sr[0] doctree = src['doctree'] if doctree is not None: # Render the full debug page that displays the contents of an uploaded # source and parsed results. doctree_str = docutils.core.publish_from_doctree( doctree, writer_name='pseudoxml', settings_overrides={'output_encoding': 'unicode'}) else: doctree_str = '' encoding = src['encoding'] source = str(src['source']).decode(encoding).encode('utf-8') os = StringIO.StringIO() print >> os, pages_header print >> os, navig(uri, unid) print >> os, '<h1>Source</h1>' print >> os, '<dl>' print >> os, ('<dt>Source Filename</dt><dd>%s</dd>' % escape(src['filename'].encode('utf-8'))) print >> os, '<dt>Encoding</dt><dd>%s</dd>' % escape(encoding) print >> os, '<dt>User</dt><dd>%s</dd>' % escape(src['username']) print >> os, '<dt>Time Uploaded</dt><dd>%s</dd>' % escape(str(src['time'])) print >> os, '<dt>Digest</dt><dd>%s</dd>' % escape(src['digest']) print >> os, '</dl>' if src['errors']: print >> os, '<a href="#errors">Errors</a> | ' print >> os, '<a href="#doctree">Document Tree</a> | ' print >> os, '<a href="#source">Source</a> ' print >> os, '<hr/>' if src['errors']: print >> os, '<a name="errors"/><h2>Errors</h2>' print >> os, '<pre>' print >> os, escape(src['errors'].encode('utf-8')) print >> os, '</pre>' print >> os, '<a name="doctree"/><h2>Document Tree</h2>' print >> os, '<pre>' print >> os, escape(doctree_str.encode('utf-8')) print >> os, '</pre>' print >> os, '<hr/>' print >> os, '<a name="source"/><h2>Source</h2>' print >> os, '<pre>' print >> os, escape(source) print >> os, '</pre>' print >> os, '<hr width="5"/>' print >> os, pages_footer return os.getvalue().decode('utf-8')
def render_notfound(): """ Return a resource-not-found error to the client. """ os = StringIO.StringIO() print >> os, 'Content-type:', 'text/plain' print >> os, 'Status: 404 Document Not Found.' print >> os print >> os, 'Document Not Found.' return os.getvalue()
def test_pprint(self): pipe = ConcreteModel() pipe.SPECIES = Set(initialize=['a', 'b', 'c']) pipe.flow = Var() pipe.composition = Var(pipe.SPECIES) pipe.pIn = Var(within=NonNegativeReals) pipe.OUT = Connector() pipe.OUT.add(-pipe.flow, "flow") pipe.OUT.add(pipe.composition, "composition") pipe.OUT.add(pipe.composition['a'], "comp_a") pipe.OUT.add(pipe.pIn, "pressure") os = StringIO() pipe.OUT.pprint(ostream=os) self.assertEqual( os.getvalue(), """OUT : Size=1, Index=None Key : Name : Size : Variable None : comp_a : 1 : composition[a] : composition : 3 : composition : flow : 1 : -1 * flow : pressure : 1 : pIn """) def _IN(m, i): return { 'pressure': pipe.pIn, 'flow': pipe.composition[i] * pipe.flow } pipe.IN = Connector(pipe.SPECIES, rule=_IN) os = StringIO() pipe.IN.pprint(ostream=os) self.assertEqual( os.getvalue(), """IN : Size=3, Index=SPECIES Key : Name : Size : Variable a : flow : 1 : composition[a] * flow : pressure : 1 : pIn b : flow : 1 : composition[b] * flow : pressure : 1 : pIn c : flow : 1 : composition[c] * flow : pressure : 1 : pIn """)
def test_quicksum_integer_var_fixed(self): m = ConcreteModel() m.x = Var() m.y = Var(domain=Binary) m.c = Constraint(expr=quicksum([m.y, m.y], linear=True) == 1) m.o = Objective(expr=m.x ** 2) m.y.fix(1) os = StringIO() m.write(os, format='gams') self.assertIn("USING nlp", os.getvalue())
def test_display(self): pipe = ConcreteModel() pipe.SPECIES = Set(initialize=['a', 'b', 'c']) pipe.flow = Var(initialize=10) pipe.composition = Var(pipe.SPECIES, initialize=lambda m, i: ord(i) - ord('a')) pipe.pIn = Var(within=NonNegativeReals, initialize=3.14) pipe.OUT = Connector() pipe.OUT.add(-pipe.flow, "flow") pipe.OUT.add(pipe.composition, "composition") pipe.OUT.add(pipe.pIn, "pressure") os = StringIO() pipe.OUT.display(ostream=os) self.assertEqual( os.getvalue(), """OUT : Size=1 Key : Name : Value None : composition : {'a': 0, 'b': 1, 'c': 2} : flow : -10 : pressure : 3.14 """) def _IN(m, i): return { 'pressure': pipe.pIn, 'flow': pipe.composition[i] * pipe.flow } pipe.IN = Connector(pipe.SPECIES, rule=_IN) os = StringIO() pipe.IN.display(ostream=os) self.assertEqual( os.getvalue(), """IN : Size=3 Key : Name : Value a : flow : 0 : pressure : 3.14 b : flow : 10 : pressure : 3.14 c : flow : 20 : pressure : 3.14 """)
def test_pprint(self): pipe = ConcreteModel() pipe.SPECIES = Set(initialize=['a','b','c']) pipe.flow = Var() pipe.composition = Var(pipe.SPECIES) pipe.pIn = Var( within=NonNegativeReals ) pipe.OUT = Connector() pipe.OUT.add(-pipe.flow, "flow") pipe.OUT.add(pipe.composition, "composition") pipe.OUT.add(pipe.composition['a'], "comp_a") pipe.OUT.add(pipe.pIn, "pressure") os = StringIO() pipe.OUT.pprint(ostream=os) self.assertEqual(os.getvalue(), """OUT : Size=1, Index=None Key : Name : Size : Variable None : comp_a : 1 : composition[a] : composition : 3 : composition : flow : 1 : - flow : pressure : 1 : pIn """) def _IN(m, i): return { 'pressure': pipe.pIn, 'flow': pipe.composition[i] * pipe.flow } pipe.IN = Connector(pipe.SPECIES, rule=_IN) os = StringIO() pipe.IN.pprint(ostream=os) self.assertEqual(os.getvalue(), """IN : Size=3, Index=SPECIES Key : Name : Size : Variable a : flow : 1 : composition[a]*flow : pressure : 1 : pIn b : flow : 1 : composition[b]*flow : pressure : 1 : pIn c : flow : 1 : composition[c]*flow : pressure : 1 : pIn """)
def test_display(self): pipe = ConcreteModel() pipe.SPECIES = Set(initialize=['a','b','c']) pipe.flow = Var(initialize=10) pipe.composition = Var( pipe.SPECIES, initialize=lambda m,i: ord(i)-ord('a') ) pipe.pIn = Var( within=NonNegativeReals, initialize=3.14 ) pipe.OUT = Connector() pipe.OUT.add(-pipe.flow, "flow") pipe.OUT.add(pipe.composition, "composition") pipe.OUT.add(pipe.pIn, "pressure") os = StringIO() pipe.OUT.display(ostream=os) self.assertEqual(os.getvalue(), """OUT : Size=1 Key : Name : Value None : composition : {'a': 0, 'b': 1, 'c': 2} : flow : -10 : pressure : 3.14 """) def _IN(m, i): return { 'pressure': pipe.pIn, 'flow': pipe.composition[i] * pipe.flow } pipe.IN = Connector(pipe.SPECIES, rule=_IN) os = StringIO() pipe.IN.display(ostream=os) self.assertEqual(os.getvalue(), """IN : Size=3 Key : Name : Value a : flow : 0 : pressure : 3.14 b : flow : 10 : pressure : 3.14 c : flow : 20 : pressure : 3.14 """)
def dump_table(conn, tablename, uri, unid=None, stored_unid=None): """ Print extracted information (again, for fun, this is not necessary). Try to print the extracted info in a generic way. If 'user' is specified, filter by that user. """ os = StringIO.StringIO() print >> os, '<h2 id="table-%s">Schema: %s</h2>' % (tablename, tablename) curs = conn.cursor() query, conds = "SELECT * FROM %s" % tablename, [] if unid is not None: conds.append("unid = '%s'" % stored_unid) if conds: query += ' WHERE ' + ' AND '.join(conds) curs.execute(query) if curs.rowcount > 0: print >> os, '<table class="nabu">' print >> os, '<thead><tr>' unidcol = None for i, colname in enumerate(curs.description): if colname[0] == 'unid': unidcol = i if unid is not None: continue print >> os, '<th>%s</th>' % colname[0] print >> os, '</tr></thead>' print >> os, '<tbody>' for row in curs.fetchall(): print >> os, '<tr>' for i, value in enumerate(row): if i == unidcol: if unid is not None: continue else: value = '<a href="%s?id=%s">%s</a>' % (uri, value, value) elif isinstance(value, str) and len(value) > 100: value = value[:30] if isinstance(value, str): value = value.decode('utf-8') print >> os, '<td>%s</td>' % value print >> os, '</tr>' print >> os, '</tbody>' print >> os, '</table>' return os.getvalue()
def test_gams_expanded_connectors(self): m = ConcreteModel() m.x = Var() m.y = Var() m.CON1 = Connector() m.CON1.add(m.x, 'v') m.CON2 = Connector() m.CON2.add(m.y, 'v') m.c = Constraint(expr=m.CON1 + m.CON2 >= 10) TransformationFactory("core.expand_connectors").apply_to(m) m.o = Objective(expr=m.x) os = StringIO() io_options = dict(symbolic_solver_labels=True) m.write(os, format="gams", io_options=io_options) # no error if we're here, but check for some identifying string self.assertIn("x + y", os.getvalue())
def test_expand_empty_indexed(self): m = ConcreteModel() m.x = Var([1, 2], domain=Binary) m.y = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.ECON = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 5) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertIs(m.x[1].domain, m.component('ECON.auto.x')[1].domain) self.assertIs(m.x[2].domain, m.component('ECON.auto.x')[2].domain) self.assertIs(m.y.domain, m.component('ECON.auto.y').domain) self.assertEqual(m.x[1].bounds, m.component('ECON.auto.x')[1].bounds) self.assertEqual(m.x[2].bounds, m.component('ECON.auto.x')[2].bounds) self.assertEqual(m.y.bounds, m.component('ECON.auto.y').bounds) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - ECON.auto.x[1] : 0.0 : True 2 : 0.0 : x[2] - ECON.auto.x[2] : 0.0 : True 3 : 0.0 : y - ECON.auto.y : 0.0 : True """)
def test_indexed_connector(self): m = ConcreteModel() m.x = Var(initialize=1, domain=Reals) m.y = Var(initialize=2, domain=Reals) m.c = Connector([1,2]) m.c[1].add(m.x, name='v') m.c[2].add(m.y, name='v') m.eq = Constraint(expr=m.c[1] == m.c[2]) TransformationFactory('core.expand_connectors').apply_to(m) os = StringIO() m.component('eq.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """eq.expanded : Size=1, Index=eq.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x - y : 0.0 : True """)
def test_expand_empty_indexed(self): m = ConcreteModel() m.x = Var([1,2], domain=Binary) m.y = Var(bounds=(1,3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.ECON = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr= m.CON == m.ECON) m.nocon = Constraint(expr = m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 5) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertIs( m.x[1].domain, m.component('ECON.auto.x')[1].domain ) self.assertIs( m.x[2].domain, m.component('ECON.auto.x')[2].domain ) self.assertIs( m.y.domain, m.component('ECON.auto.y').domain ) self.assertEqual( m.x[1].bounds, m.component('ECON.auto.x')[1].bounds ) self.assertEqual( m.x[2].bounds, m.component('ECON.auto.x')[2].bounds ) self.assertEqual( m.y.bounds, m.component('ECON.auto.y').bounds ) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """c.expanded : Size=3, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - ECON.auto.x[1] : 0.0 : True 2 : 0.0 : x[2] - ECON.auto.x[2] : 0.0 : True 3 : 0.0 : y - ECON.auto.y : 0.0 : True """)
def parseZone(self, line, f): os = StringIO.StringIO() os.write(line) last_line = None for nextline in f: nextline = nextline[:-1] if nextline.startswith("\t") or nextline.startswith(" "): os.write("\n") os.write(nextline) elif nextline.startswith("#") or len(nextline) == 0: continue else: last_line = nextline break zoneitem = zone.Zone() zoneitem.parse(os.getvalue()) self.zones[zoneitem.name] = zoneitem return last_line
def main_todo(args): all_proj_names = [x.name for x in projects_list.get_projects()] proj_names = [] repos = [] if len(args) > 0: projs = [projects_list.get_project(args[0])] else: projs = projects_list.get_projects() tasks = [] for p in projs: p.prepare() proj_names.append(p.name) for repo in p.repos: repos.append(repo) for repo in repos: full_path = projects_list.resolve_dirname(repo) new_tasks = todo_app.find_tasks(full_path, repo) new_tasks = [ t for t in new_tasks if t.extra is None or t.extra not in all_proj_names or t.extra in proj_names ] tasks += new_tasks os = StringIO() todo_app.dump_tasks(tasks, os) todo_str = os.getvalue() if len(args) > 0: projs[0].put_field('todo', todo_str) projs[0].dump_file_content() return 0 print('Looking for tasks in {}:'.format(', '.join(repos))) print(todo_str) return 0
def render_index(uri, username, srcstore): """ Generate an index of documents. """ os = StringIO.StringIO() linkfmt = u'%s?id=%%s&view=%%s' % uri print >> os, pages_header print >> os, u''' <h1>Nabu Database Contents</h1> <a href="%s?view=extracted">[view all extracted]</a> <p id="desc"> This is meant to be used for debugging only. You should build a suitable presentation interface from the extracted data. </p> ''' % uri print >> os, '<table width="100%" cellpadding="0" class="dump nabu">' sr = srcstore.get(username, None, ('unid', 'filename', 'username', 'time', 'errors',)) for s in sr: print >> os, '<tr>' print >> os, '<td><a href="%s">%s</a></td>' % (linkfmt % (s['unid'], 'source'), s['unid']) print >> os, '<td>' print >> os, ' <a href="%s">S</a>' % linkfmt % (s['unid'], 'source') print >> os, ' <a href="%s">E</a>' % linkfmt % (s['unid'], 'extracted') print >> os, ' <a href="%s">H</a>' % linkfmt % (s['unid'], 'html') print >> os, '</td>' print >> os, '<td>%s</td>' % s['filename'] print >> os, '<td>%s</td>' % s['username'] print >> os, '<td>%s</td>' % s['time'] print >> os, '<td>%s</td>' % (s['errors'] and 'ERRORS' or ' ') print >> os, '</tr>' print >> os, '</table>' print >> os, pages_footer return os.getvalue()
def __makeGraphViz(self): # :return: graphviz code representing the the link occupancy of the routes # added so far import sys, os # this is not thread safe... sys.path.append( os.path.expanduser("~aholz/DAQTools/Diagnostics/trunk/network/")) import drawIBclos sys.path.pop(-1) import utils # get the LIDs of spine and leaf switches leafSwitches, spineSwitches = utils.findSwitchLIDs(self.linkData) # convert to device names leafSwitches = [ self.linkData.getDeviceName(lid) for lid in leafSwitches ] spineSwitches = [ self.linkData.getDeviceName(lid) for lid in spineSwitches ] # get the full mapping of peer devices attached to each port of each switch for lid in self.linkData.switchLIDs: switchData = self.linkData.getSwitchDataFromLID(lid) closDrawer = drawIBclos.ClosDrawer( spineSwitches, leafSwitches, self.linkData.getSwitchToPCmapping(), self.linkData.getPeerDeviceMap()) # keep a list of all edges so that we can determine the maximum # occupancy before adding them (to determine the scaling # of occupancy to pen width) edges = [] # take occupancies from the OccupancyTable object occupancyTable = self.occupancyTableMainRoutes #---------- # source host to input leaf occupancies #---------- for key, occ in occupancyTable.sourceToInputLeafSwitchOccupancy.getItems( ): # key is sourceLid edges.append(dict(sourceLid=key, port=1, occupancy=occ)) #---------- # output leaf switch to destination PC occupancy #---------- for key, occ in occupancyTable.outputLeafSwitchToDestOccupancy.getItems( ): # key is (outputLeafSwitchLID, outputLeafSwitchPort) edges.append(dict(sourceLid=key[0], port=key[1], occupancy=occ)) #---------- # input leaf switch to spine switch occupancy #---------- for key, occ in occupancyTable.inputLeafSwitchLIDandPortToNumRoutes.getItems( ): # key is (inputLeafSwitchLID, port) edges.append(dict(sourceLid=key[0], port=key[1], occupancy=occ)) #---------- # spine switch to output leaf switch occupancy #---------- for key, occ in occupancyTable.spineSwitchLIDandPortToNumRoutes.getItems( ): # key is (spineSwitchLID, port) edges.append(dict(sourceLid=key[0], port=key[1], occupancy=occ)) #---------- # determine maximum occupancy #---------- maxOcc = max([edge['occupancy'] for edge in edges]) #---------- # determine conversion from occupancy to pen width #---------- maxPenWidth = 7 # pen width for low or no traffic minPenWidth = 0.1 penWidthScaling = drawIBclos.PenWidthScaling( minPenWidth, maxPenWidth, 1, # minimum occupancy maxOcc) #---------- # make default edges dotted #---------- for item in closDrawer.edges.values(): for edge in item.values(): edge['attrs'] = ['style=dotted'] #---------- # add edges to clos drawer #---------- for edge in edges: sourceDeviceName = self.linkData.getDeviceName(edge['sourceLid']) # determine graphviz attributes attrs = penWidthScaling.makeEdgeAttributes(edge['occupancy']) closDrawer.edges[sourceDeviceName][edge['port']]['attrs'] = attrs # the output buffer to write to import StringIO os = StringIO.StringIO() # generate the graphviz code closDrawer.doPrint(os) return os.getvalue()
def test_all_derived_class_oldAPI(self): def name(x): if type(x) in nonpyomo_leaf_types: return str(x) else: return x.name class all_callbacks(StreamBasedExpressionVisitor): def __init__(self): self.ans = [] super(all_callbacks, self).__init__() def enterNode(self, node): self.ans.append("Enter %s" % (name(node))) def exitNode(self, node, data): self.ans.append("Exit %s" % (name(node))) def beforeChild(self, node, child): self.ans.append("Before %s (from %s)" % (name(child), name(node))) def acceptChildResult(self, node, data, child_result): self.ans.append("Accept into %s" % (name(node))) def afterChild(self, node, child): self.ans.append("After %s (from %s)" % (name(child), name(node))) def finalizeResult(self, result): self.ans.append("Finalize") os = StringIO() with LoggingIntercept(os, 'pyomo'): walker = all_callbacks() self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the " "beforeChild() method", os.getvalue().replace('\n', ' ')) self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the " "acceptChildResult() method", os.getvalue().replace('\n', ' ')) self.assertIn( "Note that the API for the StreamBasedExpressionVisitor " "has changed to include the child index for the " "afterChild() method", os.getvalue().replace('\n', ' ')) self.assertIsNone(walker.walk_expression(self.e)) self.assertEqual( "\n".join(walker.ans), """Enter sum Before pow (from sum) Enter pow Before x (from pow) Enter x Exit x Accept into pow After x (from pow) Before 2 (from pow) Enter 2 Exit 2 Accept into pow After 2 (from pow) Exit pow Accept into sum After pow (from sum) Before y (from sum) Enter y Exit y Accept into sum After y (from sum) Before prod (from sum) Enter prod Before z (from prod) Enter z Exit z Accept into prod After z (from prod) Before sum (from prod) Enter sum Before x (from sum) Enter x Exit x Accept into sum After x (from sum) Before y (from sum) Enter y Exit y Accept into sum After y (from sum) Exit sum Accept into prod After sum (from prod) Exit prod Accept into sum After prod (from sum) Exit sum Finalize""")
table.addFooter(( "Total:", totalRequests, totalAverage, totalResponseTime / totalRequests, totalResponseWithoutWRTime / totalRequests, maxResponseTime, float(totalSlots) / totalRequests, minStartTime, maxEndTime, "", )) os = StringIO() table.printTable(os=os) print os.getvalue() if enableListenQueue: lqlatency = (lqssl / avgRequests, lqnon / avgRequests,) if avgRequests else (0.0, 0.0,) print " listenq latency (ssl+non): %.1f s %.1f s" % ( lqlatency[0], lqlatency[1], ) table = tables.Table() table.addHeader( ("", "<10ms", "10ms<->100ms", "100ms<->1s", "1s<->10s", "10s<->30s", "30s<->60s", ">60s", "Over 1s", "Over 10s"), ) table.setDefaultColumnFormats( ( tables.Table.ColumnFormat("%s", tables.Table.ColumnFormat.CENTER_JUSTIFY),
def save(self): os = BytesIO() writeU8(os, 25) # Version #flags flags = 0x00 if self.pos[1] < -1: flags |= 0x01 #is_underground flags |= 0x02 #day_night_differs flags |= 0x04 #lighting_expired flags |= 0x08 #generated writeU8(os, flags) writeU8(os, 2) # content_width writeU8(os, 2) # params_width cbuffer = BytesIO() # Bulk node data content = self.content k = 0 nimap = {} rev_nimap = [] first_free_content = 0 for z in range(16): for y in range(16): for x in range(16): #writeU16(cbuffer, content[k]) c = content[k] if c in nimap: writeU16(cbuffer, nimap[c]) else: nimap[c] = first_free_content writeU16(cbuffer, first_free_content) rev_nimap.append(c) first_free_content += 1 k += 1 k += (256-16) k += (16-16*256) param1 = self.param1 k = 0 for z in range(16): for y in range(16): for x in range(16): writeU8(cbuffer, param1[k]) k += 1 k += (256-16) k += (16-16*256) param2 = self.param2 k = 0 for z in range(16): for y in range(16): for x in range(16): writeU8(cbuffer, param2[k]) k += 1 k += (256-16) k += (16-16*256) os.write(zlib.compress(cbuffer.getvalue())) # Nodemeta meta = self.metadata cbuffer = BytesIO() writeU8(cbuffer, 1) # Version writeU16(cbuffer, len(meta)) for pos, data in meta.items(): writeU16(cbuffer, (pos[2]<<8)|(pos[1]<<4)|pos[0]) writeU32(cbuffer, len(data[0])) for name, val in data[0].items(): writeString(cbuffer, name) writeLongString(cbuffer, str(val)) serialize_inv(cbuffer, data[1]) os.write(zlib.compress(cbuffer.getvalue())) # Static objects writeU8(os, 0) # Version writeU16(os, 0) # Number of objects # Timestamp writeU32(os, 0xffffffff) # BLOCK_TIMESTAMP_UNDEFINED # Name-ID mapping writeU8(os, 0) # Version writeU16(os, len(rev_nimap)) for i in range(len(rev_nimap)): writeU16(os, i) writeString(os, self.name_id_mapping[rev_nimap[i]]) # Node timer writeU8(os, 2+4+4) # Timer data len writeU16(os, len(self.timers)) # Number of timers if len(self.timers) > 0: logger.info('wrote ' + str(len(self.timers)) + ' node timers') for i in range(len(self.timers)): writeU16(os, self.timers[i][0]) writeU32(os, self.timers[i][1]) writeU32(os, self.timers[i][2]) return os.getvalue()
def test_varlist_aggregator(self): m = ConcreteModel() m.flow = VarList() m.phase = Var(bounds=(1,3)) m.CON = Connector() m.CON.add( m.flow, aggregate=lambda m,v: sum(v[i] for i in v) == 0 ) m.CON.add(m.phase) m.ECON2 = Connector() m.ECON1 = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr= m.CON == m.ECON1) m.d = Constraint(expr= m.ECON2 == m.CON) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 7) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) self.assertEqual( len(m.flow), 2 ) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """c.expanded : Size=2, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : flow[1] - ECON1.auto.flow : 0.0 : True 2 : 0.0 : phase - ECON1.auto.phase : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual(os.getvalue(), """d.expanded : Size=2, Index=d.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : ECON2.auto.flow - flow[2] : 0.0 : True 2 : 0.0 : ECON2.auto.phase - phase : 0.0 : True """) os = StringIO() m.component('CON.flow.aggregate').pprint(ostream=os) self.assertEqual(os.getvalue(), """CON.flow.aggregate : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : 0.0 : flow[1] + flow[2] : 0.0 : True """) os = StringIO() m.CON.pprint(ostream=os) self.assertEqual(os.getvalue(), """CON : Size=1, Index=None Key : Name : Size : Variable None : flow : * : flow : phase : 1 : phase """)
def test_varlist_aggregator(self): m = ConcreteModel() m.flow = VarList() m.phase = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.flow, aggregate=lambda m, v: sum(v[i] for i in v) == 0) m.CON.add(m.phase) m.ECON2 = Connector() m.ECON1 = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON1) m.d = Constraint(expr=m.ECON2 == m.CON) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 7) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) self.assertEqual(len(m.flow), 2) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=2, Index=c.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : flow[1] - ECON1.auto.flow : 0.0 : True 2 : 0.0 : phase - ECON1.auto.phase : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """d.expanded : Size=2, Index=d.expanded_index, Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : ECON2.auto.flow - flow[2] : 0.0 : True 2 : 0.0 : ECON2.auto.phase - phase : 0.0 : True """) os = StringIO() m.component('CON.flow.aggregate').pprint(ostream=os) self.assertEqual( os.getvalue(), """CON.flow.aggregate : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : 0.0 : flow[1] + flow[2] : 0.0 : True """) os = StringIO() m.CON.pprint(ostream=os) self.assertEqual( os.getvalue(), """CON : Size=1, Index=None Key : Name : Size : Variable None : flow : * : flow : phase : 1 : phase """)
table.addFooter(( "Total:", totalRequests, totalAverage, totalResponseTime / totalRequests, totalResponseWithoutWRTime / totalRequests, maxResponseTime, float(totalSlots) / totalRequests, minStartTime, maxEndTime, "", )) os = StringIO() table.printTable(os=os) print(os.getvalue()) if enableListenQueue: lqlatency = (lqssl / avgRequests, lqnon / avgRequests,) if avgRequests else (0.0, 0.0,) print(" listenq latency (ssl+non): %.1f s %.1f s" % ( lqlatency[0], lqlatency[1], )) table = tables.Table() table.addHeader( ("", "<10ms", "10ms<->100ms", "100ms<->1s", "1s<->10s", "10s<->30s", "30s<->60s", ">60s", "Over 1s", "Over 10s"), ) table.setDefaultColumnFormats( ( tables.Table.ColumnFormat("%s", tables.Table.ColumnFormat.CENTER_JUSTIFY),
def test_expand_implicit_indexed(self): m = ConcreteModel() m.x = Var([1, 2], domain=Binary) m.y = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.a2 = Var([1, 2]) m.b1 = Var() m.ECON2 = Connector(implicit=['x']) m.ECON2.add(m.b1, 'y') m.ECON1 = Connector(implicit=['y']) m.ECON1.add(m.a2, 'x') # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON1) m.d = Constraint(expr=m.ECON2 == m.CON) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 3) os = StringIO() m.ECON1.pprint(ostream=os) self.assertEqual( os.getvalue(), """ECON1 : Size=1, Index=None Key : Name : Size : Variable None : x : 2 : a2 : y : - : None """) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() os = StringIO() m.ECON1.pprint(ostream=os) self.assertEqual( os.getvalue(), """ECON1 : Size=1, Index=None Key : Name : Size : Variable None : x : 2 : a2 : y : 1 : 'ECON1.auto.y' """) self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 9) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index='c.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - a2[1] : 0.0 : True 2 : 0.0 : x[2] - a2[2] : 0.0 : True 3 : 0.0 : y - 'ECON1.auto.y' : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """d.expanded : Size=3, Index='d.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : 'ECON2.auto.x'[1] - x[1] : 0.0 : True 2 : 0.0 : 'ECON2.auto.x'[2] - x[2] : 0.0 : True 3 : 0.0 : b1 - y : 0.0 : True """)
def save(self): os = BytesIO() writeU8(os, 25) # Version #flags flags = 0x00 if self.pos[1] < -1: flags |= 0x01 #is_underground flags |= 0x02 #day_night_differs flags |= 0x04 #lighting_expired flags |= 0x08 #generated writeU8(os, flags) writeU8(os, 2) # content_width writeU8(os, 2) # params_width cbuffer = BytesIO() # Bulk node data content = self.content k = 0 nimap = {} rev_nimap = [] first_free_content = 0 for z in range(16): for y in range(16): for x in range(16): #writeU16(cbuffer, content[k]) c = content[k] if c in nimap: writeU16(cbuffer, nimap[c]) else: nimap[c] = first_free_content writeU16(cbuffer, first_free_content) rev_nimap.append(c) first_free_content += 1 k += 1 k += (256-16) k += (16-16*256) param1 = self.param1 k = 0 for z in range(16): for y in range(16): for x in range(16): writeU8(cbuffer, param1[k]) k += 1 k += (256-16) k += (16-16*256) param2 = self.param2 k = 0 for z in range(16): for y in range(16): for x in range(16): writeU8(cbuffer, param2[k]) k += 1 k += (256-16) k += (16-16*256) os.write(zlib.compress(cbuffer.getvalue())) # Nodemeta meta = self.metadata cbuffer = BytesIO() writeU8(cbuffer, 1) # Version writeU16(cbuffer, len(meta)) for pos, data in meta.items(): writeU16(cbuffer, (pos[2]<<8)|(pos[1]<<4)|pos[0]) writeU32(cbuffer, len(data[0])) for name, val in data[0].items(): writeString(cbuffer, name) writeLongString(cbuffer, str(val)) serialize_inv(cbuffer, data[1]) os.write(zlib.compress(cbuffer.getvalue())) # Static objects writeU8(os, 0) # Version writeU16(os, 0) # Number of objects # Timestamp writeU32(os, 0xffffffff) # BLOCK_TIMESTAMP_UNDEFINED # Name-ID mapping writeU8(os, 0) # Version writeU16(os, len(rev_nimap)) for i in range(len(rev_nimap)): writeU16(os, i) writeString(os, self.name_id_mapping[rev_nimap[i]]) # Node timer writeU8(os, 2+4+4) # Timer data len writeU16(os, len(self.timers)) # Number of timers if len(self.timers) > 0: print('wrote ' + str(len(self.timers)) + ' node timers') for i in range(len(self.timers)): writeU16(os, self.timers[i][0]) writeU32(os, self.timers[i][1]) writeU32(os, self.timers[i][2]) return os.getvalue()
table.addFooter(( "Total:", totalRequests, totalAverage, totalResponseTime / totalRequests, totalResponseWithoutWRTime / totalRequests, maxResponseTime, float(totalSlots) / totalRequests, minStartTime, maxEndTime, "", )) os = StringIO() table.printTable(os=os) print(os.getvalue()) if enableListenQueue: lqlatency = ( lqssl / avgRequests, lqnon / avgRequests, ) if avgRequests else ( 0.0, 0.0, ) print(" listenq latency (ssl+non): %.1f s %.1f s" % ( lqlatency[0], lqlatency[1], )) table = tables.Table()