Hi,
I remember at pynw a few months ago more people than I'd expect saying
they used metaclasses often, so I figured why not post this here :-)
So, I've been playing about with trying to expose one API to users of
instance of a class, and another to the instance, such that they look
the same. The reason is so that you can have a single actor class
whereby objects inside it and outside it use the same API, but the
users calls outside can be funnelled through a message queue so that
stuff is threadsafe.
eg aiming for something like this: (naff example)
class Ship(Actor):
def __init__(self,x,y):
self.moveto(x,y)
@threadsafe
def moveto(self, x,y):
self.x = x
self.y = y
def wombat(self):
print "Wombat!"
s = Ship(100,100)
s.moveto(20,20)
Whereby self.moveto is a plain old function call, whereas s.moveto
goes through a proxy, and such that the method "wombat" is only
accessible by the instance, but not a user of the instance.
Anyway, I've got the privacy aspect and bouncing through a proxy bit
working:
http://pastebin.com/tz8Fcv8t
This results in calls inside the class happening directly and calls
outside the class being funneled through a proxy class, and attempted
calls to methods not marked as threadsafe from outside the instance
failing. So far so good.
Then normally I've be able to do this sort of this - replace this:
class Store(object):
__metaclass__ = AttributeHiding
def __init__(self, **defaults):
self.__dict__.update(defaults)
with:
class Actor(object):
__metaclass__ = AttributeHiding
class Store(Actor):
def __init__(self, **defaults):
self.__dict__.update(defaults)
... and Store would have the same __metaclass__ as Actor and it'd Just
Work. This also then means people would just subclass Actor for things
to work like this.
However, I've done something that breaks this, and I'm not sure
what :-)
It's probably caused by...
klass = type.__new__(meta, name, bases, newdict)
theKlass = mydec(klass) # ... THIS LINE ...
return theKlass
But I'm not totally certain how to fix it. Maybe a metaclass for the
Proxy class (to fudge things like bases, etc cleanly) created inside
the class decorator called by the metaclass ?
For reference this isn't for use in any actual code (yet), it's just
me messing around and confuddling myself :-D
Any suggestions, for fixing it, welcome. :-)
(Suggestions along the lines of "don't do that", will be met with a
rolling of eyes :-D )
Michael.
--
To post: [email protected]
To unsubscribe: [email protected]
Feeds: http://groups.google.com/group/python-north-west/feeds
More options: http://groups.google.com/group/python-north-west