Using metaclassed to dynamically generate a class based on a parameter to the objects init function.

2006-06-22 Thread sashang
Hi

I'd like to use metaclasses to dynamically generate a class based on a
parameter to the objects init function.

For example:

class MetaThing(type):
def __init__(cls, name, bases, dict, extra_information):
super(MetaThing, cls).__init__(name, bases, dict)
#setup the class based on the parameter extra_information

class Thing:
__metaclass__ = MetaThing
def __init__(self, extra_information):
 #Somehow pass extra_information to the MetaThing

extra_information = 1
t = Thing(extra_information)

The above sample won't work but I hope it demonstrates what I'm trying
to do.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using metaclassed to dynamically generate a class based on a parameter to the objects init function.

2006-06-23 Thread sashang

Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] wrote:
> > Hi
> >
> > I'd like to use metaclasses to dynamically generate a class based on a
> > parameter to the objects init function.
>
> Do you really need a metaclass for this ?
>
> > For example:
> >
> > class MetaThing(type):
> > def __init__(cls, name, bases, dict, extra_information):
> > super(MetaThing, cls).__init__(name, bases, dict)
> > #setup the class based on the parameter extra_information
> >
> > class Thing:
> > __metaclass__ = MetaThing
> > def __init__(self, extra_information):
> >  #Somehow pass extra_information to the MetaThing
> >
> > extra_information = 1
> > t = Thing(extra_information)
>
> Why would you want a new *class* here ?
>
> > The above sample won't work but I hope it demonstrates what I'm trying
> > to do.
>
> Not enough, I'm afraid - unless it's just me being dumb. From what I see
> here, you just can add the extra informations on the object in the
> initializer. What's your *real* use case ?
>
>

The extra_information is used in MetaThing to tell it what attributes
to add to the class. For example:

class MetaThing(type):
 def __init__(cls, name, bases, dict, extra_information):
 super(MetaThing, cls).__init__(name, bases, dict)
 #setup the class based on the parameter extra_information
 setattr(cls, make_name(extra_information),
make_object(extra_information))

Does that clarify things? I might have the wrong approach - I'm new to
metaclasses. However I do think the solution to my problem lies with
them since I have to dynamically generate a class and metaclasses
provide a mechanism for doing this.

-- 
http://mail.python.org/mailman/listinfo/python-list


mirroring object attributes using xml-rpc

2006-07-05 Thread sashang
Hi

Say I have a class like the following

class Test:
  i = 1

  def geti(self):
return self.i

And I use it in an xml-rpc server like this:

t = Test()
s.register_instance(t)

Then the client code can get the value of i like this:
c = xmlrpclib.ServerProxy("address")
c.geti()

but why can't I get the value of i like this?

c.i

How can I implement such behaviour? Moreover, if there were more member
objects in the class Test, how can tell the client 1) that these
attributes exist and 2) how can I use them from the client side without
having to resort to defining a function get_xxx() in the server class?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mirroring object attributes using xml-rpc

2006-07-05 Thread sashang
Just to make things clearer the problem I have is  if I do this:

c = xmlrpclib.ServerProxy("http://somewhere";)
c.i

I get this error:



So how do I fake things so that xmlrpc knows not to try and call i but
gets the value of i instead?

-- 
http://mail.python.org/mailman/listinfo/python-list