本文整理汇总了Python中freeode.interpreter.Interpreter.get_compiled_objects方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.get_compiled_objects方法的具体用法?Python Interpreter.get_compiled_objects怎么用?Python Interpreter.get_compiled_objects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freeode.interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.get_compiled_objects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_StatementVisitor__visit_NodeCompileStmt__code_generation_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_StatementVisitor__visit_NodeCompileStmt__code_generation_1(): #IGNORE:C01111
#py.test.skip('Test StatementVisitor.visit_NodeCompileStmt')
print 'Test StatementVisitor.visit_NodeCompileStmt:'
from freeode.interpreter import (Interpreter, IFloat, SimlFunction)
from freeode.ast import (DotName)
prog_text = \
'''
class A:
data b: Float
func dynamic(this):
b = 2
compile A
'''
#create the interpreter
intp = Interpreter()
#run program
intp.interpret_module_string(prog_text, None, 'test')
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#there must be one compiled object present
assert len(intp.get_compiled_objects()) == 1
comp_obj = intp.get_compiled_objects()[0]
#the attributes b and dynamic must exist
assert isinstance(comp_obj.get_attribute(DotName('b')), IFloat)
assert isinstance(comp_obj.get_attribute(DotName('dynamic')), SimlFunction)
assert len(comp_obj.get_attribute(DotName('dynamic')).statements) == 1
示例2: test_ProgramGenerator__create_program_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_ProgramGenerator__create_program_2():
msg = \
'''
Test ProgramGenerator.create_program:
Test program with additional initialization function.
Load program as module and test init_*** function.
'''
#skip_test(msg)
print msg
import os
from freeode.pygenerator import ProgramGenerator
from freeode.interpreter import Interpreter
prog_text = \
'''
class A:
data x: Float
data b: Float param
#This is the additional initialization function.
func init_b(this, in_b):
b = in_b #set parameter
x = 0 #set initial value
#10
func initialize(this):
b = 0.1 #set parameter
x = 0 #set initial value
func dynamic(this):
$x = b
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
#create the output text
pg = ProgramGenerator()
pg.create_program('foo.siml', intp.get_compiled_objects())
#print pg.get_buffer()
#write the buffer into a file, import the file as a module
progname = 'testprog_ProgramGenerator__create_program_2'
prog_text_file = open(progname + '.py','w')
prog_text_file.write(pg.get_buffer())
prog_text_file.close()
module = __import__(progname)
#test the generated module
A = module.A
a = A()
#call generated init_b(...) function
a.init_b(42)
assert a.param.b == 42
#clean up
os.remove(progname + '.py')
os.remove(progname + '.pyc')
示例3: test_graph_function_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_graph_function_1(): #IGNORE:C01111
#py.test.skip('Test the print function. - code generation for: user defined class.')
print 'Test the print function. - code generation for: user defined class.'
from freeode.interpreter import Interpreter
from freeode.ast import DotName
prog_text = \
'''
class A:
data c: Float
func final(this):
graph(c)
compile A
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
# print 'module after interpreter run: ---------------------------------'
# print intp.modules['test']
#get flattened object
sim = intp.get_compiled_objects()[0]
#print sim
#get the final function with the generated code
final = sim.get_attribute(DotName('final'))
assert len(final.statements) == 1
示例4: test_ProgramGenerator__create_program
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_ProgramGenerator__create_program():
msg = ''' Test ProgramGenerator.create_program:
Just see if function does not crash.
'''
#py.test.skip(msg)
print msg
from freeode.pygenerator import ProgramGenerator
from freeode.interpreter import Interpreter
prog_text = \
'''
class A:
data a: Float
data b: Float param
func initialize(this):
b = 1
func dynamic(this):
$a = b
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
#create the output text
pg = ProgramGenerator()
pg.create_program('foo.siml', intp.get_compiled_objects())
print pg.get_buffer()
示例5: test_ProgramGenerator__create_program_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_ProgramGenerator__create_program_1():
msg = \
'''
Test ProgramGenerator.create_program:
Test basic functions of the compiler. Loads generated program as module.
'''
#skip_test(msg)
print msg
import os
from freeode.pygenerator import ProgramGenerator
from freeode.interpreter import Interpreter
prog_text = \
'''
class A:
data x: Float
data b: Float param
func initialize(this):
x = 0
b = 1
solution_parameters(duration = 30, reporting_interval = 0.1)
func dynamic(this):
$x = b
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
#create the output text
pg = ProgramGenerator()
pg.create_program('foo.siml', intp.get_compiled_objects())
#print pg.get_buffer()
#write the buffer into a file, import the file as a module
progname = 'testprog_ProgramGenerator__create_program_1'
prog_text_file = open(progname + '.py','w')
prog_text_file.write(pg.get_buffer())
prog_text_file.close()
module = __import__(progname)
#test the generated module
A = module.A
a = A()
#call generated initialize(...) function
a.initialize()
assert a.param.b == 1
#solve (trivial) ODE and test solution
a.simulateDynamic()
x_vals = a.getResults()['x']
assert abs(x_vals[-1] - 30) < 1e-6
#clean up
os.remove(progname + '.py')
os.remove(progname + '.pyc')
示例6: test_unknown_const_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_unknown_const_1(): #IGNORE:C01111
msg = '''Test correct treatment of unknown constants.'''
py.test.skip(msg)
print msg
from freeode.optimizer import MakeDataFlowDecorations, DataFlowChecker
from freeode.interpreter import (Interpreter, IFloat)
from freeode.ast import DotName, NodeAssignment
prog_text = \
'''
data c1: Float const
class A:
data a, b: Float
data c2: Float const
func dynamic(this):
a = c1
b = c2
compile A
'''
#interpret the program
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
#the module
#mod = intp.modules['test']
#print mod
#get the flattened version of the A class
sim = intp.get_compiled_objects()[0]
#print sim
#get attributes
a = sim.get_attribute(DotName('a'))
b = sim.get_attribute(DotName('b'))
# c = sim.get_attribute(DotName('c1'))
# c = sim.get_attribute(DotName('c2'))
#get generated main function
dyn = sim.get_attribute(DotName('dynamic'))
hexid = lambda x: hex(id(x))
print 'a:', hexid(a), ' b:', hexid(b)#, ' c2:', hexid(c)
#create the input and output decorations on each statement of the
#function
dd = MakeDataFlowDecorations()
dd.decorate_simulation_object(sim)
#check data flow of all functions
fc = DataFlowChecker()
fc.set_sim_object(sim)
assert False, 'This program should raise an exceptions because unknown const attributes were used'
示例7: test_interpreter_dollar_operator_2
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_interpreter_dollar_operator_2(): #IGNORE:C01111
msg = '''
Test "$" operator.
Bug: $ operator did not work with attributes of user defined classes.
Background: Class instantiation did not get parent refferences right.
'''
#py.test.skip(msg)
print msg
from freeode.interpreter import Interpreter, CallableObject, IFloat
from freeode.ast import DotName, RoleStateVariable, RoleTimeDifferential
prog_text = \
'''
class A:
data z: Float
func dynamic(this):
$z = z
class B:
data a: A
func dynamic(this):
a.dynamic()
compile B
'''
#create the interpreter
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
print
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#TODO: Assertions
#get flattened object
sim = intp.get_compiled_objects()[0]
#get the attributes that we have defined
az = sim.get_attribute(DotName('a.z'))
az_dt = sim.get_attribute(DotName('a.z$time')) #implicitly defined by $ operator
dynamic = sim.get_attribute(DotName('dynamic'))
#test some facts about the attributes
assert isinstance(az, IFloat) #a1 is state variable, because it
assert az.role == RoleStateVariable #has derivative
assert isinstance(az_dt, IFloat)
assert az_dt.role == RoleTimeDifferential # $a1 is time differential
assert isinstance(dynamic, CallableObject)
#test if assignment really is 'a1$time' = 'a1'
assign = dynamic.statements[0]
assert assign.target is az_dt
assert assign.expression is az
示例8: test_function_return_value_roles_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_function_return_value_roles_1(): #IGNORE:C01111
'''
User defined functions can be called from constant and from variable
environments. Test the roles of their return values.
This test only involves fundamental types.
'''
#py.test.skip('Test roles of return values of user defined functions.')
print 'Test roles of return values of user defined functions.'
from freeode.interpreter import Interpreter
from freeode.ast import (DotName, RoleConstant, RoleAlgebraicVariable)
prog_text = \
'''
func plus2(x):
data r: Float
r = x + 2
return r
data ac,bc: Float const
ac = 3
bc = plus2(ac)
class B:
data av,bv: Float
func dynamic(this):
bv = plus2(av)
compile B
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
mod = intp.modules['test']
# print 'module after interpreter run: ---------------------------------'
# print mod
ac = mod.get_attribute(DotName('ac'))
bc = mod.get_attribute(DotName('bc'))
assert ac.role == RoleConstant
assert bc.role == RoleConstant
assert ac.value == 3
assert bc.value == 5
# #get flattened object
sim = intp.get_compiled_objects()[0]
# print 'Flattened object: ---------------------------------'
# print sim
av = sim.get_attribute(DotName('av'))
bv = sim.get_attribute(DotName('bv'))
assert av.role == RoleAlgebraicVariable
assert bv.role == RoleAlgebraicVariable
示例9: test_compile_statement_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_compile_statement_1(): #IGNORE:C01111
msg = '''
Test the compile statement
- Flattening and storage of functions' local variables'''
#py.test.skip(msg)
print msg
from freeode.interpreter import Interpreter, IFloat, CallableObject
from freeode.ast import DotName
prog_text = \
'''
class A:
data a1: Float
func foo(this, x):
return x
func dynamic(this):
data b,c: Float
b = foo(a1)
c = foo(a1 + b)
$a1 = b
compile A
'''
#create the interpreter
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
print
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#get flattened object
sim = intp.get_compiled_objects()[0]
#get the attributes that we have defined
a1 = sim.get_attribute(DotName('a1'))
a1_dt = sim.get_attribute(DotName('a1$time'))
dynamic = sim.get_attribute(DotName('dynamic'))
#test some facts about the attributes
assert isinstance(a1, IFloat)
assert isinstance(a1_dt, IFloat)
assert isinstance(dynamic, CallableObject)
#check number of attributes, most are automatically generated
#attributes: initialize, dynamic, final,
#instance variables: a1, $a1,
#local variables: A.dynamic.b, A.dynamic.c,
#intermediate result: A.foo.x, (2nd call)
assert len(sim.attributes) == 8
示例10: test_user_defined_class_roles_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_user_defined_class_roles_1(): #IGNORE:C01111
'''
The role keywords (const, param, variable, ...) should work with user
defined classes too.
'''
#py.test.skip('Test user defined classes with different roles.')
print 'Test user defined classes with different roles.'
from freeode.interpreter import Interpreter
from freeode.ast import (DotName, RoleConstant, RoleAlgebraicVariable)
prog_text = \
'''
class A:
data a: Float
#use the class as a constant
data ac: A const
ac.a = 2
class B:
#use the class as a variable
data av: A
data v: Float
func dynamic(this):
av.a = v
compile B
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
mod = intp.modules['test']
# print 'module after interpreter run: ---------------------------------'
# print mod
ac = mod.get_attribute(DotName('ac'))
a = ac.get_attribute(DotName('a'))
assert a.role == RoleConstant
# #get flattened object
sim = intp.get_compiled_objects()[0]
# print 'Flattened object: ---------------------------------'
# print sim
av_a = sim.get_attribute(DotName('av.a'))
assert av_a.role == RoleAlgebraicVariable
示例11: test_interpreter_expression_statement_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_interpreter_expression_statement_1(): #IGNORE:C01111
'''
Unevaluated expressions also generate code.
'''
#py.test.skip('Test expression statement - code generation.')
print 'Test expression statement - code generation.'
from freeode.interpreter import Interpreter, IFloat, CallableObject
from freeode.ast import DotName, NodeExpressionStmt
prog_text = \
'''
class A:
data a,b: Float
func dynamic(this):
1+2
a+b
compile A
'''
#create the interpreter and interpret the mini-program
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
print
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#get flattened object
sim = intp.get_compiled_objects()[0]
#get the attributes that we have defined
a = sim.get_attribute(DotName('a'))
b = sim.get_attribute(DotName('b'))
dynamic = sim.get_attribute(DotName('dynamic'))
#test some facts about the attributes
assert isinstance(a, IFloat)
assert isinstance(b, IFloat)
assert isinstance(dynamic, CallableObject)
#only one statement is collected (a+b)
assert len(dynamic.statements) == 1
stmt0 = dynamic.statements[0]
assert isinstance(stmt0, NodeExpressionStmt)
assert stmt0.expression.operator == '+'
示例12: test_interpreter_dollar_operator_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_interpreter_dollar_operator_1(): #IGNORE:C01111
msg = 'Test "$" operator. Basic capabililities.'
#py.test.skip(msg)
print msg
from freeode.interpreter import Interpreter, IFloat, CallableObject
from freeode.ast import DotName, RoleStateVariable, RoleTimeDifferential
prog_text = \
'''
class A:
data a1: Float
func dynamic(this):
$a1 = a1
compile A
'''
#create the interpreter
intp = Interpreter()
intp.interpret_module_string(prog_text, None, 'test')
print
#print intp.modules['test']
#print intp.get_compiled_objects()[0]
#get flattened object
sim = intp.get_compiled_objects()[0]
#get the attributes that we have defined
a1 = sim.get_attribute(DotName('a1'))
a1_dt = sim.get_attribute(DotName('a1$time')) #implicitly defined by $ operator
dynamic = sim.get_attribute(DotName('dynamic'))
#test some facts about the attributes
assert isinstance(a1, IFloat) #a1 is state variable, because it
assert a1.role == RoleStateVariable #has derivative
assert isinstance(a1_dt, IFloat)
assert a1_dt.role == RoleTimeDifferential # $a1 is a time differential
assert isinstance(dynamic, CallableObject)
#test if assignment really is 'a1$time' = 'a1'
assign = dynamic.statements[0]
assert assign.target is a1_dt
assert assign.expression is a1
示例13: test_SimulationClassGenerator__create_sim_class_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_SimulationClassGenerator__create_sim_class_1():
msg = ''' Test SimulationClassGenerator.create_sim_class:
Just see if function does not crash.
'''
#py.test.skip(msg)
print msg
import cStringIO
from freeode.pygenerator import SimulationClassGenerator
from freeode.interpreter import Interpreter
#from freeode.simulatorbase import SimulatorBase
prog_text = \
'''
class A:
data a:Float
data b: Float
data c: Float param
func initialize(this):
a = 1
c = 2
print(a)
func dynamic(this):
b = c
$a = b * sin(a)
func final(this):
graph(a)
compile A
'''
#interpret the compile time code
intp = Interpreter()
intp.interpret_module_string(prog_text, 'foo.siml', '__main__')
flat_o = intp.get_compiled_objects()[0]
#create the Python class definition as text
buf = cStringIO.StringIO()
cg = SimulationClassGenerator(buf)
cg.create_sim_class('A', flat_o)
cls_txt = buf.getvalue()
print cls_txt
示例14: test_print_function_4
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_print_function_4(): #IGNORE:C01111
#py.test.skip('Test the print function. - code generation for: user defined class.')
print 'Test the print function. - code generation for: user defined class.'
from freeode.interpreter import Interpreter
from freeode.ast import DotName
prog_text = \
'''
#print user defined class
class C:
data a: Float
data b: String
func __str__(this):
return a.__str__() + ' and ' + b.__str__()
#return ' and ' + b.__str__()
class A:
data c: C
func dynamic(this):
print(c)
compile A
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
# print 'module after interpreter run: ---------------------------------'
# print intp.modules['test']
#get flattened object
sim = intp.get_compiled_objects()[0]
#print sim
#get the dynamic function with the generated code
dynamic = sim.get_attribute(DotName('dynamic'))
assert len(dynamic.statements) == 1
示例15: test_print_function_3
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import get_compiled_objects [as 别名]
def test_print_function_3(): #IGNORE:C01111
#py.test.skip('Test the print function. - code generation for: Float, String')
print 'Test the print function. - code generation for: Float, String'
from freeode.interpreter import Interpreter
from freeode.ast import DotName
prog_text = \
'''
class A:
data a,b,foo: Float
data bar: String
func dynamic(this):
#print known constants
print(23)
print('hello ',2, ' the world!')
#print unknown value
print(foo)
print(bar)
#print unevaluated expression
print(a+b)
compile A
'''
#create the interpreter
intp = Interpreter()
#run mini program
intp.interpret_module_string(prog_text, None, 'test')
# print
# print 'module after interpreter run: ---------------------------------'
# print intp.modules['test']
#get flattened object
sim = intp.get_compiled_objects()[0]
#print sim
#get the dynamic function with the generated code
dynamic = sim.get_attribute(DotName('dynamic'))
assert len(dynamic.statements) == 5