|
From: <bc...@wo...> - 2001-02-28 13:15:26
|
On Tue, 27 Feb 2001 07:30:57 -0600, you wrote:
>The reason I ask is I have some code which wants to iterate an instance
>using __len__ and __finditem__ magic methods on any sequence.
Incidently, I hope you are aware that __len__ are allowed to lie. The
only correct way to iterate over a sequence from java are:
PyObject item;
for (int i = 0; (item = seq.__finditem__(i)) != null; i++)
;
>I'll accept
>anything with those two methods and I thought the Collection classes where
>wrapped as such (thus my previous question about CollectionProxy; after
>thinking about, it makes sense as to why Java classes come in as
>PyJavaInstance instances.). In the meantime I do this:
>
>protected boolean isSeq(PyObject object) {
> if(object instanceof PyJavaInstance) {
> List list = (List)object.__tojava__(List.class);
> if(list == Py.NoConversion) {
> return false;
> }
> object = new PyList((PyObject[])list.toArray(new PyObject[list.size()]));
> }
> return object.__findattr__("__finditem__") != null &&
>object.__findattr__("__len__") != null;
>}
It should probably use __findattr__("__getitem__") if you want it to
succeed on python classes with a "def __getitem__(self,idx):" method.
OTOH, the best way of testing an objects sequence-ness is by indexing
it:
protected boolean isSeq(PyObject object) {
try {
object.__finditem__(0);
} catch (PyException exc) {
return !Py.matchException(exc, Py.AttributeError);
}
return true;
}
Generally the python language is very weak in this array. Jython's
addition of java object does not improve the situation at all.
>I'll look into writing the Java->Python wrapper factory and the developers
>can do what they want with it.
>
>As for subclass support, would not Class.isAssignableFrom(Class) be
>sufficient for handling these cases. I'm thinking of this off the top of
>my head so I'll have to test it out first.
Indeed that would work.
Implementation wise I can come up with two simple design. One use a
hashtable where the subclass support is difficult to achive, the other
use a list of two-tuples that is traversed sequentually where it is
easy. I think the list is best, but only if it can be self-sorting:
If a user add one wrapper for java.util.Vector and another for
java.util.List, the Vector must be tested before the List in the list. I
don't want the user to be concerned about this sequence.
regards,
finn
|