Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa26f7c

Browse files
committed
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.
1 parent b47879b commit fa26f7c

1 file changed

Lines changed: 61 additions & 43 deletions

File tree

Lib/pydoc.py

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,21 @@ def allmethods(cl):
128128
methods[key] = getattr(cl, key)
129129
return methods
130130

131-
def _split_class_attrs(attrs, predicate):
131+
def _split_list(s, predicate):
132+
"""Split sequence s via predicate, and return pair ([true], [false]).
133+
134+
The return value is a 2-tuple of lists,
135+
([x for x in s if predicate(x)],
136+
[x for x in s if not predicate(x)])
137+
"""
138+
132139
yes = []
133140
no = []
134-
for tuple in attrs:
135-
if predicate(tuple):
136-
yes.append(tuple)
141+
for x in s:
142+
if predicate(x):
143+
yes.append(x)
137144
else:
138-
no.append(tuple)
145+
no.append(x)
139146
return yes, no
140147

141148
# ----------------------------------------------------- module manipulation
@@ -608,9 +615,20 @@ def docclass(self, object, name=None, mod=None, funcs={}, classes={}):
608615
contents = []
609616
push = contents.append
610617

618+
# Cute little class to pump out a horizontal rule between sections.
619+
class HorizontalRule:
620+
def __init__(self):
621+
self.needone = 0
622+
def maybe(self):
623+
if self.needone:
624+
push('<hr>\n')
625+
self.needone = 1
626+
hr = HorizontalRule()
627+
611628
def spill(msg, attrs, predicate):
612-
ok, attrs = _split_class_attrs(attrs, predicate)
629+
ok, attrs = _split_list(attrs, predicate)
613630
if ok:
631+
hr.maybe()
614632
push(msg)
615633
for name, kind, homecls, value in ok:
616634
push(self.document(getattr(object, name), name, mod,
@@ -621,17 +639,19 @@ def spill(msg, attrs, predicate):
621639
# pydoc can't make any reasonable sense of properties on its own,
622640
# and it doesn't appear that the getter, setter and del'er methods
623641
# are discoverable. For now, just pump out their names.
624-
def spillproperties(msg, attrs):
625-
ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'property')
642+
def spillproperties(msg, attrs, predicate):
643+
ok, attrs = _split_list(attrs, predicate)
626644
if ok:
645+
hr.maybe()
627646
push(msg)
628647
for name, kind, homecls, value in ok:
629648
push('<dl><dt><strong>%s</strong></dl>\n' % name)
630649
return attrs
631650

632-
def spilldata(msg, attrs):
633-
ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'data')
651+
def spilldata(msg, attrs, predicate):
652+
ok, attrs = _split_list(attrs, predicate)
634653
if ok:
654+
hr.maybe()
635655
push(msg)
636656
for name, kind, homecls, value in ok:
637657
base = self.docother(getattr(object, name), name, mod)
@@ -658,18 +678,17 @@ def spilldata(msg, attrs):
658678
except TypeError:
659679
pass
660680

661-
# All attrs defined in this class come first.
662-
attrs, inherited = _split_class_attrs(attrs,
663-
lambda t: t[2] is object)
664-
# Sort inherited attrs by name of defining class.
665-
inherited.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
681+
# Sort attrs by name of defining class.
682+
attrs.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
683+
684+
thisclass = object # list attrs defined here first
685+
while attrs:
686+
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
666687

667-
thisclass = object
668-
while attrs or inherited:
669688
if thisclass is object:
670689
tag = "defined here"
671690
else:
672-
tag = "inherited from class %s" % self.classlink(thisclass,
691+
tag = "inherited from %s" % self.classlink(thisclass,
673692
object.__module__)
674693
tag += ':<br>\n'
675694

@@ -683,17 +702,17 @@ def spilldata(msg, attrs):
683702
lambda t: t[1] == 'class method')
684703
attrs = spill("Static methods %s" % tag, attrs,
685704
lambda t: t[1] == 'static method')
686-
attrs = spillproperties("Properties %s" % tag, attrs)
687-
attrs = spilldata("Data %s" % tag, attrs)
705+
attrs = spillproperties("Properties %s" % tag, attrs,
706+
lambda t: t[1] == 'property')
707+
attrs = spilldata("Data %s" % tag, attrs,
708+
lambda t: t[1] == 'data')
688709
assert attrs == []
689710

690711
# Split off the attributes inherited from the next class (note
691712
# that inherited remains sorted by class name).
692713
if inherited:
693714
attrs = inherited
694715
thisclass = attrs[0][2]
695-
attrs, inherited = _split_class_attrs(attrs,
696-
lambda t: t[2] is thisclass)
697716

698717
contents = ''.join(contents)
699718

@@ -973,7 +992,7 @@ def makename(c, m=object.__module__): return classname(c, m)
973992
push = contents.append
974993

975994
def spill(msg, attrs, predicate):
976-
ok, attrs = _split_class_attrs(attrs, predicate)
995+
ok, attrs = _split_list(attrs, predicate)
977996
if ok:
978997
push(msg)
979998
for name, kind, homecls, value in ok:
@@ -984,16 +1003,16 @@ def spill(msg, attrs, predicate):
9841003
# pydoc can't make any reasonable sense of properties on its own,
9851004
# and it doesn't appear that the getter, setter and del'er methods
9861005
# are discoverable. For now, just pump out their names.
987-
def spillproperties(msg, attrs):
988-
ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'property')
1006+
def spillproperties(msg, attrs, predicate):
1007+
ok, attrs = _split_list(attrs, predicate)
9891008
if ok:
9901009
push(msg)
9911010
for name, kind, homecls, value in ok:
9921011
push(name + '\n')
9931012
return attrs
9941013

995-
def spilldata(msg, attrs):
996-
ok, attrs = _split_class_attrs(attrs, lambda t: t[1] == 'data')
1014+
def spilldata(msg, attrs, predicate):
1015+
ok, attrs = _split_list(attrs, predicate)
9971016
if ok:
9981017
push(msg)
9991018
for name, kind, homecls, value in ok:
@@ -1004,41 +1023,40 @@ def spilldata(msg, attrs):
10041023

10051024
attrs = inspect.classify_class_attrs(object)
10061025

1007-
# All attrs defined in this class come first.
1008-
attrs, inherited = _split_class_attrs(attrs,
1009-
lambda t: t[2] is object)
1010-
# Sort inherited attrs by name of defining class.
1011-
inherited.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
1026+
# Sort attrs by name of defining class.
1027+
attrs.sort(lambda t1, t2: cmp(t1[2].__name__, t2[2].__name__))
1028+
1029+
thisclass = object # list attrs defined here first
1030+
while attrs:
1031+
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
10121032

1013-
thisclass = object
1014-
while attrs or inherited:
10151033
if thisclass is object:
10161034
tag = "defined here"
10171035
else:
1018-
tag = "inherited from class %s" % classname(thisclass,
1019-
object.__module__)
1036+
tag = "inherited from %s" % classname(thisclass,
1037+
object.__module__)
10201038

10211039
# Sort attrs by name.
10221040
attrs.sort(lambda t1, t2: cmp(t1[0], t2[0]))
10231041

10241042
# Pump out the attrs, segregated by kind.
1025-
attrs = spill("Methods %s:\n" % tag, attrs,
1043+
attrs = spill("* Methods %s:\n" % tag, attrs,
10261044
lambda t: t[1] == 'method')
1027-
attrs = spill("Class methods %s:\n" % tag, attrs,
1045+
attrs = spill("* Class methods %s:\n" % tag, attrs,
10281046
lambda t: t[1] == 'class method')
1029-
attrs = spill("Static methods %s:\n" % tag, attrs,
1047+
attrs = spill("* Static methods %s:\n" % tag, attrs,
10301048
lambda t: t[1] == 'static method')
1031-
attrs = spillproperties("Properties %s:\n" % tag, attrs)
1032-
attrs = spilldata("Data %s:\n" % tag, attrs)
1049+
attrs = spillproperties("* Properties %s:\n" % tag, attrs,
1050+
lambda t: t[1] == 'property')
1051+
attrs = spilldata("* Data %s:\n" % tag, attrs,
1052+
lambda t: t[1] == 'data')
10331053
assert attrs == []
10341054

10351055
# Split off the attributes inherited from the next class (note
10361056
# that inherited remains sorted by class name).
10371057
if inherited:
10381058
attrs = inherited
10391059
thisclass = attrs[0][2]
1040-
attrs, inherited = _split_class_attrs(attrs,
1041-
lambda t: t[2] is thisclass)
10421060

10431061
contents = '\n'.join(contents)
10441062
if not contents:

0 commit comments

Comments
 (0)