Want more features on Pastebin? Sign Up, it's FREE!
Guest

Wrapper to provide Pydoc for opaque (undocumented) objects

By: a guest on Jun 26th, 2012  |  syntax: Python  |  size: 1.09 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class DocWrap(object):
  2.   '''
  3.  This class provides a simple base class for wrapper objects around opaque,
  4.  undocumented blobs. Instances should provide custom doc strings to make their opaque
  5.  contents appear as a documented Python object.
  6.  '''
  7.  
  8.   def __init__(self, obj, docstr):
  9.     '''
  10.    initialise DocWrap object with given opaque instance and docstring for it.
  11.    '''
  12.     self.__instance__ = obj
  13.     self.__doc__ = docstr
  14.  
  15.   def __setattr_(self, name, value):
  16.     if name == '__instance__':
  17.       object.__setattr__(self, name, value)
  18.     else:
  19.       object.__setattr__(self.__instance__, name, value)
  20.  
  21.   def __getattribute__(self, name):
  22.     if name == '__doc__':
  23.       return object.__getattribute__(self, name)
  24.     elif name == '__instance__':
  25.       return object.__getattribute__(self, name)
  26.     else:
  27.       return object.__getattribute__(self.__instance__, name)
  28.  
  29. test= DocWrap(object(), 'hello world!')
  30. ex = DocWrap(Exception(), 'some other type for comparison of dir() output')
  31. print(test.__doc__) # should appear as 'hello world!'
  32. print(dir(test))
  33. print(dir(ex))
clone this paste RAW Paste Data