daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Jun 23rd, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pydoc
  2. import re
  3.  
  4.  
  5. def shape_text(this_cls, doc=pydoc.plaintext):
  6.     attrs = [
  7.         (name, kind) for name, kind, cls, _ in pydoc.classify_class_attrs(this_cls)
  8.         if cls == this_cls
  9.     ]
  10.     attrs = [
  11.         (name, kind) for name, kind in attrs if not (name.startswith("__") and name.endswith("__"))
  12.     ]
  13.     attrs = [(name, kind) for name, kind in attrs if not name.startswith("_")]
  14.     method_names = [name for name, kind in attrs if kind == "method"]
  15.     method_annotations = [
  16.         "@OVERRIDE: " if any(c for c in this_cls.mro()[1:] if hasattr(c, name)) else ""
  17.         for name in method_names
  18.     ]
  19.     method_docs = [
  20.         prefix + doc.document(getattr(this_cls, name))
  21.         for prefix, name in zip(method_annotations, method_names)
  22.     ]
  23.  
  24.     content = doc.indent("".join(method_docs))
  25.     mro = " <- ".join([cls.__name__ for cls in this_cls.mro()])
  26.     return "\n".join([mro, content])
  27.  
  28.  
  29. def grep_by_indent(s, level, rx=re.compile("^\s+")):
  30.     for line in s.split("\n"):
  31.         m = rx.search(line)
  32.         if m is None or len(m.group(0)) <= level:
  33.             yield line
  34.  
  35.  
  36. # from collections import ChainMap
  37. from wsgiref.simple_server import WSGIServer
  38.  
  39. for cls in WSGIServer.mro():
  40.     if cls == object:
  41.         break
  42.     text = shape_text(cls)
  43.     print("\n".join(grep_by_indent(text, 4)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top