From: tuorjr Date: Wed, 28 Sep 2016 10:14:22 +0000 (+0000) Subject: Some changes to Python.scl. X-Git-Tag: v1.31.0~16 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=35382d886800c915bbdcfd595cf7b8401413377f;p=simantics%2Fpython.git Some changes to Python.scl. git-svn-id: https://www.simantics.org/svn/simantics-incubator/reino@1692 e36c2e66-7d30-0410-bdb2-d9e1f5a6d952 --- diff --git a/org.simantics.pythonlink/scl/Simantics/Python.scl b/org.simantics.pythonlink/scl/Python.scl similarity index 63% rename from org.simantics.pythonlink/scl/Simantics/Python.scl rename to org.simantics.pythonlink/scl/Python.scl index 375c549..b1ca052 100644 --- a/org.simantics.pythonlink/scl/Simantics/Python.scl +++ b/org.simantics.pythonlink/scl/Python.scl @@ -5,8 +5,8 @@ effect Python "org.simantics.pythonlink.PythonContext" class PythonCompatible a where - setPythonVariable :: String -> a -> () - getPythonVariable :: String -> a + setPythonVariable :: String -> a -> () + getPythonVariable :: String -> a importJava "org.simantics.pythonlink.NDArray" where data NDArray @@ -39,42 +39,49 @@ importJava "org.simantics.pythonlink.NDArray" where instance Show NDArray where show = ndarrayToString +instance Eq NDArray where + (==) = ndarrayEquals + importJava "org.simantics.pythonlink.PythonContext" where data PythonContext @JavaName close closePythonContext :: PythonContext -> () - executePythonStatement :: String -> () + executePythonStatement :: String -> () - setPythonIntegerVariable :: String -> Integer -> () - setPythonIntegerArrayVariable :: String -> Vector Integer -> () - setPythonDoubleVariable :: String -> Double -> () - setPythonDoubleArrayVariable :: String -> Vector Double -> () - setPythonStringVariable :: String -> String -> () - setPythonStringArrayVariable :: String -> Vector String -> () - setPythonNDArrayVariable :: String -> NDArray -> () + setPythonIntegerVariable :: String -> Integer -> () + setPythonIntegerArrayVariable :: String -> Vector Integer -> () + setPythonDoubleVariable :: String -> Double -> () + setPythonDoubleArrayVariable :: String -> Vector Double -> () + setPythonStringVariable :: String -> String -> () + setPythonStringArrayVariable :: String -> Vector String -> () + setPythonNDArrayVariable :: String -> NDArray -> () - getPythonIntegerVariable :: String -> Integer - getPythonIntegerArrayVariable :: String -> Vector Integer - getPythonDoubleVariable :: String -> Double - getPythonDoubleArrayVariable :: String -> Vector Double - getPythonStringVariable :: String -> String - getPythonStringArrayVariable :: String -> Vector String - getPythonNDArrayVariable :: String -> NDArray + getPythonIntegerVariable :: String -> Integer + getPythonIntegerArrayVariable :: String -> Vector Integer + getPythonDoubleVariable :: String -> Double + getPythonDoubleArrayVariable :: String -> Vector Double + getPythonStringVariable :: String -> String + getPythonStringArrayVariable :: String -> Vector String + getPythonNDArrayVariable :: String -> NDArray importJava "org.simantics.pythonlink.Python" where - openPythonContext :: () -> PythonContext + openPythonContext :: PythonContext - runPythonF :: (() -> a) -> a - runWithPythonContextF :: PythonContext -> (() -> a) -> a + runPythonF :: (() -> a) -> a + runWithPythonContextF :: PythonContext -> (() -> a) -> a -runPython :: ( a) -> a +runPython :: ( a) -> a runPython v = runPythonF (\_ -> v) -runWithPythonContext :: PythonContext -> ( a) -> a +runWithPythonContext :: PythonContext -> ( a) -> a runWithPythonContext python v = runWithPythonContextF python (\_ -> v) +instance (VecComp a, PythonCompatible (Vector a)) => PythonCompatible [a] where + setPythonVariable n v = setPythonVariable n (vector v) + getPythonVariable = vectorToList . getPythonVariable + instance PythonCompatible Double where setPythonVariable = setPythonDoubleVariable getPythonVariable = getPythonDoubleVariable @@ -82,7 +89,7 @@ instance PythonCompatible Double where instance PythonCompatible (Vector Double) where setPythonVariable = setPythonDoubleArrayVariable getPythonVariable = getPythonDoubleArrayVariable - + instance PythonCompatible Integer where setPythonVariable = setPythonIntegerVariable getPythonVariable = getPythonIntegerVariable diff --git a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts index 7464d73..50c0423 100644 --- a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts +++ b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/scripts/Python.sts @@ -1,5 +1,4 @@ -> import "Simantics/Python" -> import "Vector" +> import "Python" > > runPython do > executePythonStatement "foo = 'bar'\nd = dir()" @@ -46,3 +45,8 @@ vector [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0] > executePythonStatement "bar = foo.cumsum(1)" > getPythonNDArrayVariable "bar" ndarray(2x3) [[1.0, 3.0, 6.0], [4.0, 9.0, 15.0]] +> runPython do +> setPythonVariable "foo" ([1.0, 2.0, 3.0, 4.0] :: [Double]) +> executePythonStatement "foo.append(sum(foo))" +> getPythonVariable "foo" :: [Double] +[1.0, 2.0, 3.0, 4.0, 10.0]