Hi Thomas,
On Wed, 1 Feb 2012, Thomas Koch wrote:
OK, I found a solution (obviously not the best one...): lucene.Set is
representing a java.util *interface* Set<E> which of course cannot be
instantiated. HashSet is an implementing class, and can be instantiated. You
can add elements via the add() method to the set then. Example:
def get_lucene_set(python_list):
"""convert python list into lucene.Set (Java.util.set interface)
using the HashSet class (java.util) wrapped in lucene.HashSet
"""
hs = lucene.HashSet()
for el in python_list:
hs.add(el)
return hs
However I'm still looking for a more elegant constructor that would allow to
create a HashSet from a python set (or list). Is that available/possible?
In pylucene's python directory, there is a file called collections.py that
has what you're looking for, I think.
It's a Python class called JavaSet, that extends a PythonSet class which is
an extension point for the java.util.Set interface. PythonSet implements all
the java.util.Set methods by calling the corresponding python methods on the
JavaSet python class. PythonSet itself is defined in
java/org/apache/pylucene/util/PythonSet.java.
With this pair of classes you have a Python-backed set object being
integrated with Java via a java.util.Set implementation.
The same holds for lists like the ArrayList (from java.util too) which
implements the Collection interface:
And the very same could be done for java.util.ArrayList. It should be easy
enough by following the JavaSet/PythonSet example.
If you send in a patch that implements this, I'd be glad to integrate it !
Andi..
Example:
l =range(3)
l
[0, 1, 2]
a = lucene.ArrayList(l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
lucene.InvalidArgsError: (<type 'ArrayList'>, '__init__', ([0, 1, 2],))
using the for-in-do obj.add "trick" allows to generate a 'filled' instance
here as well : <ArrayList: [0, 1, 2]>
but wouldn't it be nice to be able to create an instance more "pythonic"?
I'm not a Java expert (nor do I know much about the Collections API), so
maybe it's even impossible in Java to create an instance of a
List,Vector,HashSet (whatever) and passing some literals (like Strings) -
who knows... So if anyone has a better idea how to do this in PyLucene
please let me know ,-)
regards,
Thomas