def load_eqn_template(problem_data, template_file, renderer=pystache.Renderer(escape=lambda u: u)): """Loads pystache template and uses it to generate code. Parameters ---------- problem_data - dict Workspace defining variables for template template_file - str Path to template file to be used renderer Renderer used to convert template file to code Returns ------- Code generated from template """ template_path = beluga.root( ) + '/optimlib/templates/' + problem_data['method'] + '/' + template_file with open(template_path) as f: tmpl = f.read() # Render the template using the data code = renderer.render(tmpl, problem_data) return code
# For security module.__dict__.update({'__builtin__': {}}) exec(code_string, module.__dict__) return getattr(module, function_name) PythonCodeGen = sp.Workflow( [ # Create module for holding compiled code sp.Task(ft.partial(create_module), inputs='problem_data', outputs=('code_module')), # Load equation template files and generate code sp.Task(ft.partial(load_eqn_template, template_file=beluga.root() + '/optimlib/templates/brysonho/deriv_func.py.mu'), inputs='problem_data', outputs='deriv_func_code'), sp.Task(ft.partial(load_eqn_template, template_file=beluga.root() + '/optimlib/templates/brysonho/bc_func.py.mu'), inputs='problem_data', outputs='bc_func_code'), sp.Task( ft.partial(load_eqn_template, template_file=beluga.root() + '/optimlib/templates/brysonho/compute_control.py.mu'), inputs='problem_data', outputs='compute_control_code'),