diff --git a/baseclasses/__init__.py b/baseclasses/__init__.py index 6f36645..c6cd247 100644 --- a/baseclasses/__init__.py +++ b/baseclasses/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.5.3" +__version__ = "1.5.4" from .problems import ( AeroProblem, diff --git a/baseclasses/solvers/BaseSolver.py b/baseclasses/solvers/BaseSolver.py index 3f410c2..a266d54 100644 --- a/baseclasses/solvers/BaseSolver.py +++ b/baseclasses/solvers/BaseSolver.py @@ -4,10 +4,9 @@ Holds a basic Python Analysis Classes (base and inherited). """ from difflib import get_close_matches -from pprint import pprint import copy import warnings -from ..utils import CaseInsensitiveDict, CaseInsensitiveSet, Error +from ..utils import CaseInsensitiveDict, CaseInsensitiveSet, Error, pp # ============================================================================= # BaseSolver Class @@ -224,7 +223,7 @@ def printModifiedOptions(self): modifiedOptions = self.getModifiedOptions() self.pp(modifiedOptions) - def pp(self, obj, flush=False): + def pp(self, obj, flush=True): """ This method prints ``obj`` (via pprint) on the root proc of ``self.comm`` if it exists. Otherwise it will just print ``obj``. @@ -232,10 +231,10 @@ def pp(self, obj, flush=False): Parameters ---------- obj : object - any Python object to be printed + Any Python object to be printed + flush : bool + If True, the stream will be flushed. """ - if (self.comm is not None and self.comm.rank == 0) or self.comm is None: - if isinstance(obj, str): - print(obj, flush=flush) - else: - pprint(obj) + + # Call the parallel safe pp routine defined in utils + pp(obj, self.comm, flush=flush) diff --git a/baseclasses/utils/utils.py b/baseclasses/utils/utils.py index 9be4d55..2506521 100644 --- a/baseclasses/utils/utils.py +++ b/baseclasses/utils/utils.py @@ -2,8 +2,7 @@ Helper methods for supporting python3 and python2 at the same time """ import sys -from pprint import pprint -from .containers import CaseInsensitiveDict, CaseInsensitiveSet +from pprint import pformat def getPy3SafeString(string): @@ -20,14 +19,25 @@ def getPy3SafeString(string): return string -def pp(obj, comm=None): +def pp(obj, comm=None, flush=True): + """ + Parallel safe printing routine. This method prints ``obj`` (via pprint) on the root proc of ``self.comm`` if it exists. Otherwise it will just print ``obj``. + + Parameters + ---------- + obj : object + Any Python object to be printed + comm : MPI comm + The MPI comm object on this processor + flush : bool + If True, the stream will be flushed. + """ if (comm is None) or (comm is not None and comm.rank == 0): # use normal print for string so there's no quotes if isinstance(obj, str): - print(obj) - # use pprint for other built-in types (other than string) - elif obj.__class__.__module__ == "__builtin__" or isinstance(obj, (CaseInsensitiveDict, CaseInsensitiveSet)): - pprint(obj) - # use print for everything else + print(obj, flush=flush) + # use pprint for everything else else: - print(obj) + # we use pformat to get the string and then call print manually, that way we can flush if we need to + pprint_str = pformat(obj) + print(pprint_str, flush=flush)