none
Unexpected identifier in expression. Expected ',', ']' or other token.

    Question

  • in order to solve error unicode_escape_decode() takes no arguments

    i change unicode to ascii, however got another error

    Unexpected identifier in expression. Expected ',', ']' or other token.

    let engine = Python.CreateEngine(); let paths = engine.GetSearchPaths(); paths.Add(@"C:\Python27\Lib\site-packages"); paths.Add(@"C:\Python27"); paths.Add(@"C:\Program Files (x86)\IronPython 2.7.1\Lib"); engine.SetSearchPaths(paths); let scope = engine.CreateScope(); let MatrixString = "" let numberofvariables = 2 let numberofvariablestring = numberofvariables.ToString() let unicodeString = String.Format("import sys from sympy import * from sympy import Matrix from sympy.abc import x, y, z, f, a, b from sympy import * f = Symbol('f') x = Symbol('x') y = Symbol('y') z = Symbol('z') varlist = [x,y,z,a,b]

    Matrix = ([[1,0],[0,1]]) preresult = A") // Create two different encodings. let ascii : Encoding = Encoding.ASCII; let unicode : Encoding = Encoding.Unicode; // Convert the string into a byte array. let unicodeBytes = unicode.GetBytes(unicodeString); // Perform the conversion from one encoding to the other. let asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes); // Convert the new byte[] into a char[] and then into a string. //let asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)] let asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)] // error at here ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0); let asciiString = new String(asciiChars); let script = engine.CreateScriptSourceFromString(asciiString) let scoperesult = script.Execute(scope)




    Early Bird


    • Edited by oooooxxxxx Sunday, March 16, 2014 4:46 AM
    Sunday, March 16, 2014 4:45 AM

Answers

  • It's likely to be much easier to get the Python code working at the IPy prompt, and only then firewall it away within the scripting engine.

    Trying IPy 2.7.4 and SymPy 0.7.5 gets you a nice stack trace

    >>> from sympy import *
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\__init__.py", line 32, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\__init__.py", line 8, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\expr.py", line 7, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\evalf.py", line 9, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\__init__.py", line 5, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\ctx_fp.py", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\ctx_base.py", line 3, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\__init__.py", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\libmpf.py", line 17, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\backend.py", line 33, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\six.py", line 333, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\six.py", line 323, in exec_
    AttributeError: 'module' object has no attribute '_getframe'

    where we can then go and look at the code, and see that it's executing `sys._getframe`, which we need to enable with ` -X:Frames` on the command line, or via options to the `CreateEngine` code

    let options = Dictionary<string, object>()
    options.["Frames"] <- true
    options.["FullFrames"] <- true
    let engine = Python.CreateEngine(options);

    Next go round, where I've used

    def my_unicode_escape_decode(x):
      print 'ued:> ' + x
      return x

    outputs

    >>> from sympy import *
    ued:> s
    ued:> ms
    ued:> \u03bcs
    ued:> ns
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\__init__.py", line 34, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\assumptions\__init__.py", line 2, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\assumptions\ask.py", line 323, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\cache.py", line 93, in wrapper
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\function.py", line 185, in __new__
    ImportError: No module named fancysets
    which indicates that we're going to have to implement the decoding of unicode escapes properly (it should be printing s,ms, μs,ns).  And that from here in in it is purely a problem of getting SymPy running with IronPython 2.7 and no longer an F# problem.


    • Edited by Mr. Tines Wednesday, March 19, 2014 10:47 AM There may be more of a problem with sympy
    • Marked as answer by oooooxxxxx Thursday, March 20, 2014 9:45 AM
    Wednesday, March 19, 2014 8:47 AM

All replies

  • It's telling you that your copy/paste of some C# is not valid F#.  Quelle surprise!

    I'm assuming you've elided some substitutions which would make that use of `String.Format` make sense.

    Given that when all the characters in the string `unicodeString` are in the ASCII range, you could just write

    let asciiString = unicodeString

    I'll assume you have a string with non-ASCII characters in e.g.

    let unicodeString = "this costs €24.99"

    and want to filter them out.  So you could check MSDN for the definition of `Encoding.GetChars` which tells us it is

    GetChars : 
            bytes:byte[] -> char[] 

    so you neither need nor want to declare a target array, and write

    // Convert the new byte[] into a char[] and then into a string. let asciiString = String(asciiBytes |> ascii.GetChars) //val asciiString : String = "this costs ?24.99"

    noting that, as the string is not `use`d as an `IDisposable`, it is not idiomatic F# to use the `new` keyword with the constructor (and indeed as `System.String` does not implement `IDisposable`, you couldn't).



    • Edited by Mr. Tines Sunday, March 16, 2014 8:41 AM extending the discussion of the use of `new`
    Sunday, March 16, 2014 8:33 AM
  • same error even if convert to ascii

    let engine = Python.CreateEngine();
        let paths = engine.GetSearchPaths();
    
        paths.Add(@"C:\Python27\Lib\site-packages");
        paths.Add(@"C:\Python27");
        paths.Add(@"C:\Program Files\IronPython 2.7\Lib");
        engine.SetSearchPaths(paths);
    
        let scope = engine.CreateScope();
    
        let MatrixString = ""
        let numberofvariables = 2
        let numberofvariablestring = numberofvariables.ToString()
    
        let unicodeString = String.Format("import sys 
    from sympy import *
    from sympy import Matrix
    from sympy.abc import x, y, z, f, a, b
    from sympy import *
    f = Symbol('f')
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')
    varlist = [x,y,z,a,b]
    A = Matrix([[1,0],[0,1]])
    result = A")
        // Create two different encodings.
        let ascii : Encoding = Encoding.ASCII;
        let unicode : Encoding = Encoding.Unicode;
        // Convert the string into a byte array.
        let unicodeBytes = unicode.GetBytes(unicodeString);
        // Perform the conversion from one encoding to the other.
        let asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
    
        // Convert the new byte[] into a char[] and then into a string.
        //let asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)]
        //let asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)]  //  error at here
        //ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
        //let asciiString = new String(asciiChars);
        let asciiString = String(asciiBytes |> ascii.GetChars)
    
        let script = engine.CreateScriptSourceFromString(asciiString)
        let scoperesult = script.Execute(scope)
        Console.WriteLine(scope.GetVariable("result"))


    Early Bird

    Monday, March 17, 2014 3:21 AM
  • Looking at the IronPython source at https://github.com/IronLanguages/main/blob/master/Languages/IronPython/IronPython.Modules/_codecs.cs I observe it's just a stub --

            public static PythonTuple unicode_escape_decode() {
                throw PythonOps.NotImplementedError("unicode_escape_decode");
            }
    I would guess you would have to monkeypatch the codecs module.

    Monday, March 17, 2014 7:02 AM
  • how to  monkeypatch the codecs module

    does it mean that i have to download source code of ironpython and edit it and compile again in visual studio 2013?


    If ask me rabbit faster or turtle faster, i can not answer, because if rabbit do not know direction or path, it is meaningless to run first.

    Monday, March 17, 2014 8:02 AM
  • This is where you take advantage of Python being a dynamic language and inject your own method over the top of the supplied one -- code would be something like this, done at the start of the Python execution

    import codecs
    
    def my_unicode_escape_decode(x):
      return x
    
    codecs.unicode_escape_decode = my_unicode_escape_decode


    • Edited by Mr. Tines Monday, March 17, 2014 1:11 PM typo
    Monday, March 17, 2014 10:02 AM
  • after tried it , it runs a very long time, never stop

    is these code correct?

        let engine = Python.CreateEngine();
        let paths = engine.GetSearchPaths();
    
        paths.Add(@"C:\Python27\Lib\site-packages");
        paths.Add(@"C:\Python27");
        paths.Add(@"C:\Program Files\IronPython 2.7\Lib");
        engine.SetSearchPaths(paths);
    
        let scope = engine.CreateScope();
    
        let MatrixString = ""
        let numberofvariables = 2
        let numberofvariablestring = numberofvariables.ToString()
    
        let unicodeString = String.Format("import codecs
    def my_unicode_escape_decode(x):
        return x
    codecs.unicode_escape_decode = my_unicode_escape_decode
    import sys 
    from sympy import *
    from sympy import Matrix
    from sympy.abc import x, y, z, f, a, b
    from sympy import *
    f = Symbol('f')
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')
    varlist = [x,y,z,a,b]
    A = Matrix([[1,0],[0,1]])
    result = A")
        let ascii : Encoding = Encoding.ASCII;
        let unicode : Encoding = Encoding.Unicode;
        let unicodeBytes = unicode.GetBytes(unicodeString);
        let asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
        let asciiString = String(asciiBytes |> ascii.GetChars)
    
        let script = engine.CreateScriptSourceFromString(asciiString)
        let scoperesult = script.Execute(scope)
        Console.WriteLine(scope.GetVariable("result"))


    Early Bird

    Tuesday, March 18, 2014 5:52 AM
  • That's the sort of thing I was thinking of; and it does seem to have addressed the error you were seeing; but at this point I'm out of easy advice.  I guess you're going to have to debug this yourself.

    It may be informative to add a call to System.Diagnostics.Debug.WriteLine in `my_unicode_escape_decode` to dump the input value, and then add a number of similar trace statements at intervals through the rest of the script (and then watch what it reports in DbgView)

    Tuesday, March 18, 2014 9:45 AM
  • if run the script with python, it do not have error.

    After add async, it has error when run in F#

    Additional information: 'ScopeStorage' object has no attribute 'my_unicode_escape_decode'

    After run a long time, it has error finally.

    An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.Dynamic.dll


    Additional information: 'module' object has no attribute '_getframe'

    to see this , need to delete async

    [<EntryPoint>]
    let main argv = 
        let engine = Python.CreateEngine();
        let paths = engine.GetSearchPaths();
    
        paths.Add(@"C:\Python27\Lib\site-packages");
        paths.Add(@"C:\Python27");
        paths.Add(@"C:\Program Files\IronPython 2.7\Lib");
        engine.SetSearchPaths(paths);
    
        let scope = engine.CreateScope();
    
        let MatrixString = ""
        let numberofvariables = 2
        let numberofvariablestring = numberofvariables.ToString()
    
        let unicodeString = String.Format("import codecs
    def my_unicode_escape_decode(x):
        return x
    codecs.unicode_escape_decode = my_unicode_escape_decode
    import sys 
    from sympy import *
    from sympy import Matrix
    from sympy.abc import x, y, z, f, a, b
    from sympy import *")
        let ascii : Encoding = Encoding.ASCII;
        let unicode : Encoding = Encoding.Unicode;
        let unicodeBytes = unicode.GetBytes(unicodeString);
        let asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
        let asciiString = String(asciiBytes |> ascii.GetChars)
    
        let script = engine.CreateScriptSourceFromString(asciiString)
        let scoperesult = async{script.Execute(scope)}
        System.Diagnostics.Debug.WriteLine(scope.GetVariable("my_unicode_escape_decode"))




    Early Bird




    • Edited by oooooxxxxx Wednesday, March 19, 2014 3:30 AM
    Wednesday, March 19, 2014 2:53 AM
  • It's likely to be much easier to get the Python code working at the IPy prompt, and only then firewall it away within the scripting engine.

    Trying IPy 2.7.4 and SymPy 0.7.5 gets you a nice stack trace

    >>> from sympy import *
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\__init__.py", line 32, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\__init__.py", line 8, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\expr.py", line 7, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\evalf.py", line 9, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\__init__.py", line 5, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\ctx_fp.py", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\ctx_base.py", line 3, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\__init__.py", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\libmpf.py", line 17, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\backend.py", line 33, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\six.py", line 333, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\mpmath\libmp\six.py", line 323, in exec_
    AttributeError: 'module' object has no attribute '_getframe'

    where we can then go and look at the code, and see that it's executing `sys._getframe`, which we need to enable with ` -X:Frames` on the command line, or via options to the `CreateEngine` code

    let options = Dictionary<string, object>()
    options.["Frames"] <- true
    options.["FullFrames"] <- true
    let engine = Python.CreateEngine(options);

    Next go round, where I've used

    def my_unicode_escape_decode(x):
      print 'ued:> ' + x
      return x

    outputs

    >>> from sympy import *
    ued:> s
    ued:> ms
    ued:> \u03bcs
    ued:> ns
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\__init__.py", line 34, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\assumptions\__init__.py", line 2, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\assumptions\ask.py", line 323, in <module>
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\cache.py", line 93, in wrapper
      File "C:\Python27\Lib\site-packages\sympy-0.7.5\sympy\core\function.py", line 185, in __new__
    ImportError: No module named fancysets
    which indicates that we're going to have to implement the decoding of unicode escapes properly (it should be printing s,ms, μs,ns).  And that from here in in it is purely a problem of getting SymPy running with IronPython 2.7 and no longer an F# problem.


    • Edited by Mr. Tines Wednesday, March 19, 2014 10:47 AM There may be more of a problem with sympy
    • Marked as answer by oooooxxxxx Thursday, March 20, 2014 9:45 AM
    Wednesday, March 19, 2014 8:47 AM