[David Primmer]

| I'm going to be writing a lot of ADSI scripts in python and I thought 
| I'd wrap the IADsUser object. Right now, my custom user 
| object "myUser", 
| simply looks up the user in AD, does a GetObject() on it and the 
| resulting IADsUser is a member of the class. So, if I want to get the 
| name of the user in AD, I access it with "myUser.IADsUser.Name".
| 
| However, I'd really like to simply subclass IADsUser and add a few 
| attributes of my own, so myUser.Name is actually pointing to the 
| IADsUser object's name. I don't have the skills to do anything as 
| dynamic as Tim Golden's WMI wrapper. How would I do this subclassing?

Without answering your actual question, what the WMI wrapper does
is broadly known as delegation or proxying. There's nothing terribly 
difficult about doing it -- luckily, or I'd never have managed! -- so, 
depending on your exact requirements, something like this may serve 
your needs:

<code>
#
# This could well be someone else's class
#  outside your control, eg IADsUser.
#
class UsefulClass:
  def __init__ (self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

  def do_stuff (self, stuff):
    print stuff

class MyOwnClass:
  def __init__ (self, x, y, z, a, b):
    self.__dict__["_useful"] = UsefulClass (x, y, z)
    self.a = a
    self.b = b

  def __getattr__ (self, attribute):
    return getattr (self._useful, attribute)

mine = MyOwnClass (1, 2, 3, 4, 5)
print mine.a, mine.b
print mine.x, mine.y, mine.z
print mine.x is mine._useful.x

mine.do_stuff ("blahblah")

</code>

Notes:
1) __getattr__ *only* fires when an unknown attribute
is referenced; ie it won't fire for mine.a or mine.b
since these are recognised attributed of the "mine" instance.

2) The self.__dict__ stuff at the top of the __init__ for
MyOwnClass is because the __getattr__ will fail with a
recursion overflow error until you've defined an attribute
_useful for the MyOwnClass instance. (Try it and see). The
self.__dict__ stuff is a sneaky way of getting something
into the instance's namespace while bypassing the __getattr__
mechanism. If there's some more elegant way to do this, I'm
happy to hear about it!

This basic approach is quite straightforward and, clearing
away the clutter, comes down to holding an instance of the
"other" class inside your own and setting up a __getattr__
to proxy attribute calls across. Of course you can get fancy
with filtering which calls get passed etc. but this basic
approach is sufficient for many needs.

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to