Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 19th, 2012  |  syntax: None  |  size: 1.29 KB  |  hits: 6  |  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. import inspect
  2. import logging
  3.  
  4. def interrogate(item):
  5.     """Print useful information about item."""
  6.     if hasattr(item, '__name__'):
  7.         logging.debug( "NAME:    " + item.__name__)
  8.     if hasattr(item, '__class__'):
  9.         logging.debug("CLASS:   " + item.__class__.__name__)
  10.     logging.debug("ID:      {0}".format(id(item)))
  11.     logging.debug("TYPE:    {0}".format(type(item)))
  12.     logging.debug("VALUE:   " + repr(item))
  13.     logging.debug("CALLABLE:")
  14.     if callable(item):
  15.         logging.debug("Yes")
  16.     else:
  17.         logging.debug("No")
  18.     if hasattr(item, '__doc__'):
  19.         doc = getattr(item, '__doc__')
  20.     if doc:
  21.         doc = doc.strip()   # Remove leading/trailing whitespace.
  22.         firstline = doc.split('\n')[0]
  23.         logging.debug( "DOC:     ", firstline)
  24.  
  25. def create_public_method_proxy(component, method_projector, baseclass, class_suffix='ServerProxy'):
  26.     interrogate(component)
  27.     methods = inspect.getmembers(component, inspect.ismethod)
  28.     public_methods = { method_projector: method
  29.                       for method_name, method in methods
  30.                       if not method_name.startswith("_")}
  31.     logging.debug(public_methods)
  32.     class_name = component.__class__.__name__ + class_suffix
  33.     proxy_class = type(class_name, (baseclass,), public_methods )
  34.     return proxy_class