[Python-checkins] CVS: python/dist/src/Lib pydoc.py,1.45,1.46

Tim Peters tim_one@users.sourceforge.net
Mon, 24 Sep 2001 01:05:13 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv2025/python/Lib

Modified Files:
	pydoc.py 
Log Message:
More work on class display:

+ Minor code cleanup, generalization and simplification.

+ "Do something" to make the attribute aggregation more apparent:
    - In text mode, stick a "* " at the front of subgroup header lines.
    - In GUI mode, display a horizontal rule between subgroups.
   For GUI mode, this is a huge improvement, at least under IE.


Index: pydoc.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pydoc.py,v
retrieving revision 1.45
retrieving revision 1.46
diff -C2 -d -r1.45 -r1.46
*** pydoc.py	2001/09/24 04:47:19	1.45
--- pydoc.py	2001/09/24 08:05:11	1.46
***************
*** 129,140 ****
      return methods
  
! def _split_class_attrs(attrs, predicate):
      yes = []
      no = []
!     for tuple in attrs:
!         if predicate(tuple):
!             yes.append(tuple)
          else:
!             no.append(tuple)
      return yes, no
  
--- 129,147 ----
      return methods
  
! def _split_list(s, predicate):
!     """Split sequence s via predicate, and return pair ([true], [false]).
! 
!     The return value is a 2-tuple of lists,
!         ([x for x in s if predicate(x)],
!          [x for x in s if not predicate(x)])
!     """
! 
      yes = []
      no = []
!     for x in s:
!         if predicate(x):
!             yes.append(x)
          else:
!             no.append(x)
      return yes, no
  
***************
*** 609,615 ****
          push = contents.append
  
          def spill(msg, attrs, predicate):
!             ok, attrs = _split_class_attrs(attrs, predicate)
              if ok:
                  push(msg)
                  for name, kind, homecls, value in ok:
--- 616,633 ----
          push = contents.append
  
+         # Cute little class to pump out a horizontal rule between sections.
+         class HorizontalRule:
+             def __init__(self):
+                 self.needone = 0
+             def maybe(self):
+                 if self.needone:
+                     push('<hr>\n')
+                 self.needone = 1
+         hr = HorizontalRule()
+ 
          def spill(msg, attrs, predicate):
!             ok, attrs = _split_list(attrs, predicate)
              if ok:
+                 hr.maybe()
                  push(msg)
                  for name, kind, homecls, value in ok:
***************
*** 622,628 ****
          # and it doesn't appear that the getter, setter and del'er methods
          # are discoverable.  For now, just pump out their names.
!         def spillproperties(msg, attrs):
!             ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'property')
              if ok:
                  push(msg)
                  for name, kind, homecls, value in ok:
--- 640,647 ----
          # and it doesn't appear that the getter, setter and del'er methods
          # are discoverable.  For now, just pump out their names.
!         def spillproperties(msg, attrs, predicate):
!             ok, attrs = _split_list(attrs, predicate)
              if ok:
+                 hr.maybe()
                  push(msg)
                  for name, kind, homecls, value in ok:
***************
*** 630,636 ****
              return attrs
  
!         def spilldata(msg, attrs):
!             ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'data')
              if ok:
                  push(msg)
                  for name, kind, homecls, value in ok:
--- 649,656 ----
              return attrs
  
!         def spilldata(msg, attrs, predicate):
!             ok, attrs = _split_list(attrs, predicate)
              if ok:
+                 hr.maybe()
                  push(msg)
                  for name, kind, homecls, value in ok:
***************
*** 659,674 ****
                  pass
  
!         # All attrs defined in this class come first.
!         attrs, inherited = _split_class_attrs(attrs,
!                                               lambda t: t[2] is object)
!         # Sort inherited attrs by name of defining class.
!         inherited.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
  
!         thisclass = object
!         while attrs or inherited:
              if thisclass is object:
                  tag = "defined here"
              else:
!                 tag = "inherited from class %s" % self.classlink(thisclass,
                                                            object.__module__)
              tag += ':<br>\n'
--- 679,693 ----
                  pass
  
!         # Sort attrs by name of defining class.
!         attrs.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
  
!         thisclass = object  # list attrs defined here first
!         while attrs:
!             attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
! 
              if thisclass is object:
                  tag = "defined here"
              else:
!                 tag = "inherited from %s" % self.classlink(thisclass,
                                                            object.__module__)
              tag += ':<br>\n'
***************
*** 684,689 ****
              attrs = spill("Static methods %s" % tag, attrs,
                            lambda t: t[1] == 'static method')
!             attrs = spillproperties("Properties %s" % tag, attrs)
!             attrs = spilldata("Data %s" % tag, attrs)
              assert attrs == []
  
--- 703,710 ----
              attrs = spill("Static methods %s" % tag, attrs,
                            lambda t: t[1] == 'static method')
!             attrs = spillproperties("Properties %s" % tag, attrs,
!                                     lambda t: t[1] == 'property')
!             attrs = spilldata("Data %s" % tag, attrs,
!                               lambda t: t[1] == 'data')
              assert attrs == []
  
***************
*** 693,698 ****
                  attrs = inherited
                  thisclass = attrs[0][2]
-                 attrs, inherited = _split_class_attrs(attrs,
-                                                 lambda t: t[2] is thisclass)
  
          contents = ''.join(contents)
--- 714,717 ----
***************
*** 974,978 ****
  
          def spill(msg, attrs, predicate):
!             ok, attrs = _split_class_attrs(attrs, predicate)
              if ok:
                  push(msg)
--- 993,997 ----
  
          def spill(msg, attrs, predicate):
!             ok, attrs = _split_list(attrs, predicate)
              if ok:
                  push(msg)
***************
*** 985,990 ****
          # and it doesn't appear that the getter, setter and del'er methods
          # are discoverable.  For now, just pump out their names.
!         def spillproperties(msg, attrs):
!             ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'property')
              if ok:
                  push(msg)
--- 1004,1009 ----
          # and it doesn't appear that the getter, setter and del'er methods
          # are discoverable.  For now, just pump out their names.
!         def spillproperties(msg, attrs, predicate):
!             ok, attrs = _split_list(attrs, predicate)
              if ok:
                  push(msg)
***************
*** 993,998 ****
              return attrs
  
!         def spilldata(msg, attrs):
!             ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'data')
              if ok:
                  push(msg)
--- 1012,1017 ----
              return attrs
  
!         def spilldata(msg, attrs, predicate):
!             ok, attrs = _split_list(attrs, predicate)
              if ok:
                  push(msg)
***************
*** 1005,1021 ****
          attrs = inspect.classify_class_attrs(object)
  
!         # All attrs defined in this class come first.
!         attrs, inherited = _split_class_attrs(attrs,
!                                               lambda t: t[2] is object)
!         # Sort inherited attrs by name of defining class.
!         inherited.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
  
!         thisclass = object
!         while attrs or inherited:
              if thisclass is object:
                  tag = "defined here"
              else:
!                 tag = "inherited from class %s" % classname(thisclass,
!                                                             object.__module__)
  
              # Sort attrs by name.
--- 1024,1039 ----
          attrs = inspect.classify_class_attrs(object)
  
!         # Sort attrs by name of defining class.
!         attrs.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
  
!         thisclass = object  # list attrs defined here first
!         while attrs:
!             attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
! 
              if thisclass is object:
                  tag = "defined here"
              else:
!                 tag = "inherited from %s" % classname(thisclass,
!                                                       object.__module__)
  
              # Sort attrs by name.
***************
*** 1023,1034 ****
  
              # Pump out the attrs, segregated by kind.
!             attrs = spill("Methods %s:\n" % tag, attrs,
                            lambda t: t[1] == 'method')
!             attrs = spill("Class methods %s:\n" % tag, attrs,
                            lambda t: t[1] == 'class method')
!             attrs = spill("Static methods %s:\n" % tag, attrs,
                            lambda t: t[1] == 'static method')
!             attrs = spillproperties("Properties %s:\n" % tag, attrs)
!             attrs = spilldata("Data %s:\n" % tag, attrs)
              assert attrs == []
  
--- 1041,1054 ----
  
              # Pump out the attrs, segregated by kind.
!             attrs = spill("* Methods %s:\n" % tag, attrs,
                            lambda t: t[1] == 'method')
!             attrs = spill("* Class methods %s:\n" % tag, attrs,
                            lambda t: t[1] == 'class method')
!             attrs = spill("* Static methods %s:\n" % tag, attrs,
                            lambda t: t[1] == 'static method')
!             attrs = spillproperties("* Properties %s:\n" % tag, attrs,
!                                     lambda t: t[1] == 'property')
!             attrs = spilldata("* Data %s:\n" % tag, attrs,
!                               lambda t: t[1] == 'data')
              assert attrs == []
  
***************
*** 1038,1043 ****
                  attrs = inherited
                  thisclass = attrs[0][2]
-                 attrs, inherited = _split_class_attrs(attrs,
-                                                 lambda t: t[2] is thisclass)
  
          contents = '\n'.join(contents)
--- 1058,1061 ----