• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jython Grief

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Ok, here's my diclaimer: I am as beginner as you get with Obj Orient. prog. and know very little about Java and computers altogether, but wound up using Jython to write Scripts for a software. Anyway, here's the deal: I want to access Java methods like readInt so that I can use them in my scripts. But when I use the import command in Jython i get an error message telling me that the java module does not exist!! Can anyone help me out here? If you really have some spare time on your hands, you could download Jython from www.Jython.org and play around with it.
Thanks,
Josh
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joshua, can you please be a little more specific and give us a code example which does not work for you? What platform are you using, what java?
I have downloaded onto Win/NT and installed Jython and can do things like:
>>> from java.io import *
>>> x = FileInputStream("Hello.txt")
>>> y = InputStreamReader(x)
>>> z = BufferedReader(y)
>>> print z.readLine()
Hello
>>> print z.readLine()
None
>>> z.close()
>>> y.close()
>>> x.close()
[ February 21, 2003: Message edited by: Barry Gaunt ]
 
joshua weinstein
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey barry,
thanks for taking the time to erply to my post. ok, let me give you a more precise description of my problem. I am trying to read information from a file. I can open the desired file and read in data no problem, however I can only read data in the form of strings and not integers! I wasnt ot be able to import the readInt() method from Java in order to read data in the form of integers.
Here's the code:
from java.io import readInt()
import struct
import math
import os
file1 = open("C:/Program Files/Hec/HecDssVue/script_iofiles/prompt_pmaker.in")
nbpart = file1.readInt()
The compile/interpretor error occurs on that last line and it is:
AttributeError: instance of 'org.Python.core.PyFile' has no attribute 'readInt'
I am using the Windows/2000 platform. I think that the problem is that I am not importing the readInt() method properly. Any suggestions?
Thanks again,
Josh
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Josh, can you give an example of what your input file looks like? Is it a file written by a Java program using writeInt? Or is it a text file?
When I know I can play around a bit to get something to read it.
-Barry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Barry,
The file is a text file. It looks like this:
7
subbasin-3
subbasin-5
subbasin-6
subbasin-7
subbasin-8
subbasin-9
subbasin-10
2
flow
elevation
10min
The last line of the code that I gave you is supposed to read the number '7' from the file. I think that it is the import of the readInt() method that Jython is having a problem with.
Thanks,
Josh
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Josh,
I think the problem is that you are getting a bit confused between the files that Jython uses (Python files), and the files that Java uses. A Python file only has the read(), readline() and readlines() methods and they read Jython strings.
So you can do something like line=file1.read() to get a string (line) from the file. line will have the value None at the end of the file. You would the have to split the strings and coerce the bits that are representing integers: i = int(piece) , where piece is a string '42', say.
To do Java-like processing you would have to create a DataInputStream and use its readInt() method. But that would only work with files created with DataOutputStream.writeInt().
The important thing is to realize that the Python IO is independent from Java IO and is a viable simple alternative to the Java IO.
Now I'm going to play with your file, to see if I'm talking sense or not
-Barry
BTW you don't import methods but modules (or Java packages/classes).
Also a book: "Jython Essentials, Samuele Pedroni, Noel Rappin, O'Reilly"
[ March 01, 2003: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is in no way a program, but more what I typed by hand. You can paste it into a file (josh.py) and do jython josh.py. Try it:
Note the use of the "slice" [:-1] to get rid of the newline character. And don't forget make sure that 10min in the data file is terminated by a new line.
[ March 01, 2003: Message edited by: Barry Gaunt ]
 
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic