Hi Andi,
thanks for the feedback! I revised the code and send you attached a new
patch.
I also attach a short demo script that shows the problems I mentioned
earlier when trying to initialize an ArrayList with a JavaSet (or JavaList)
containing integers.
Finally I'd suggest to rename collections.py because there's one defined on
Python lib already:
http://docs.python.org/library/collections.html
Below are some comments to your comments...
Regards,
Thomas
> -----Ursprüngliche Nachricht-----
> Von: Andi Vajda [mailto:[email protected]]
> Gesendet: Sonntag, 26. Februar 2012 23:29
> An: [email protected]
> Betreff: Re: AW: AW: Setting Stopword Set in PyLucene (or using Set in
general)
>
> ...
> According to the javadocs, this method is supposed to throw
NoSuchElementException. Raising StopIteration is not going to do the trick.
> Same comment on the previous method too.
Ok, I was unsure on how to properly throw a Java Exception in Python code -
and couldn't find an example.
Also I thought a Java Exception type should be exported in lucene - this is
not the case however:
>>> lucene.NoSuchElementException
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'NoSuchElementException'
I imagine I could
- add the java.util.NoSuchElementException to the Makefile to get it
generated by JCC and throw it via raise?
- use lucene.JavaError and pass 'java.util.NoSuchElementException' name in
the constructor?
- extend / use PythonException?
- defined Python Exception class
NoSuchElementException(exceptions.Exception) and raise that one?
- raise RuntimeError, 'NoSuchElementException' and raise that one?
- define some helper methods for 'native' Java Exceptions in PythonList.java
and call 'em
Which one does the trick? Unless I know better I go with the last one...
(I understand PythonException is used by JCC to wrap errors that escape from
Python to Java and JavaError is used by JCC for Java Exceptions that escape
from Java to Python - but how do you 'fake' a Java Exception within Python?)
Same problem for IndexOutOfBoundsException in get()
> Why not also implement remove() and set() ?
Because they are optional ... I've implemented them now.
+ def lastIndexOf(obj):
> Wouldn't it be more efficient to iterate backwards until the element is
found instead of copying the list (self._lst[::-1]) and iterate forwards ?
Done.
+ def remove(self, obj_or_index):
+ if type(obj_or_index) is type(1):
+ return removeAt(int(obj_or_index))
+ return removeElement(obj_or_index)
> It's better to do this at the Java level.
> Declare differently named native methods for each overload of remove() and
implement remove(int) in Java to call removeInt(int) and remove(Object) to
call removeObject(Object
Done. The different methods are declared private now.
+
+ def subList(fromIndex, toIndex):
+ sublst = self._lst[fromIndex:toIndex]
+ return JavaList(sublst)
> The javadoc expects this method to throws IndexOutOfBoundsException
instead of behaving nice like a Python slice.
This check (and Exception handling) is done on java-level now.
+public class PythonListIterator extends PythonIterator implements
ListIterator {
+
+ // private long pythonObject;
+
+ public PythonListIterator()
+ {
+ }
+
+ /* defined in super class PythonIterator:
...
> If this work, you don't need pythonObject to be protected anymore in the
superclass then ?
True - just reverted the changes in PythonIterator.
import sys,os
import lucene
from collections import JavaSet, JavaList
def testStringSet():
s = set(['a','b','c'])
print "\nSet of Strings: ", s
# create python wrapper for Java Set
# NOTE: this Python class extends/implements the Java class lucene.PythonSet
ps = JavaSet(s)
print "created: ",ps, type(ps)
size = ps.size()
print "size: " , size
assert(size == len(s)), "size"
has = ps.contains('b')
print "contains('b'): ", has
assert(has is True), "contains"
# create HashSet in JVM
js = lucene.HashSet(ps)
print "created: ",js, type(js)
assert(size == js.size()), "size"
assert(js.contains('b') is True), "contains"
ar = js.toArray()
print "toArray:", ar
# create ArrayList in JVM
jl = lucene.ArrayList(ps)
print "created: ",jl, type(jl)
assert(size == jl.size()), "size"
sl = jl.subList(1,3)
print "sublist:", sl
def testStringList():
l = ['a','b','c']
print "\nList of Strings: ",l
# create python wrapper for Java List
# NOTE: this Python class extends/implements the Java class
lucene.PythonList
pl = JavaList(l)
print "created: ",pl, type(pl)
size = pl.size()
print "size: " , size
assert(size == len(l)), "size"
pos = pl.indexOf('b')
print "indexOf('b'): ", pos
assert(pos == 1), "indexOf"
# create HashSet in JVM
js = lucene.HashSet(pl)
print "created: ",js, type(js)
assert(size == js.size()), "size"
assert(js.contains('b') is True), "contains"
ar = js.toArray()
print "toArray:", ar
# create ArrayList in JVM
jl = lucene.ArrayList(pl)
print "created: ",jl, type(jl)
assert(size == jl.size()), "size"
assert(pos == jl.indexOf('b')), "indexOf"
sl = jl.subList(1,3)
print "sublist:", sl
def testIntSet():
s = set(range(10))
print "\nSet of Integers: ",s
ps = JavaSet(s)
print "created: ",ps
print "size: " , ps.size()
print "contains(2): ", ps.contains(2)
# create ArrayList in JVM
# TODO: this results in lucene.JavaError
jl = lucene.ArrayList(ps)
print "created: ",jl
def testIntList():
x = range(10)
print "\nList of Integers: ",x
pl = JavaList(x)
print "created: ",pl
print "size: " , pl.size()
print "indexOf(2): ", pl.indexOf(2)
# create ArrayList in JVM
# TODO: this results in lucene.JavaError
jl = lucene.ArrayList(pl)
print "created: ",jl
if __name__ == '__main__':
lucene.initVM()
testStringSet()
testStringList()
testIntSet()
testIntList()