Picon

TypeError question

Hi,
    I need to analyse a python script(annotationScript.py) from another python script(Processor) within java.
1). I did like below and it gave an error. It is  confusing for me because when I called the method using python it worked without errors. Can anyone help me to overcome this error.

//java code
        interp.exec("import annotationScript");
        interp.exec("import Processor");
        String stt = "Processor.getDictionary(annotationScript)";
        PyObject ob1 = interp.eval(stt);

#python method
def getDictionary(str1):
    #strFile = "/home/heshan/workspace/IdeaProjects/PYtest/src/pythonScripts/annotationScript.py"
    #fh = open(strFile,"r")
    fh = open(str1,"r")
    igot = fh.readlines()
    Dictionary = {}
    for line in igot:
        #store the necessary data to Dictionary        
    print Dictionary
    return Dictionary

StackTrace
Exception in thread "main" Traceback (innermost last):
  File "<string>", line 1, in ?
  File "/home/heshan/workspace/IdeaProjects/PYtest/src/pythonScripts/Processor.py", line 20, in getDictionary
TypeError: org.python.core.PyFile(): 1st arg can't be coerced to java.io.OutputStream, java.io.InputStream, java.io.RandomAccessFile or java.io.Writer


2). Since the error was a casting error I thought of doing it like below. It gave an error as well.

//java code
        interp.exec("import Processor");
        String filePath = "/home/heshan/repo/scripts/annotationScript.py";
        String stt = "Processor.getDictionary(" + filePath +")";
        PyObject ob1 = interp.eval(stt);

StackTrace
Exception in thread "main" Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 25, 'Processor.getDictionary(/home/heshan/repo/scripts/annotationScript.py)'))


Can anyone give me an idea of how to overcome this issue.


--
Regards,
Heshan Suriyaarachchi

http://heshans.blogspot.com/
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Jython-users mailing list
Jython-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users
Oti | 3 Sep 22:43
Picon

Re: TypeError question

Hi Heshan,

just a guess: I assume that the getDictionary() method of Processor
wants a String as argument.

Then you could use:
  String stt = "Processor.getDictionary(\"" + filePath +"\")";
or:
  String stt = "Processor.getDictionary('" + filePath +"')";
instead of:
  String stt = "Processor.getDictionary(" + filePath +")";

(basically surround the filePath with single or double quotes.

Hope this helps,
Oti.

On Mon, Aug 25, 2008 at 10:45 AM, Heshan Suriyaarachchi
<heshan.suri <at> gmail.com> wrote:
> Hi,
>     I need to analyse a python script(annotationScript.py) from another
> python script(Processor) within java.
> 1). I did like below and it gave an error. It is  confusing for me because
> when I called the method using python it worked without errors. Can anyone
> help me to overcome this error.
>
> //java code
>         interp.exec("import annotationScript");
>         interp.exec("import Processor");
>         String stt = "Processor.getDictionary(annotationScript)";
>         PyObject ob1 = interp.eval(stt);
>
> #python method
> def getDictionary(str1):
>     #strFile =
> "/home/heshan/workspace/IdeaProjects/PYtest/src/pythonScripts/annotationScript.py"
>     #fh = open(strFile,"r")
>     fh = open(str1,"r")
>     igot = fh.readlines()
>     Dictionary = {}
>     for line in igot:
>         #store the necessary data to Dictionary
>     print Dictionary
>     return Dictionary
>
> StackTrace
> Exception in thread "main" Traceback (innermost last):
>   File "<string>", line 1, in ?
>   File
> "/home/heshan/workspace/IdeaProjects/PYtest/src/pythonScripts/Processor.py",
> line 20, in getDictionary
> TypeError: org.python.core.PyFile(): 1st arg can't be coerced to
> java.io.OutputStream, java.io.InputStream, java.io.RandomAccessFile or
> java.io.Writer
>
>
> 2). Since the error was a casting error I thought of doing it like below. It
> gave an error as well.
>
> //java code
>         interp.exec("import Processor");
>         String filePath = "/home/heshan/repo/scripts/annotationScript.py";
>         String stt = "Processor.getDictionary(" + filePath +")";
>         PyObject ob1 = interp.eval(stt);
>
> StackTrace
> Exception in thread "main" Traceback (innermost last):
>   (no code object) at line 0
> SyntaxError: ('invalid syntax', ('<string>', 1, 25,
> 'Processor.getDictionary(/home/heshan/repo/scripts/annotationScript.py)'))
>
>
> Can anyone give me an idea of how to overcome this issue.
>
>
> --
> Regards,
> Heshan Suriyaarachchi
>
> http://heshans.blogspot.com/
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Jython-users mailing list
> Jython-users <at> lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jython-users
>
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

Gmane