#NEWLINE# def build_python( xmile_file ) : #NEWLINE# #INDENT# ' Load the xml file and pass the relevant elements to the builder class ' #NEWLINE# smile_parser = SMILEParser ( ) #NEWLINE# xml_parser = etree . XMLParser ( encoding = 'utf-8' , recover = True ) #NEWLINE# root = etree . parse ( xmile_file , parser = xml_parser ) . getroot ( ) #NEWLINE# NS = root . nsmap . values ( ) [ 0 ] #NEWLINE# filename = ( '.' . join ( xmile_file . split ( '.' ) [ : ( - 1 ) ] ) + '.py' ) #NEWLINE# builder . new_model ( filename ) #NEWLINE# flaux_xpath = '//ns:model/ns:variables/ns:aux|//ns:model/ns:variables/ns:flow' #NEWLINE# for element in root . xpath ( flaux_xpath , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) : #NEWLINE# #INDENT# identifier = smile_parser . parse ( element . attrib [ 'name' ] , context = 'defn' ) #NEWLINE# pyeqn = smile_parser . parse ( element . xpath ( 'ns:eqn' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) [ 0 ] . text ) #NEWLINE# builder . add_flaux ( filename , identifier , pyeqn ) #NEWLINE# #UNINDENT# stock_xpath = '//ns:model/ns:variables/ns:stock' #NEWLINE# for element in root . xpath ( stock_xpath , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) : #NEWLINE# #INDENT# identifier = smile_parser . parse ( element . attrib [ 'name' ] , context = 'defn' ) #NEWLINE# inflows = [ smile_parser . parse ( e . text ) for e in element . xpath ( 'ns:inflow' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) ] #NEWLINE# outflows = [ smile_parser . parse ( e . text ) for e in element . xpath ( 'ns:outflow' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) ] #NEWLINE# pyeqn = ( ' + ' . join ( inflows ) if inflows else '' ) #NEWLINE# pyeqn += ( ( ' - ' + ' - ' . join ( outflows ) ) if outflows else '' ) #NEWLINE# initial_value = smile_parser . parse ( element . xpath ( 'ns:eqn' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) [ 0 ] . text ) #NEWLINE# builder . add_stock ( filename , identifier , pyeqn , initial_value ) #NEWLINE# #UNINDENT# tstart = smile_parser . parse ( root . xpath ( '//ns:sim_specs/ns:start' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) [ 0 ] . text ) #NEWLINE# builder . add_flaux ( filename , 'initial_time' , tstart ) #NEWLINE# tstop = smile_parser . parse ( root . xpath ( '//ns:sim_specs/ns:stop' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) [ 0 ] . text ) #NEWLINE# builder . add_flaux ( filename , 'final_time' , tstop ) #NEWLINE# dt = smile_parser . parse ( root . xpath ( '//ns:sim_specs/ns:dt' , namespaces = { #NEWLINE# #INDENT# 'ns' : NS , #NEWLINE# #UNINDENT# } ) [ 0 ] . text ) #NEWLINE# builder . add_flaux ( filename , 'time_step' , dt ) #NEWLINE# return filename