2424
2525import re
2626from collections import OrderedDict
27+ from functools import partial
2728from itertools import chain , groupby
2829
2930import six
@@ -741,8 +742,7 @@ def clearConstraints(self, recurse=False):
741742 self .unconstrain (* self ._constraints )
742743
743744 if recurse :
744- _constraint_clearer = lambda m : hasattr (m , "clearConstraints" )
745- for m in filter (f , self ._iterManaged ()):
745+ for m in filter (_has_clear_constraints , self ._iterManaged ()):
746746 m .clearConstraints (recurse )
747747 return
748748
@@ -822,19 +822,16 @@ def clearRestraints(self, recurse=False):
822822 found there as well.
823823 """
824824 self .unrestrain (* self ._restraints )
825-
826825 if recurse :
827- f = lambda m : hasattr (m , "clearRestraints" )
828- for m in filter (f , self ._iterManaged ()):
829- m .clearRestraints (recurse )
826+ for msg in filter (_has_clear_restraints (), self ._iterManaged ()):
827+ msg .clearRestraints (recurse )
830828 return
831829
832830 def _getConstraints (self , recurse = True ):
833831 """Get the constrained Parameters for this and managed sub-objects."""
834832 constraints = {}
835833 if recurse :
836- f = lambda m : hasattr (m , "_getConstraints" )
837- for m in filter (f , self ._iterManaged ()):
834+ for m in filter (_has_get_constraints (), self ._iterManaged ()):
838835 constraints .update (m ._getConstraints (recurse ))
839836
840837 constraints .update (self ._constraints )
@@ -848,8 +845,7 @@ def _getRestraints(self, recurse=True):
848845 """
849846 restraints = set (self ._restraints )
850847 if recurse :
851- f = lambda m : hasattr (m , "_getRestraints" )
852- for m in filter (f , self ._iterManaged ()):
848+ for m in filter (_has_get_restraints (), self ._iterManaged ()):
853849 restraints .update (m ._getRestraints (recurse ))
854850
855851 return restraints
@@ -968,15 +964,13 @@ def show(self, pattern="", textwidth=78):
968964 the screen width. Do not trim when negative or 0.
969965 """
970966 regexp = re .compile (pattern )
971- pmatch = lambda s : (
972- len (s .split (None , 1 )) < 2 or regexp .search (s .split (None , 1 )[0 ])
973- )
967+ _pmatch_with_re = partial (_pmatch , regexp = regexp )
974968 # Show sub objects and their parameters
975969 lines = []
976970 tlines = self ._formatManaged ()
977971 if tlines :
978972 lines .extend (["Parameters" , _DASHEDLINE ])
979- linesok = filter (pmatch , tlines )
973+ linesok = filter (_pmatch_with_re , tlines )
980974 lastnotblank = False
981975 # squeeze repeated blank lines
982976 for lastnotblank , g in groupby (linesok , bool ):
@@ -1002,7 +996,7 @@ def show(self, pattern="", textwidth=78):
1002996 if lines :
1003997 lines .append ("" )
1004998 lines .extend (["Restraints" , _DASHEDLINE ])
1005- lines .extend (filter (pmatch , tlines ))
999+ lines .extend (filter (_pmatch_with_re , tlines ))
10061000
10071001 # Determine effective text width tw.
10081002 tw = textwidth if (textwidth is not None and textwidth > 0 ) else None
@@ -1059,5 +1053,22 @@ def equationFromString(
10591053 return eq
10601054
10611055
1062- def _constraint_clearer (msg ):
1056+ def _has_clear_constraints (msg ):
10631057 return hasattr (msg , "clearConstraints" )
1058+
1059+
1060+ def _has_clear_restraints (msg ):
1061+ return hasattr (msg , "clearRestraints" )
1062+
1063+
1064+ def _has_get_restraints (msg ):
1065+ return hasattr (msg , "_getRestraints" )
1066+
1067+
1068+ def _has_get_constraints (msg ):
1069+ return hasattr (msg , "_getConstraints" )
1070+
1071+
1072+ def _pmatch (inp_str , regexp ):
1073+ parts = inp_str .split (None , 1 )
1074+ return len (parts ) < 2 or regexp .search (parts [0 ])
0 commit comments