Discussion:
[Jython-users] Using Python/Jython Regular Expression syntax from a Java application...
Jesse D. Sightler
2002-04-08 17:04:22 UTC
Permalink
Hello,

Does anyone here have any idea how it would be possible to use Jython's
Regular Expression syntax directly from Java Code?

I know that I could just rely on the ORO implementation of Regular
Expression's, but my understanding is that it still doesn't implement
named groups.

If this is possible, how difficult would it be?

Thanks,
Jess
Brad Buchsbaum
2002-04-09 08:02:06 UTC
Permalink
Hi all,

An article on Java scripting languages has just appeared on the JavaWorld
website:

http://www.javaworld.com/javaworld/jw-04-2002/jw-0405-scripts.html?

Jython is treated rather unfairly I think. I believe JavaWorld publishes
responses to such articles, especially when their facts are wrong.


B. Buchsbaum
Rune Braathen
2002-04-09 09:39:26 UTC
Permalink
Post by Brad Buchsbaum
Hi all,
An article on Java scripting languages has just appeared on the JavaWorld
http://www.javaworld.com/javaworld/jw-04-2002/jw-0405-scripts.html?
Jython is treated rather unfairly I think. I believe JavaWorld publishes
responses to such articles, especially when their facts are wrong.
Well, this guy probably didn't take too much time writing his article
I'm afraid... He states that "Multidemensional arrays are not
supported" in Jython, which is plain wrong, fact-wise and
spelling-wise. :)

His timing results regarding the array allocation also seems a bit far
fetched. Running the following program:

from jarray import *
from java.lang import System

start = System.currentTimeMillis()
a = zeros( 1000000, 'i')
print System.currentTimeMillis() - start

dumps 100 (sometimes 115) on my 1.4 Athlon (well, a bit more funky
than his test platform, but not 10X more funky).

Anyone cared to download his sourcecode, and prepare for a, hm,
retaliation?
--
runeb
Finn Bock
2002-04-09 13:35:53 UTC
Permalink
[Rune Braathen]
Post by Rune Braathen
Post by Brad Buchsbaum
Hi all,
An article on Java scripting languages has just appeared on the JavaWorld
http://www.javaworld.com/javaworld/jw-04-2002/jw-0405-scripts.html?
Jython is treated rather unfairly I think. I believe JavaWorld publishes
responses to such articles, especially when their facts are wrong.
Well, this guy probably didn't take too much time writing his article
I'm afraid... He states that "Multidemensional arrays are not
supported" in Jython, which is plain wrong, fact-wise and
spelling-wise. :)
Creating multidimensinal java arrays isn't straightforward. It is
possible to do, but it is so cryptic that we really should have a FAQ
entry about it.
Post by Rune Braathen
His timing results regarding the array allocation also seems a bit far
fetched.
Here is his allocArray.py benchmark. The work of assigning values to the
elements is done twice (and his grasp of the 'for' loop isn't quite
right).

from jarray import array

def allocArray(count):
newList = array(range(0, count), 'i')
for index in newList:
newList[index] = index
print "done"

bench = 100000;
print "allocating and initializing a ", bench , " element array..."
allocArray(bench);
Post by Rune Braathen
Anyone cared to download his sourcecode, and prepare for a, hm,
retaliation?
As long as we are in 1. place, does it really matter how much first we
are? I don't think so.

regards,
finn
Michel Pelletier
2002-04-09 14:13:14 UTC
Permalink
Post by Finn Bock
Post by Rune Braathen
Well, this guy probably didn't take too much time writing his article
I'm afraid... He states that "Multidemensional arrays are not
supported" in Jython, which is plain wrong, fact-wise and
spelling-wise. :)
The comment that jython doesn't come with a debugger is also incorrect.
Post by Finn Bock
As long as we are in 1. place, does it really matter how much first we
are? I don't think so.
I agree, although the author may want to know that the code and some of the
comments are incorrect, if only just so he/she learns something. ;)

-Michel
Finn Bock
2002-04-09 13:18:25 UTC
Permalink
[Brad Buchsbaum]
Post by Brad Buchsbaum
Hi all,
An article on Java scripting languages has just appeared on the JavaWorld
http://www.javaworld.com/javaworld/jw-04-2002/jw-0405-scripts.html?
Thanks for telling us about the article.
Post by Brad Buchsbaum
Jython is treated rather unfairly I think.
It is not that bad IMO. After all, he admit that he is a python newbie
and yet his wrong, inefficient and unoptimized little benchmarks shows
jython quite favoriable.

regards,
finn
Gordon Scott
2002-04-09 15:57:09 UTC
Permalink
being a newbie to python and jython myself,
can someone explain what is incorrect about this
for loop?

Thx.

-----Original Message-----
From: ***@worldonline.dk [mailto:***@worldonline.dk]
Sent: Tuesday, April 09, 2002 8:28 AM
To: jython-***@lists.sourceforge.net
Cc: Rune Braathen
Subject: Re: [Jython-users] Re: Jython panned in JavaWorld article


Here is his allocArray.py benchmark. The work of assigning values to the
elements is done twice (and his grasp of the 'for' loop isn't quite
right).

from jarray import array

def allocArray(count):
newList = array(range(0, count), 'i')
for index in newList:
newList[index] = index
print "done"

bench = 100000;
print "allocating and initializing a ", bench , " element array..."
allocArray(bench);
Kevin J. Butler
2002-04-09 20:31:20 UTC
Permalink
Changed my
Jesse D. Sightler
2002-04-09 16:33:33 UTC
Permalink
I haven't seen an answer, so I'll answer this with another question. ;)

Would the easiest way to do this be to write a thin class in Jython
wrapping the regex API, and compile this with Jythonc? It would seem
like that should make a simple interface to the Python Regex code easily
accessible from the Java side, anyway.

Or is there a better way to do this?

Thanks,
Jess
Post by Jesse D. Sightler
Hello,
Does anyone here have any idea how it would be possible to use Jython's
Regular Expression syntax directly from Java Code?
I know that I could just rely on the ORO implementation of Regular
Expression's, but my understanding is that it still doesn't implement
named groups.
If this is possible, how difficult would it be?
Thanks,
Jess
Mike Hostetler
2002-04-09 16:46:47 UTC
Permalink
Post by Jesse D. Sightler
Would the easiest way to do this be to write a thin class in Jython
wrapping the regex API, and compile this with Jythonc? It would seem
like that should make a simple interface to the Python Regex code easily
accessible from the Java side, anyway.
I didn't answer, because I didn't know <g> but that would be how I would
try it . . .
Post by Jesse D. Sightler
Or is there a better way to do this?
I used ORO Matcher one to do some pretty significat regex substituion in
Java. I created a HashTable of regular expression statements matched
with the string they were supposed to substitute in. I gave my class the
hashtable and the string and matched it. This may or may not be better,
depending on what you are trying to accomplish.

It was one of the first non-trivial things I have written in Java, and I
remember stumbling through it. But if you have some experience with
Java, than it should be pretty easy.

I may have a copy of it somewhere. If you want to see it, mail me off
the list.

-- mikeh
Jesse D. Sightler
2002-04-09 18:26:03 UTC
Permalink
Hi Mike,
Post by Mike Hostetler
Post by Jesse D. Sightler
Would the easiest way to do this be to write a thin class in Jython
wrapping the regex API, and compile this with Jythonc? It would seem
like that should make a simple interface to the Python Regex code easily
accessible from the Java side, anyway.
I didn't answer, because I didn't know <g> but that would be how I would
try it . . .
Understandable. :-)
Post by Mike Hostetler
Post by Jesse D. Sightler
Or is there a better way to do this?
I used ORO Matcher one to do some pretty significat regex substituion in
Java. I created a HashTable of regular expression statements matched
with the string they were supposed to substitute in. I gave my class the
hashtable and the string and matched it. This may or may not be better,
depending on what you are trying to accomplish.
It was one of the first non-trivial things I have written in Java, and I
remember stumbling through it. But if you have some experience with
Java, than it should be pretty easy.
I may have a copy of it somewhere. If you want to see it, mail me off
the list.
Ah, yes, I can see where this would be useful. Although, for the tasks
that I normally end up using regex for, it really helps to have named
groups. I seem to frequently end up writing XML description languages
for parsing text using regex and placing the results in a HashMap.

So, I'll end up with something like:
<extractor expression="Name: (.*?) Url= (.*?)\nDescription:(.*)"
value1="name" value2="url" value3="description"/>

That works, but it is more than a bit awkward, considering that the
regex could just as easily have defined names for its own groups. :)

Anyway, thanks for the help. I think that it should be fairly easy to
write a good wrapper for this thing the next time I need this
functionality. :)

----
Jess
Anthony Eden
2002-04-09 17:05:32 UTC
Permalink
This:

newList = array(range(0, count), 'i')

Initializes the array, so there is no reason to loop through the array again with the for loop to assign values (not
sure if that was clear from the previous poster's message).

As for what is wrong with the for loop: I am not sure that there is anything wrong but it is a matter of how he is using
it. Your looping through the values in newList just to assign them to newList again? Seems kind of silly to me as
well.

On a side note: it seems strange that Python's syntax and Jython's syntax for the array() function are different. In
Python the function is defined as array(typecode[, initializer]) wheres in Jython the initializer comes first, followed
by the typecode. Can anyone explain why?

Sincerely,
Anthony Eden
Post by Gordon Scott
-----Original Message-----
Scott
Sent: Tuesday, April 09, 2002 12:06 PM
Subject: RE: [Jython-users] Re: Jython panned in JavaWorld article
being a newbie to python and jython myself,
can someone explain what is incorrect about this
for loop?
Thx.
-----Original Message-----
Sent: Tuesday, April 09, 2002 8:28 AM
Cc: Rune Braathen
Subject: Re: [Jython-users] Re: Jython panned in JavaWorld article
Here is his allocArray.py benchmark. The work of assigning values to the
elements is done twice (and his grasp of the 'for' loop isn't quite
right).
from jarray import array
newList = array(range(0, count), 'i')
newList[index] = index
print "done"
bench = 100000;
print "allocating and initializing a ", bench , " element array..."
allocArray(bench);
_______________________________________________
Jython-users mailing list
https://lists.sourceforge.net/lists/listinfo/jython-users
Finn Bock
2002-04-09 17:27:22 UTC
Permalink
[Jesse D. Sightler]
Post by Jesse D. Sightler
Hello,
Does anyone here have any idea how it would be possible to use Jython's
Regular Expression syntax directly from Java Code?
I'm not entirely sure what you mean here. You can't use the "re" module
from a java program that does not use jython.

But if your program already include jython, you can import the "re"
module and access its function by using the Jython/Java API. Something
along the lines of:

import org.python.core.*;

public class test {
public static void main(String[] args) {
PySystemState.initialize();
PyObject builtins = Py.getSystemState().builtins;
PyObject re = builtins.__finditem__("__import__").__call__(
Py.newString("re"));
PyObject pattern = re.invoke("compile",
Py.newString("H(?P<x>.*)d"));
PyObject match = pattern.invoke("search",
Py.newString("Hello world"));
System.out.println(match.invoke("group",
Py.newString("x")));
}
}
Post by Jesse D. Sightler
I know that I could just rely on the ORO implementation of Regular
Expression's, but my understanding is that it still doesn't implement
named groups.
I think that is correct.

regards,
finn
Jesse D. Sightler
2002-04-09 18:28:02 UTC
Permalink
Hi Finn,

That also is roughly what I was looking for, although, I would imagine
that I would still need to write some sort of wrapper class to hide the
ugliness of calling into Jython code from Java.

Thanks for the idea,
Jess
Post by Finn Bock
I'm not entirely sure what you mean here. You can't use the "re" module
from a java program that does not use jython.
But if your program already include jython, you can import the "re"
module and access its function by using the Jython/Java API. Something
import org.python.core.*;
public class test {
public static void main(String[] args) {
PySystemState.initialize();
PyObject builtins = Py.getSystemState().builtins;
PyObject re = builtins.__finditem__("__import__").__call__(
Py.newString("re"));
PyObject pattern = re.invoke("compile",
Py.newString("H(?P<x>.*)d"));
PyObject match = pattern.invoke("search",
Py.newString("Hello world"));
System.out.println(match.invoke("group",
Py.newString("x")));
}
}
C***@ot.com.au
2002-04-10 07:55:03 UTC
Permalink
Post by Finn Bock
As long as we are in 1. place, does it really matter how much first we
are? I don't think so.
I think it may not matter to those who already know but
for the others who are looking for a language to use and
from reading this article, they may never consider using
Jython.

Chai
+-
| mailto:***@ozemail.com.au | +61 401 688 408
| http://www.ozemail.com.au/~calcium
| Children need encouragement. So if a kid gets an answer
| right, tell him it was a lucky guess. That way, he
| develops a good, lucky feeling - Jack Handy

Loading...