Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python generators in Java?

Reply
Thread Tools

Python generators in Java?

 
 
Robert Oschler
Guest
Posts: n/a
 
      11-15-2004
Preamble:

- I know this is the Python forum
- I know about (and have used) Jython

I already posted this question in comp.lang.java. But after a week I have
still not received a single reply.

One of my favorite Python features is generators. I have to use Java for
one particular project (it happens). I would like to have something at
least close to Python's generators for the project.

Jython won't do because last I checked it was dormant, and has been for
quite some time. In addition, the version that it is "frozen" at (2.1)
does not contain generators (if this is wrong on any point, *please* point
me to a URL for a current version). I checked the mailing list archive and
except for a single recent post from a party external to the Jython project,
there hasn't been any mailing list activity since 2002. In fact, I saw my
two unanswered posts about Jython and generators from 2002. There was a
2.2a (alpha release) in July of 2003 with lots of warnings about using the
code in a production environment, but nothing since.

So does anyone here that has Python and Java experience have any ideas on
this?

Also, has anybody had any experience integrating CPython with Java, if it is
at all possible?

Thanks.

--
Robert Oschler
http://www.robotsrule.com/phpBB2/
Robot & Android Discussion Forum


 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      11-15-2004
Robert Oschler wrote:
> Preamble:
> Jython won't do because last I checked it was dormant, and has been for
> quite some time. In addition, the version that it is "frozen" at (2.1)
> does not contain generators (if this is wrong on any point, *please* point
> me to a URL for a current version).

Yes, that is still the current production release.

I checked the mailing list archive and
> except for a single recent post from a party external to the Jython project,
> there hasn't been any mailing list activity since 2002.


??? There is continuous, moderate activity on the ML. Try
http://sourceforge.net/mailarchive/f...?forum_id=5586

In fact, I saw my
> two unanswered posts about Jython and generators from 2002. There was a
> 2.2a (alpha release) in July of 2003 with lots of warnings about using the
> code in a production environment, but nothing since.


There has been some encouraging news about a Jython update in the works.
See the news at http://www.jython.org
We won't have generators in Jython soon but there is progress.

Kent
 
Reply With Quote
 
 
 
 
Alan Kennedy
Guest
Posts: n/a
 
      11-15-2004
[Robert Oschler]
> Preamble:
>
> - I know this is the Python forum
> - I know about (and have used) Jython
>
> I already posted this question in comp.lang.java. But after a week I
> have still not received a single reply.
>
> One of my favorite Python features is generators. I have to use Java
> for one particular project (it happens). I would like to have
> something at least close to Python's generators for the project.



Hi Robert,

Hmm, I don't see a direct question in your post, so I'll just assume
that you're generally inquiring about generator style functionality in java.

If you're looking to achieve the execution efficiency of python
generators, e.g. in terms of processing sequences one value at a time
rather than generating the entire sequence and then processing, then you
should look at java iterators, which are designed specifically for this
purpose.

Note that java 1.5 brings new syntactic features which support
iterators, e.g. short-hand for loops like this

public int sumArray(int array[]) {
int sum = 0;
for(int i : array) {
sum += i;
}
return sum;
}

Quite pythonic, IMHO.

http://java.sun.com/developer/techni...2se15langfeat/

However, if you're looking for the resumable-functions aspect of
generators, you're out of luck: java doesn't not support them. But note
that a java iterator will still very likely be more efficient than a
python generator, albeit that the code won't be as clean.

Note that there is a currently a proposal from the codehaus people to
add continuations to java, which could make both generators and full
coroutines possible in java.

http://docs.codehaus.org/display/continuation/Home

Lastly, it would probably be easier to discuss this subject if you gave
an example of the type of thing you want to do, and some reasons why you
want to do it that way. Post some python: maybe the java would be
straightforward.

Regards,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
 
Reply With Quote
 
Daniel Dittmar
Guest
Posts: n/a
 
      11-15-2004
Robert Oschler wrote:
> One of my favorite Python features is generators. I have to use Java for
> one particular project (it happens). I would like to have something at
> least close to Python's generators for the project.


Sketch:
- create a Queue class
- the 'generator' would run in a thread and put objects into the queue
(equivalent to yield)
- the 'main' program would run in the main thread and consume objects
from the queue (equivalent to calling generator.next ())
- the consumer must block if the queue is empty
- the producer must block if the number of objects in the queue exceeds
a certain amount (queue size = 1 is perfectly acceptable unless the
producer is I/O bound)
- if you use generators to walk recursive structures, then you probably
need only one queue instead of one generator for each level.
- perhaps you can get away with one superclass that creates both queue
and thread, the 'generator' could then be implemented as an anonymous class.

Daniel
 
Reply With Quote
 
Robert Oschler
Guest
Posts: n/a
 
      11-15-2004

"Alan Kennedy" <(E-Mail Removed)> wrote in message
news:I%4md.41892$(E-Mail Removed)...
>
> Hi Robert,
>
> Hmm, I don't see a direct question in your post, so I'll just assume
> that you're generally inquiring about generator style functionality in

java.
>
> If you're looking to achieve the execution efficiency of python
> generators, e.g. in terms of processing sequences one value at a time
> rather than generating the entire sequence and then processing, then you
> should look at java iterators, which are designed specifically for this
> purpose.
>
> Note that java 1.5 brings new syntactic features which support
> iterators, e.g. short-hand for loops like this
>
> public int sumArray(int array[]) {
> int sum = 0;
> for(int i : array) {
> sum += i;
> }
> return sum;
> }
>
> Quite pythonic, IMHO.
>
> http://java.sun.com/developer/techni...2se15langfeat/
>
> However, if you're looking for the resumable-functions aspect of
> generators, you're out of luck: java doesn't not support them. But note
> that a java iterator will still very likely be more efficient than a
> python generator, albeit that the code won't be as clean.
>
> Note that there is a currently a proposal from the codehaus people to
> add continuations to java, which could make both generators and full
> coroutines possible in java.
>
> http://docs.codehaus.org/display/continuation/Home
>
> Lastly, it would probably be easier to discuss this subject if you gave
> an example of the type of thing you want to do, and some reasons why you
> want to do it that way. Post some python: maybe the java would be
> straightforward.
>
> Regards,
>
> --
> alan kennedy
> ------------------------------------------------------
> email alan: http://xhaus.com/contact/alan


Alan,

Yes it's the resumable aspect of generators I'm after. For me that is their
main appeal. I'm familiar with iterators from C++, but it's the ability to h
ave a function that maintains it's own "state" (generator) that is
attractive. It simplifies a lot of thorny programming problems.

Thanks for the link on Java continuations, I'll check that out.

--
Robert Oschler
http://www.robotsrule.com/phpBB2/
Robot & Android Discussion Forum


 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      11-18-2004
On Mon, 15 Nov 2004 16:44:46 +0000, rumours say that Alan Kennedy
<(E-Mail Removed)> might have written:

>http://java.sun.com/developer/techni...2se15langfeat/


In that page, under "Autoboxing/Unboxing", there is the following
sentence:

"""
In a pure object-oriented language there should be no difference between
a primitive data type and a reference, as everything is an object.
"""

How sweet... a Sun document for Java that can be used for Python
advocacy: "Python is a pure object-oriented language"
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using generators for event handling in python. Robin Bryce Python 4 09-04-2004 09:00 AM
Generators: section 9.10 of the python tutorial David Stockwell Python 3 05-13-2004 07:38 PM
Python HTML Documentation Generators Mikey At Work Python 3 02-25-2004 03:06 PM
Scripting C++ Game AI object using Python Generators The_Incubator Python 4 01-06-2004 12:17 AM
Python parser generators anton muhin Python 2 11-28-2003 07:37 PM



Advertisments