本文整理汇总了Python中freeode.interpreter.Interpreter.apply方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.apply方法的具体用法?Python Interpreter.apply怎么用?Python Interpreter.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freeode.interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.apply方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_SimlFunction_1
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import apply [as 别名]
def test_SimlFunction_1(): #IGNORE:C01111
msg = \
'''
Test SimlFunction: call user defined function
User defined functions are created without parser.
'''
#skip_test(msg)
print msg
from freeode.interpreter import (Interpreter, SimlFunction,
Signature, IFloat, IString)
from freeode.ast import NodeFuncArg, NodeReturnStmt, NodeIdentifier
from freeode.util import UserException
#create the interpreter
intp = Interpreter() #IGNORE:W0612
lib = intp.built_in_lib
#create a Siml value as function argument
val_1 = IFloat(1)
#create a function without statements (impossible in Siml)
# func test(a:Float):
# ** nothing **
f1 = SimlFunction('test', Signature([NodeFuncArg('a', IFloat)]),
statements=[], global_scope=lib)
#call with existing value
intp.apply(f1, (val_1,))
#create a function with return statement - uses interpreter for executing the statement
# func test(a:Float) -> Float:
# return a
f2 = SimlFunction('test', Signature([NodeFuncArg('a', IFloat)], IFloat),
statements=[NodeReturnStmt([NodeIdentifier('a')])],
global_scope=lib)
#call function and see if value is returned
ret_val = intp.apply(f2, (val_1,))
assert ret_val.value == 1. #IGNORE:E1103
#create a function with wrong return type
# func test(a:Float) -> String:
# return a
f3= SimlFunction('test', Signature([NodeFuncArg('a', IFloat)], IString),
statements=[NodeReturnStmt([NodeIdentifier('a')])],
global_scope=lib)
def raise_1():
intp.apply(f3, (val_1,))
assert_raises(UserException, 3200320, raise_1)
示例2: test_SimlFunction_3
# 需要导入模块: from freeode.interpreter import Interpreter [as 别名]
# 或者: from freeode.interpreter.Interpreter import apply [as 别名]
def test_SimlFunction_3(): #IGNORE:C01111
msg = \
'''
Test SimlFunction: storage of local variables during code collection.
User defined functions are created without parser.
'''
#skip_test(msg)
print msg
from freeode.interpreter import (Interpreter, SimlFunction, IModule,
Signature, IFloat)
from freeode.ast import NodeFuncArg
from freeode.util import DotName #, aa_make_tree
#create the interpreter
intp = Interpreter()
#create a Siml value as function argument
val_1 = IFloat(1)
#create a function without statements (impossible in Siml)
# func test(a:Float):
# ** nothing **
f1 = SimlFunction('test', Signature([NodeFuncArg('a', IFloat)]),
statements=[], global_scope=intp.built_in_lib, loc=None,
dot_name=DotName('test_module.test'))
#create module where the function lives
mod1 = IModule()
mod1.test = f1
#call with existing value
# and set interpreter up to collect code. In this mode local variables of all
# functions must become algebraic variables of the simulation object.
intp.start_collect_code()
intp.apply(f1, (val_1,))
_stmts, fn_locals = intp.stop_collect_code()
#print aa_make_tree(fn_locals)
#The function argument 'a' must appear in the storage for local variables
assert fn_locals.test.i1.a is val_1