From: john c. <joh...@ya...> - 2001-08-07 22:08:08
|
Yes, something of this nature, but not quite like that since several layers of code must still be traversed before getting to the meat and potatoes slowing it down. It is important that this interface be transparent (like a ghost) since that is largely the beauty of it. For demonstration purposes I'm embedding the feature in directly into the PyInstance. I define a new private method called setSkeleton in PyInstance.java. I add a bit of code in __setattr__ and __findattr__ which will search the skeleton instance first before self. When setting the attribute I check to see if it is a "basis type" (i.e. float int string). If it is not I must check that object to see if it has a skeleton. If so, set the ghost attribute with the ob's skeleton. I include an extra optional argument to init args called __skeleton__. If it's None I do nothing, if it is defined, I set the PyInstance's skeleton class before we assign attributes to the instance. This way the skeleton will automatically be initialized since __setattr__ and __findattr__ have been slightly modified. What is important about the nature of ghosting is that information is stored in both the java native skeleton and the real PyObject. The same information in two places. Somewhat of an OO no no. But I am willing to make this trade off to get 500x performance boost. Since all it requires is a bit a maintenance work in well tucked away places. This seems funny, and alot of work, but really isn't. Once you see the speed boost obtained from doing bulk processing you may deem this feature worthwhile. It does retain a great deal of python flexibility. Provided a small set of attributes are not deleted. That should throw an exception. thanks for your comments. have been very helpful. -john --- Samuele Pedroni <pe...@in...> wrote: > Hi. > > I'm still a bit confused, how far is this (up to > PyObject subclassing and > other not directly related issues) from what you > would like? > > <jc.py> > > import new > > class X: > def __init__(self,c): > self.c = c > > def add(self,a,b): > return self.c*(a+b) > > def mul(self,a,b): > return self.c*a*b > > x=X(2) > print repr(x.__class__) > print X.add > print X.mul > print x.add(1,2) > print x.mul(3,2) > > def jmerge(cls,jskel): > new_cls = > new.classobj(cls.__name__,(jskel,cls),{}) > new_cls.__module__ = cls.__module__ > return new_cls > > import Xj > > X=jmerge(X,Xj) > x=X(2) > print repr(x.__class__) > print X.add > print X.mul > print x.add(1,2) > print x.mul(3,2) > > print x.describe() > > </jc.py> > > Output: > <class __main__.X at 5783932> > <unbound method X.add> > <unbound method X.mul> > 6 > 12 > <class __main__.X at 3462044> > <java function add at 3199804> > <unbound method X.mul> > 6 > 12 > Xj(c=2) > > > Consider, Xj is the following java class: > > > public class Xj { > > public int c; > > public int add(int a,int b) { > return c*(a+b); > } > > > public String describe() { > return "Xj(c="+c+")"; > } > > } > > > > regards, Samuele Pedroni. > __________________________________________________ Do You Yahoo!? Make international calls for as low as $.04/minute with Yahoo! Messenger http://phonecard.yahoo.com/ |