>... wondering why it seems that every variable used has to
>have self prefixed to it.
Global and local variables do not require the "self." prefix;
it is only members -- aka instance variables -- and methods
that require this.
>I'd just like to know why this is needed or if im doing something wrong.
Or both! (Your error is not capitalizing the "I" and omitting
the apostrophe. Mine is in pointing that out. You're welcome...)
See http://www.python.org/doc/Humor.html#zen ... the second item says
Explicit is better than implicit.
That's the guiding principle for why this part of the language
was designed this way.
channeling-the-Tim-bot-channeling-Guido-ly,
-- Doug

>What I'd suggest is some sort of collection of "porting stories"
>(Python to Jython) and/or issues to expect when doing this kind
>of thing. I'm not sure the FAQ is the best place for that, but
>maybe I'm wrong.
I think a "porting FAQ", whether integrated with the main
FAQ or not, is always a good idea. It provides a way to
capture particular issues that maybe don't belong in the
main docs, but still can save folks some time if documented
somewhhere.

I have a .jar build with jdk 1.3
I'm running jython21 installed under jdk1.4.
when I try to import my jar, I can see the jar and a dir of the imported
package shows all the classes.
But I cannot instantiate or otherwise reference the package.
This problem is familiar, but I've searched for it without luck.
Could I get a little help please

Hello,
I'm trying to use Jython to get to some EJB objects via JBoss 2.2 and
having very little luck. I can get to the database from another
application (that also uses the EJB, but is Java) so I know that it is
being deployed properly.
According to the JBoss startup, my database is being deploy thus:
[jdbc/WP30] Starting
[jdbc/WP30] XA Connection pool jdbc/WP30 bound to java:/jdbc/WP30
[jdbc/WP30] Started
But when I try to get at it via zxODBC, it doesn't work:
>>> from com.ziclix.python.sql import zxJDBC
>>> jndi = "jdbc/WP30"
>>> db = zxJDBC.lookup(jndi)
Traceback (innermost last):
File "<console>", line 1, in ?
DatabaseError: jdbc not bound
And there is a jndi.properties file in that directory, and Jython is
reading it. I know this because if I muck with it, it give me an error:
original jndi.properties:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
messed-up jndi.properties:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=fred://localhost
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Run with bad jndi.properties:
>>> from com.ziclix.python.sql import zxJDBC
>>> jndi = "jdbc/WP30"
>>> db = zxJDBC.lookup(jndi)
Traceback (innermost last):
File "<console>", line 1, in ?
DatabaseError: fred
Now, I am totally lost. What could the problem be? Is there any
suggestions?
-- mikeh
--

Folks,
Please try to avoid using 3 consecutive X's in any code samples as some
email monitoring software views this as adult content and flags the email
for system administrator review. The code below in which I replaced two
X's with periods was the culprit on our system.
Properties are diffrent. X..
> sys.argv[0]
> loading of python classes.
Thanks.
Doug

If you look at the documentation
http://java.sun.com/j2se/1.3/docs/api/java/io/Serializable.html it
details this. Basically java uses two private methods.
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
and
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
to handle serialization. You can implement you own versions of these
methods on an object so that the jvm uses these to serialize/deserialize
the object. This mean that when you do
o = new ObjectOutputStream(new FileOutputStream(filename));
o.writeObject(myClass);
java uses your o.writeObject to write the object to disk. You can then
use any format you wish to describe the object. If you represent you
object as a byte array you should be able to read this array into pyhon
and populate a python object using this data.
On Wed, 2002-02-27 at 21:47, Nicolas Bill Russell wrote:
>
> Really? Could you please explain in detail. :)
> o = new ObjectOutputStream(new FileOutputStream(filename));
> o.writeObject(myClass); (?)
> Isn't the syntax of Python classes different from Java?
> Thanks,
> Bill Nicolas
> Jeff Martin <jeff@...> wrote: You could try implementing the readObject and writeObject methods on
> your java object. This would allow you to control the format into which
> they are serialized so you can interpret in in python.
>
>
> On Tue, 2002-02-26 at 21:31, Nicolas Bill Russell wrote:
> >
> > The reason why I want to use Jython is that I need to access serialized Java objects.
> > I've designed a system that uses objects as the main storage media rather than databases. I used Java for the frontend of my system (Swing GUIs and object serialization) and is planning to use Python (Jython) for client-side processing of Java objects.
> > Is there any other (simpler) way to access Java objects using Python?
> > Thanks,
> > Bill Russell Gagalac-Nicolas
> >
> >
> >
> > ---------------------------------
> > Do You Yahoo!?
> > Get personalised at My Yahoo!.
> --
>
>
>
> ---------------------------------
> Do You Yahoo!?
> Get personalised at My Yahoo!.
--

Hi! I'm still not getting this to work. Now I get another error-message:
Traceback (innermost last): File "<console>", line 1, in ? File =
"v:\eonworx_testbed\gubos\src\python\python-geAamB", line 14, in =
runHello java.lang.IllegalAccessException
at java.lang.reflect.Constructor.newInstance(Native Method)
at =
org.python.core.PyReflectedConstructor.__call__(PyReflectedConstructor.ja=
va)
at org.python.core.PyJavaInstance.__init__(PyJavaInstance.java)
at org.python.core.PyJavaClass.__call__(PyJavaClass.java)
at org.python.core.PyObject.__call__(PyObject.java)
at =
org.python.pycode._pyx2.runHello$2(v:\eonworx_testbed\gubos\src\python\py=
thon-geAamB:14)
at =
org.python.pycode._pyx2.call_function(v:\eonworx_testbed\gubos\src\python=
\python-geAamB)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyFunction.__call__(PyFunction.java)
at org.python.pycode._pyx3.f$0(<console>:1)
at org.python.pycode._pyx3.call_function(<console>)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyCode.call(PyCode.java)
at org.python.core.Py.runCode(Py.java)
at org.python.core.Py.exec(Py.java)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java)
at =
org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.jav=
a)
at =
org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.j=
ava)
at =
org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.j=
ava)
at org.python.util.InteractiveConsole.push(InteractiveConsole.java)
at =
org.python.util.InteractiveConsole.interact(InteractiveConsole.java)
at org.python.util.jython.main(jython.java)
java.lang.IllegalAccessException: java.lang.IllegalAccessException
What could be causing this?
My classpath does not contain the Hello class (I checked with :
>>> for path in System.getProperty("java.class.path").split(";"):
... print path)
I've been getting this error when I try to use sys.add_extdir too. I =
never seem to get this dynamic loading of Java-classes to work. I must =
be doing something very wrong.
/Gustav
-----Original Message-----
From: Samuele Pedroni [mailto:pedroni@...]
Sent: Wednesday, February 27, 2002 6:15 PM
To: Gustav Bostrom; Jython (E-mail)
Subject: Re: [Jython-users] Example of Java class reloading
Example of Java class reloadingHi
From: Gustav Bostrom
>Hi!
>I'm struggling to get the Java-class reloading to work.
>I'm not very succesful.
>Does anyone have a good example to share?
>I tried this, but it didn't work:
>import jreload
>classpath=3D["V:\\eonworx\\output\\contrib\\studio\\classes"]
>classesLoadSet=3Djreload.makeLoadSet("classes",classpath)
>print classesLoadSet
>from classes import Hello
>jreload.reload(classesLoadSet)
you don't need this jreload.reload call
>def rl():
> jreload.reload(classesLoadSet)
>def runHello():
> Hello().sayHello()
>
> When I run rl() the Hello-class is still not reloaded and runHello() =
still
produces the old result.
Assuming that you have changed the class before calling rl() <wink>, the =
code
seems correct.
The Jython Demo/jreload directory contains the artifacts to run the =
interactive
session example
in Doc/jreload.html documentation.
Up to bugs, the only important thing to remember is that the LoadSet =
path
should
be disjunct from classpath and sys.path otherwise reloading will not =
work
(Java limitation).
regards, Samuele Pedroni.

You have encountered a bug,
a workaround is to define
a startup.py:
import TopLevel.mainmodule
and then jythonc --core --deep --jar whatever.jar startup.py
but depending our needs this can be useful or not.
Let us know.
regards, Samuele Pedroni.
----- Original Message -----
From: Bill Janssen <bill@...>
To: <jython-users@...>
Sent: Thursday, February 28, 2002 4:17 AM
Subject: [Jython-users] freezing an application with pythonc? Do Python
packages work?
> I've got a fairly large Python application working with Jython, and
> I'd like to generate a jar file so that the application could be run
> with a command-line like
>
> java -classpath myjarfile.jar TopLevel.mainmodule
>
> This looks pretty easy to do with jythonc, and I've gotten it to work
> on some simple examples. However my large app does things like
>
> import TopLevel
>
> from inside TopLevel.mainmodule. And that doesn't work; I get an
> import error. I see that the documentation page for jythonc has some
> unfinished sections at the bottom, where it says:
>
>
> Freezing modules
>
> jythonc can also be used to freeze a python application. The frozen
> application can then be distributed and deployed as any other java
> application.
>
> Some diffrences between an interpreted application and a frozen
> application exists:
>
> Properties are diffrent. XXX
> sys.argv[0]
> loading of python classes.
>
>
> Anyone know the specifics of those differences? I suspect my troubles
> are coming from "loading of python classes". In particular, I notice
> that if TopLevel contains an __init__.py file, the import fails, but
> if there's no such module, the import succeeds.
>
> Bill
>
> _______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users
>

I've got a fairly large Python application working with Jython, and
I'd like to generate a jar file so that the application could be run
with a command-line like
java -classpath myjarfile.jar TopLevel.mainmodule
This looks pretty easy to do with jythonc, and I've gotten it to work
on some simple examples. However my large app does things like
import TopLevel
from inside TopLevel.mainmodule. And that doesn't work; I get an
import error. I see that the documentation page for jythonc has some
unfinished sections at the bottom, where it says:
Freezing modules
jythonc can also be used to freeze a python application. The frozen
application can then be distributed and deployed as any other java
application.
Some diffrences between an interpreted application and a frozen
application exists:
Properties are diffrent. XXX
sys.argv[0]
loading of python classes.
Anyone know the specifics of those differences? I suspect my troubles
are coming from "loading of python classes". In particular, I notice
that if TopLevel contains an __init__.py file, the import fails, but
if there's no such module, the import succeeds.
Bill

Hi.
[Terry Hancock]
>I was thinking of waiting until the O'Reilly book
>on Jython comes out (I'm not experienced at Java,
>so I don't think the other book is going to help
>me so much), and then having a go at writing such
>a product myself, since I want to do a fairly
>large amount of Jython stuff with Zope. My attraction
>to this is the beauty of having both server and
>client programmed in the same programming language
>(Python), instead of having to learn and interface
>between two different ones.
1. I think that what you imagine is doable
2. Honestly it is not clear to me how
much useful it would be.
Applets can be a useful tool depending
on the kind of work-distribution between
server and client you want to achieve,
and your deployment scenario.
For sure Jython applets are a bit more
heavy than pure Java ones, counting
the runtime or not.
3. Writing this would require a fair
amount of Java/Jython expertise,
especially because at least IMO
it would make sense only if there
is a high-level/smooth and meaningful
interface for talking back to the server,
And for this there are no standard pre-built
solutions.
So much with ranting.
About our book,
[before having an unhappy reader]
we try to supply valuable information
to both audiences, Java-to-Jython
and from-Python-to-Java/Jython
To the first audience Java-to-Jython
we teach Jython.
To the second audience [because of space
/market issues, and because there are already
a lot of books and material about Java around]
we supply comparative information
CPython/Jython and Java/Python but we
still expect that they will read/obtain
introductory Java information from other sources.
If someone comes from Python and have
just gone through some introduction to Java,
he will still have a lot of expections
about what he can do and how because
of the CPython background, we try to
offer valuable information for the cases
where those expections are going to become
gotchas or delusional. There are aspects
of Jython that unless you are a Java
and Python expert and have possibly
looked at the implementation can seem
just arbitrary [more on a different post].
I don't think there is a sensible and valuable
way to explain and teach Jython as
Python + an Extension-that-is-a-Python-view-on-
Java
and then you are done with it.
Java is not simply a language, it is a platform
and rich library too.
regards, Samuele Pedroni.

Hi.
From: <Matt_Conway@...>
> This might be more of a python question, but here goes anyway. If I
> import a module that has an error in it, the import correctly fails. But
> if I then import the same module again, the import succeeds, and the only
> attributes on that module are those that were processed before the failure
> was hit. Is this correct behavior? It seems very dangerous.
> e.g.
>
> <a.py>
> foo='foo'
> errorhere
> bar='bar'
>
> <interactive>
> >>> import a
> ...NameError...
> >>> import a
> >>> a.foo
> 'foo'
> >>> a.bar
> .. AttributeError...
>
Is the BDFL (Guido van Rossum) intended behavior,
the point is that, if an import fails somehow, you are on your own,
if you know how to cleanup things you can do that explicitly
OTOH the interpreter does not try to illude you to have
properly cleaned up things, because there is no general notion
of what that means ...
the precise rationale is something also Guido continously forget
about.
regards, Samuele Pedroni.

This might be more of a python question, but here goes anyway. If I
import a module that has an error in it, the import correctly fails. But
if I then import the same module again, the import succeeds, and the only
attributes on that module are those that were processed before the failure
was hit. Is this correct behavior? It seems very dangerous.
e.g.
<a.py>
foo='foo'
errorhere
bar='bar'
<interactive>
>>> import a
...NameError...
>>> import a
>>> a.foo
'foo'
>>> a.bar
.. AttributeError...
Matt

Sorry Martin - I'll post properly this time :-)
Martin wrote:
Hi,Im new to jython and python but have been programming in java for
a while. I was just wondering why it seems that every variable used has
to
have self prefixed to it. A basic applet example of mine is below.
This wouldnt compile/run until self was placed before each variable and
between
the brackets of init.I'd just like to know why this is needed or if im
doing something wrong.from javax.swing import *
from java.awt import *
class Button1(JApplet):
b1 = JButton("Button 1")
b2 = JButton("Butotn 2")
def init(self):
self.cp = self.getContentPane()
self.cp.setLayout(FlowLayout())
self.cp.add(self.b1)
'self'' is the naming convention for the instance reference and is
effectively the equivalent of 'this' in Java. However it isn't a
keyword in Jython (or CPython) and must be the first parameter declared
in all instance methods. On invocation, 'self' is
replaced by the reference to the object instance, i.e.
class MyClass:
def __init__(self):
# blah blah
def doit(self):
# blah blah
myInst = MyClass()
myInst.doit()
where the self of doit() refers to myInst. It's just that (J/C)Python
requires you to be explicit about how the object
reference gets to the bit of code in question. This is finessed by
languages like Java and C++ but essentially the same
thing happens in "real code". 'this' is just an implicit parameter for
the function in question.
I haven't quite got my own head around the difference between class and
intance methods in Python and I certainly didn't
like this construct at first. But see the way Perl does it for objects
and maybe you'll bless Jython a little more :-)
You are of course free to use 'this' or any other non-conflicting name
instead of 'self' - it's just that 'self' is the de facto
standard.
BTW : the constructor of a class is the method def __init__( self ).
Not sure if that was your intention in the code you
quoted.
Regards
Hamish
PS what I forgot to mention is the fact that you do need to distinguish
between instance variables and local variables inside a method.
self.varname referes to an instance variable
leaving off the self. prefix just leaves you with a variable varname
which is scoped locally to the function.

Hi,
Im new to jython and python but have been programming in java for a while. I
was just wondering why it seems that every variable used has to have self
prefixed to it. A basic applet example of mine is below. This wouldnt
compile/run until self was placed before each variable and between the
brackets of init.
I'd just like to know why this is needed or if im doing something wrong.
from javax.swing import *
from java.awt import *
class Button1(JApplet):
b1 = JButton("Button 1")
b2 = JButton("Butotn 2")
def init(self):
self.cp = self.getContentPane()
self.cp.setLayout(FlowLayout())
self.cp.add(self.b1)

[Bill Russell Gagalac-Nicolas]
> The reason why I want to use Jython is that I need to access serialized Java
> objects.
> I've designed a system that uses objects as the main storage media rather
than
> databases. I used Java for the frontend of my
> system (Swing GUIs and object serialization) and is planning to use Python
> (Jython) for client-side processing of Java objects.
> Is there any other (simpler) way to access Java objects using Python?
Sorry now I'm confused, what's the role of Zope in this.
What do you mean for client-side processing, client-side inside a browser
or ... ?
regards, Samuele Pedroni.

Example of Java class reloadingHi
From: Gustav Bostrom
>Hi!
>I'm struggling to get the Java-class reloading to work.
>I'm not very succesful.
>Does anyone have a good example to share?
>I tried this, but it didn't work:
>import jreload
>classpath=["V:\\eonworx\\output\\contrib\\studio\\classes"]
>classesLoadSet=jreload.makeLoadSet("classes",classpath)
>print classesLoadSet
>from classes import Hello
>jreload.reload(classesLoadSet)
you don't need this jreload.reload call
>def rl():
> jreload.reload(classesLoadSet)
>def runHello():
> Hello().sayHello()
>
> When I run rl() the Hello-class is still not reloaded and runHello() still
produces the old result.
Assuming that you have changed the class before calling rl() <wink>, the code
seems correct.
The Jython Demo/jreload directory contains the artifacts to run the interactive
session example
in Doc/jreload.html documentation.
Up to bugs, the only important thing to remember is that the LoadSet path
should
be disjunct from classpath and sys.path otherwise reloading will not work
(Java limitation).
regards, Samuele Pedroni.

Hi.
> Hello,
>
> 1.
> I'm getting following result in jython (2.1):
> >>> dir("String")
> []
> but python's (2.1) answer is:
> >>> dir("string")
> ['capitalize', 'center', ...., 'upper']
> Ist this intended?
>
> OTOH for Hashtables the dir query delivers the same result for both:
> >>> dir({})
> ['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault',
'update', 'values']
>
> 2.
> The result of dir("string".__class__) in jython is not the same as
dir("string") in python.
> Why?
>
The long story should say that here the difference between the undelying impls
shows through,
a difference that should be reduced with Jython 2.2. In Jython the difference
between
types/classes is less than it was in CPython but the rules are a bit arbitrary.
With CPython 2.2 CPython has undergone a
series of changes to reduce that difference and so Jython 2.2 will have a
clearer model to mimic.
Also the behavior of dir has been precised.
Nevertheless the difference is known and accepted more than intended.
Further dir() is intended only for use at the interactive prompt (a thing that
now is stated in the CPython
docs too, now = latest CPython CVS <wink> not the doc that comes with CPython
2.2), so using it in
a production program is asking for a bit of troubles.
regards, Samuele Pedroni.

Hello -
I have been trying to use the javaos.execute() method for
some time now, unsuccessfully. I basically wanted to do the
following:
import os
os.execute( "f:\tests\HelloWorld" )
where HelloWorld is my compiled and working Java
application. I always get
AttributeError: class 'org.python.modules.os' has no
attribute 'execute'
I have researched this mailing lists archives for this bug
and have seen several posts. But I *have* confirmed that I
am finding javaos on my classpath (I added a print statement
in javaos.py and see it printed). I noticed that execute()
is a method within _ShellEnv class of javaos. So I guess I
do not have access to it? Am I then "forced" to implement
the execute() method in my own .py file? I confirmed that
other os function calls work. e.g., os.getcwd(), which are
in the __all__ of javaos. I have printed sys.path and see
the jython Lib directory in it.
I'm relatively new to Jython so any help/comments are
appreciated.
Hugh

Hi!
I'm struggling to get the Java-class reloading to work.=20
I'm not very succesful.
Does anyone have a good example to share?=20
I tried this, but it didn't work:
import jreload
classpath=3D["V:\\eonworx\\output\\contrib\\studio\\classes"]
classesLoadSet=3Djreload.makeLoadSet("classes",classpath)
print classesLoadSet
from classes import Hello
jreload.reload(classesLoadSet)
def rl():
jreload.reload(classesLoadSet)
def runHello():
Hello().sayHello()
When I run rl() the Hello-class is still not reloaded and runHello() =
still produces the old result.
gustav bostr=F6m
the eon company
http://www.eoncompany.com
o +46-8-54528800
m +46-70-9575154
e gustav.bostrom@...