Skip to content

Commit deded8f

Browse files
committed
Squeeze blank lines in RecipeOrganizer.show().
Avoid long sections of blank lines in filtered output. Also simplify the unit test of RecipeOrganizer.show.
1 parent abd8266 commit deded8f

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

diffpy/srfit/fitbase/recipeorganizer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from numpy import inf
2727
from collections import OrderedDict
28-
from itertools import chain, ifilter
28+
from itertools import chain, ifilter, groupby
2929
import re
3030

3131
from diffpy.srfit.fitbase.constraint import Constraint
@@ -970,7 +970,14 @@ def show(self, pattern="", textwidth=78):
970970
tlines = self._formatManaged()
971971
if tlines:
972972
lines.extend(["Parameters", _DASHEDLINE])
973-
lines.extend(filter(pmatch, tlines))
973+
linesok = filter(pmatch, tlines)
974+
lastnotblank = False
975+
# squeeze repeated blank lines
976+
for lastnotblank, g in groupby(linesok, bool):
977+
lines.extend(g if lastnotblank else [""])
978+
# remove trailing blank line
979+
if not lastnotblank:
980+
lines.pop(-1)
974981

975982
# FIXME - parameter names in equations not particularly informative
976983
# Show constraints

diffpy/srfit/tests/testrecipeorganizer.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -509,47 +509,50 @@ def test_releaseOldEquations(self):
509509
def test_show(self):
510510
"""Verify output from the show function.
511511
"""
512-
sys.stdout = cStringIO.StringIO()
513-
self.m.show()
514-
self.assertEqual('', sys.stdout.getvalue())
515-
sys.stdout = cStringIO.StringIO()
512+
def capture_show(*args, **kwargs):
513+
sys.stdout = cStringIO.StringIO()
514+
self.m.show(*args, **kwargs)
515+
rv = sys.stdout.getvalue()
516+
sys.stdout = sys.__stdout__
517+
return rv
518+
self.assertEqual('', capture_show())
516519
self.m._newParameter('x', 1)
517520
self.m._newParameter('y', 2)
518-
self.m.show()
519-
out1 = sys.stdout.getvalue()
521+
out1 = capture_show()
520522
lines1 = out1.strip().split('\n')
521523
self.assertEqual(4, len(lines1))
522524
self.assertTrue('Parameters' in lines1)
523525
self.assertFalse('Constraints' in lines1)
524526
self.assertFalse('Restraints' in lines1)
525527
self.m._newParameter('z', 7)
526528
self.m.constrain('y', '3 * z')
527-
sys.stdout = cStringIO.StringIO()
528-
self.m.show()
529-
out2 = sys.stdout.getvalue()
529+
out2 = capture_show()
530530
lines2 = out2.strip().split('\n')
531531
self.assertEqual(9, len(lines2))
532532
self.assertTrue('Parameters' in lines2)
533533
self.assertTrue('Constraints' in lines2)
534534
self.assertFalse('Restraints' in lines2)
535535
self.m.restrain('z', lb=2, ub=3, sig=0.001)
536-
sys.stdout = cStringIO.StringIO()
537-
self.m.show()
538-
out3 = sys.stdout.getvalue()
536+
out3 = capture_show()
539537
lines3 = out3.strip().split('\n')
540538
self.assertEqual(13, len(lines3))
541539
self.assertTrue('Parameters' in lines3)
542540
self.assertTrue('Constraints' in lines3)
543541
self.assertTrue('Restraints' in lines3)
544-
sys.stdout = cStringIO.StringIO()
545-
self.m.show(pattern='x')
546-
out4 = sys.stdout.getvalue()
542+
out4 = capture_show(pattern='x')
547543
lines4 = out4.strip().split('\n')
548544
self.assertEqual(9, len(lines4))
549-
sys.stdout = cStringIO.StringIO()
550-
self.m.show(pattern='^')
551-
out5 = sys.stdout.getvalue()
545+
out5 = capture_show(pattern='^')
552546
self.assertEqual(out3, out5)
547+
# check output with another level of hierarchy
548+
self.m._addObject(RecipeOrganizer("foo"), self.m._containers)
549+
self.m.foo._newParameter("bar", 13)
550+
out6 = capture_show()
551+
self.assertTrue("foo.bar" in out6)
552+
# filter out foo.bar
553+
out7 = capture_show('^(?!foo).')
554+
self.assertFalse("foo.bar" in out7)
555+
self.assertEqual(out3, out7)
553556
return
554557

555558
# ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)