def setUp(self): self.init() self.tmpdir = tempdir() self.model = PythonModel(name='func') self.model.set_code(SAMPLE_CODE) self.data = PythonFile(content=SAMPLE_CODE, path=self.tmpdir / "test.py")
def __init__(self, *args, **kwargs): PythonModel.__init__(self, *args, **kwargs) if self.name is None: self.name = 'LPyModel' self._step = 1 self.lsystem = Lsystem() self.axialtree = AxialTree() self.scene_name = self.name + '_scene'
def load_data(): model = PythonModel(name='func') model.set_code(code) tmpdir = tempdir() data = PythonFile(content=code, path=tmpdir / "test.py") # ns is provided by tester editor = ns['editor_manager'] editor.open_data(data)
def test_repr_code(): model_src = '''""" input = x:int=4, y:float=3.14, z, debug:bool output = success beautifull doc """ print "ok" ''' model = PythonModel(code=model_src) print '>>>', model.repr_code(), '<<<' assert model.repr_code() == model_src
def test_in_function(): code = """ ''' output=out ''' a=1 def f(): return(a) out = f() """ from openalea.core.model import PythonModel model = PythonModel(name='func') model.set_code(code) assert model.init() == 1
def test_model_get_documentation(): model_src = '''""" This is the doc of my model """ print "ok" result = 42 """ ok """ ''' model = PythonModel(code=model_src) assert model.get_documentation() == "This is the doc of my model"
def __copy__(self): m = PythonModel.__copy__(self) m.set_code(self._initial_code) m.lsystem = self.lsystem m.axialtree = self.axialtree m.scene_name = self.scene_name return m
def test_recursif(): model_src = '''"""input = x output = result""" result = x + 1 ''' model = PythonModel(code=model_src) result = model(model(model(model(model(1))))) assert result == 6
class TestCaseParadigmEditor(QtTestCase): def setUp(self): self.init() self.tmpdir = tempdir() self.model = PythonModel(name='func') self.model.set_code(SAMPLE_CODE) self.data = PythonFile(content=SAMPLE_CODE, path=self.tmpdir / "test.py") def tearDown(self): self.tmpdir.rmtree() self.finalize() def test_open_data(self): self.widget = ParadigmContainer() self.widget.open_data(self.data) def test_apply_and_save(self): self.widget = ParadigmContainer() self.widget.open_data(self.data) memory_code = "# How are you ?" hdd_code = "# Fine!" pyqode = self.widget.currentWidget() pyqode.setPlainText(memory_code) # Unchanged because data has not been saved self.assertEquals(self.data.content, SAMPLE_CODE) # APPLY: change data object but do not save on disk self.widget.apply() # Changed in memory but not on disk self.assertFalse(self.data.path.exists()) self.assertEquals(self.data.content, memory_code) pyqode.setPlainText(hdd_code) self.widget.save() # SAVE: change data object and save to disk with open(self.data.path, 'r') as f: disk_code = f.read() self.assertEquals(self.data.content, hdd_code) self.assertEquals(disk_code, hdd_code)
def test_nested_functions_in_init(): model_src = ''' """ output = a """ def f0(): def f1(): return 10 return f1() a = f0() ''' model = PythonModel(code=model_src) result = model.init() assert result == 10
def test_magic(): model_src = '''""" input = x:int=4, y:float=3.14, z, debug:bool output = success beautifull doc """ %pylab inline print "ok" ''' model = PythonModel(code=model_src) assert model.repr_code() == model_src assert model.get_documentation() is not None assert len(model.inputs_info) == 4 assert len(model.outputs_info) == 1
def test_clean_ns(): from openalea.core.model import PythonModel from openalea.core.service.ipython import interpreter interp = interpreter() interp.user_ns['ipython_ns'] = 1 model = PythonModel(name='clean') model.outputs_info = [OutputObj('ns')] model.inputs_info = [InputObj('a=0'), InputObj('b')] model.set_step_code('local_to_model=1\nns = dir()') initial_ns = dict(initial_ns=1) model_local_ns = model(namespace=initial_ns, b=1) model_varname = ['a', 'b', 'local_to_model'] for varname in model_varname: assert varname in model_local_ns for varname in model_varname: assert varname not in interp.user_ns assert 'ipython_ns' in interp.user_ns
def test_nested_functions_special_functions(): model_src = ''' """ output = a """ def step(): def f0(): def f1(): return 10 return f1() a = a+f0() a = 0 ''' model = PythonModel(code=model_src) result = model.init() assert result == 0 result = model.init() result = model.step() assert result == 10 result = model.run(nstep=10) assert result == 100
def test_run_tuple(): model_src = '''"""input = x, y=(1,2,3) output = result""" result = 0 for val in x: result += val for val in y: result += val ''' model = PythonModel(code=model_src) assert model is not None assert model.outputs == [] result = model([4]) assert model.outputs == 10 assert result == 10 result = model.run([5]) assert result == 11 result = model.run([1, 1], [1, 1]) assert result == 4 result = model([2, 2], [2, 2]) assert result == 8 result = model.run((5, )) assert result == 11 result = model.run((1, 1), (1, 1)) assert result == 4 result = model((2, 2), [2, 2]) assert result == 8
def test_model_inputs_info(): model_src = '''""" input = x:int=4, y:float=3.14, z, debug:bool output = success beautifull doc """ print "ok" ''' model = PythonModel(code=model_src) assert len(model.inputs_info) == 4 assert len(model.outputs_info) == 1 assert model.inputs_info[0].name == "x" assert model.inputs_info[0].default == "4" # assert model.inputs_info[0].interface == "int" assert model.inputs_info[1].name == "y" assert model.inputs_info[1].default == "3.14" # assert model.inputs_info[1].interface == "float" assert model.inputs_info[2].name == "z" assert model.inputs_info[3].name == "debug" # assert model.inputs_info[3].interface == "bool" assert model.outputs_info[0].name == "success" assert model.get_documentation() == """input = x:int=4, y:float=3.14, z, debug:bool output = success beautifull doc""" model_src2 = '''""" input = x=[1,2,3], y=(1,2), z=(1,), a=4, b output = c, d, e:bool beautifull doc """ print "ok" ''' model2 = PythonModel(code=model_src2) assert len(model2.inputs_info) == 5 assert len(model2.outputs_info) == 3 assert model2.inputs_info[0].name == "x" assert model2.inputs_info[0].default == "[1,2,3]" assert model2.inputs_info[1].name == "y" assert model2.inputs_info[1].default == "(1,2)" assert model2.inputs_info[2].name == "z" assert model2.inputs_info[2].default == "(1,)" assert model2.inputs_info[3].name == "a" assert model2.inputs_info[3].default == "4" assert model2.inputs_info[4].name == "b" assert model2.outputs_info[0].name == "c" assert model2.outputs_info[1].name == "d" assert model2.outputs_info[2].name == "e" # assert model2.outputs_info[2].interface == "bool" assert model2.get_documentation() == """input = x=[1,2,3], y=(1,2), z=(1,), a=4, b
def test_kwargs(): model_src = '''"""input = x=1, y=2 output = result""" result = x + y ''' model = PythonModel(code=model_src) result = model(0, 1) assert result == 1 result = model(x=2, y=2) assert result == 4 result = model(x=3) assert result == 5 result = model(y=3) assert result == 4
def test_run(): model_src = '''"""input = x=1, y=2 output = result""" result = x + y ''' model = PythonModel(code=model_src) assert model is not None assert model.outputs == [] result = model() assert model.outputs == 3 assert result == 3 result = model.run(4) assert result == 6 result = model.run(1, 1) assert result == 2 result = model(5) assert result == 7 result = model(3, 5) assert result == 8
def test_step(): model = PythonModel(code=model_src) result = model.init() assert result == 0 result = model.step() assert result == 1 result = model.step() assert result == 2 result = model.step() assert result == 3 result = model.step() assert result == 4 result = model.init() assert result == 0 result = model.animate() assert result == 10
def test_run_list(): model_src = '''"""input = x, y=[1,2,3] output = result""" result = 0 for val in x: result += val for val in y: result += val ''' model = PythonModel(code=model_src) assert model is not None assert model.outputs == [] result = model([4]) assert model.outputs == 10 assert result == 10 result = model.run([5]) assert result == 11 result = model.run([1, 1], [1, 1]) assert result == 4 result = model([2, 2], [2, 2]) assert result == 8
def test_fibonacci(): model_fibo_src = '''""" Compute one step of Fibonacci sequence. Take in inputs x(i) and x(i+1) and return x(i+1) and x(i+2). input = a, b output = b, r """ r = a + b ''' fibo = PythonModel(code=model_fibo_src) xi, xj = 0, 1 nb_step = 20 for i in range(int(nb_step) - 1): xi, xj = fibo(xi, xj, namespace=locals()) assert xi == 4181 assert xj == 6765
def test_step_without_run(): model_src = '''""" output = a""" a = 0 def step(): a = a + 1 ''' model = PythonModel(code=model_src) result = model.init() assert result == 0 result = model.step() assert result == 1 result = model.step() assert result == 2 result = model.step() assert result == 3 result = model.init() assert result == 0
def test_parse_code(): code = ''' """ input = a,b output = c """ c = a+b ''' from openalea.core.model import PythonModel m1 = PythonModel(name='m1', code=code) m2 = PythonModel(name='m2') m2.code = code m3 = PythonModel(name='m3') m3.set_code(code) assert m1.run(a=1, b=2) == 3 assert m2.run(a=2, b=3) == 5 assert m3.run(a=3, b=4) == 7 m1 = PythonModel(name='ModelWithoutDoc') assert m1.get_documentation() == '' m1 = PythonModel(name='ModelWithDoc', code=code) assert m1.get_documentation()
def __copy__(self): m = PythonModel.__copy__(self) m.set_code(self._initial_code) return m