Skip to content

Commit

Permalink
Refactoring pp in BaseSolver to use an updating pp in utils. (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Neil Wu <[email protected]>
  • Loading branch information
eirikurj and ewu63 committed Feb 15, 2022
1 parent e761f10 commit d2f5c5b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion baseclasses/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.5.3"
__version__ = "1.5.4"

from .problems import (
AeroProblem,
Expand Down
17 changes: 8 additions & 9 deletions baseclasses/solvers/BaseSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -224,18 +223,18 @@ 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``.
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)
28 changes: 19 additions & 9 deletions baseclasses/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit d2f5c5b

Please sign in to comment.