def algo2cyml(self): code = 'from datetime import datetime\nfrom math import *\n' tab = ' ' * 4 #print(self.meta_inp(self.name)) for mod in self.model.model: code += 'from %s import model_%s\n' % ( signature(mod).capitalize(), signature(mod) ) if mod.package_name is None else 'from %s import model_%s\n' % ( signature(mod), signature(mod)) name = self.model.name.strip().replace(' ', '_').lower() signature_mod = "def model_" + name + "(%s):" % (",\n ".join( map(my_input, self.meta_inp(self.name)))) code += signature_mod + "\n" code += self.decl(defa=False) lines = [tab + l for l in self.algorithm().split('\n') if l.split()] code += '\n'.join(lines) code += "\n" + tab + "return " + ", ".join( [out.name for out in self.model.outputs]) out_states = [ out for out in self.model.outputs if out.variablecategory == "state" ] if self.model.initialization: file_init = self.model.initialization[0].filename path_init = Path(os.path.join(self.pkg, "crop2ml", file_init)) with open(path_init, 'r') as f: code_init = f.read() if code_init is not None: lines = [tab + l for l in code_init.split('\n') if l.split()] code += self.generate_function_signature(self.model) + '\n' code += self.val_init(self.model) code += '\n'.join(lines) code += '\n' + tab + 'return ' + ', '.join( [o.name for o in out_states]) + '\n' return code
def visit_function_definition(self, node): self.newline(extra=1) self.newline(node) self.write('def %s(' % node.name) for i, pa in enumerate(node.params): #if pa.type == "local": self.write(pa.name) if "value" in dir(pa) or "elements" in dir(pa) or "pairs" in dir(pa) : #self.write(pa.name) self.write(" = ") self.visit(pa) if i!= (len(node.params)-1): self.write(',\n ') self.write('):') self.newline(node) if self.model and node.name.split("model_")[1]==signature(self.model): self.write(' """\n') self.write(self.doc.desc) self.newline(node) self.write(self.doc.inputs_doc) self.newline(node) self.write(self.doc.outputs_doc) self.newline(node) self.write(' """\n') self.newline(node) self.model = None self.body(node.block)
def generate_function_signature(self, model): print(model.name) input = model.inputs output = model.outputs inout = input + output outname = [o.name for o in output] tab = [] # initialization inputs for inp in inout: if inp.name not in outname: if inp.name not in tab: tab.append(inp) if sys.version_info[0] >= 3: init_inp = tab.copy() else: init_inp = tab # Compute name from title. # We need an explicit name rather than infering it from Title #name = desc.Title code = '\n\ndef init_%s(' % (signature(model)) code_size = len(code) #_input_names = [inp.name.lower() for inp in inputs] ins = [my_input(inp) for inp in init_inp] separator = ',\n' + code_size * ' ' code += separator.join(ins) code += '):\n' return code
def visit_function_definition(self, node): self.newline(extra=1) self.newline(node) self.funcname = node.name self.write('%s <- function (' % node.name) for i, pa in enumerate(node.params): self.write(pa.name) if "value" in dir(pa) or "elements" in dir(pa) or "pairs" in dir( pa): self.write(" = ") self.visit(pa) if i != (len(node.params) - 1): self.write(',\n ') self.write('){') self.newline(node) if self.model and node.name.split("model_")[1] == signature( self.model): self.write(self.doc.header) self.newline(node) self.write(self.doc.desc) self.newline(node) self.write(self.doc.inputs_doc) self.newline(node) self.write(self.doc.outputs_doc) self.newline(node) self.model = None self.body(node.block) self.newline(node) self.write("}")
def generate_function_signature(self, model): tab = [] # initialization inputs for inp in model.inputs: if "variablecategory" in dir(inp) and inp.variablecategory in ( "auxiliary"): tab.append(inp) if "parametercategory" in dir(inp): tab.append(inp) init_inp = tab.copy() inputs = init_inp # Compute name from title. # We need an explicit name rather than infering it from Title #name = desc.Title code = '\n\ndef init_%s(' % (signature(model)) code_size = len(code) #_input_names = [inp.name.lower() for inp in inputs] ins = [my_input(inp) for inp in inputs] separator = ',\n' + code_size * ' ' code += separator.join(ins) code += '):\n' return code