From 51e5dda702fda11c157fd53dc8f3a0b8d8bd6927 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 15 Nov 2020 10:53:23 +0100 Subject: [PATCH] documentation + consistent string formatting --- python/damask/_geom.py | 10 +++---- python/damask/_result.py | 4 +-- python/damask/util.py | 55 ++++++++++++++++++++++------------- python/tests/test_Rotation.py | 6 ++-- python/tests/test_util.py | 2 +- 5 files changed, 45 insertions(+), 32 deletions(-) diff --git a/python/damask/_geom.py b/python/damask/_geom.py index 798bad0f8..d364ad6e8 100644 --- a/python/damask/_geom.py +++ b/python/damask/_geom.py @@ -74,23 +74,23 @@ class Geom: """ message = [] if np.any(other.grid != self.grid): - message.append(util.delete(f'grid a b c: {util.srepr(other.grid," x ")}')) + message.append(util.deemph(f'grid a b c: {util.srepr(other.grid," x ")}')) message.append(util.emph( f'grid a b c: {util.srepr( self.grid," x ")}')) if not np.allclose(other.size,self.size): - message.append(util.delete(f'size x y z: {util.srepr(other.size," x ")}')) + message.append(util.deemph(f'size x y z: {util.srepr(other.size," x ")}')) message.append(util.emph( f'size x y z: {util.srepr( self.size," x ")}')) if not np.allclose(other.origin,self.origin): - message.append(util.delete(f'origin x y z: {util.srepr(other.origin," ")}')) + message.append(util.deemph(f'origin x y z: {util.srepr(other.origin," ")}')) message.append(util.emph( f'origin x y z: {util.srepr( self.origin," ")}')) if other.N_materials != self.N_materials: - message.append(util.delete(f'# materials: {other.N_materials}')) + message.append(util.deemph(f'# materials: {other.N_materials}')) message.append(util.emph( f'# materials: { self.N_materials}')) if np.nanmax(other.material) != np.nanmax(self.material): - message.append(util.delete(f'max material: {np.nanmax(other.material)}')) + message.append(util.deemph(f'max material: {np.nanmax(other.material)}')) message.append(util.emph( f'max material: {np.nanmax( self.material)}')) return util.return_message(message) diff --git a/python/damask/_result.py b/python/damask/_result.py index 5bed4347f..45a7fafc6 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -167,9 +167,7 @@ class Result: def allow_modification(self): - print(util.bcolors().WARNING+util.bcolors().BOLD+ - 'Warning: Modification of existing datasets allowed!'+ - util.bcolors().ENDC) + print(util.warn('Warning: Modification of existing datasets allowed!')) self._allow_modification = True def disallow_modification(self): diff --git a/python/damask/util.py b/python/damask/util.py index d1b3ed842..49e85c9f5 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -17,7 +17,7 @@ __all__=[ 'srepr', 'croak', 'report', - 'emph','deemph','delete','strikeout', + 'emph','deemph','warn','strikeout', 'execute', 'show_progress', 'scale_to_coprime', @@ -74,7 +74,7 @@ def croak(what, newline = True): def report(who = None, what = None): """ - Reports script and file name. + Report script and file name. DEPRECATED @@ -86,16 +86,13 @@ def emph(what): """Formats string with emphasis.""" return bcolors.BOLD+srepr(what)+bcolors.ENDC - def deemph(what): """Formats string with deemphasis.""" return bcolors.DIM+srepr(what)+bcolors.ENDC - -def delete(what): - """Formats string as deleted.""" - return bcolors.DIM+srepr(what)+bcolors.ENDC - +def warn(what): + """Formats string for warning.""" + return bcolors.WARNING+emph(what)+bcolors.ENDC def strikeout(what): """Formats string as strikeout.""" @@ -302,20 +299,38 @@ def shapeblender(a,b): return a + b[i:] -def extend_docstring(general): - """Decorator: Append Orientation parameter documentation to function's docstring.""" - def _decorator(func): - func.__doc__ += general - return func - return _decorator +def extend_docstring(extra_docstring): + """ + Decorator: Append to function's docstring. + + Parameters + ---------- + extra_docstring : str + Docstring to append. + + """ + def _decorator(func): + func.__doc__ += extra_docstring + return func + return _decorator -def extended_docstring(f,general): - """Decorator: Combine Orientation parameter documentation with another function's docstring.""" - def _decorator(func): - func.__doc__ = f.__doc__ + general - return func - return _decorator +def extended_docstring(f,extra_docstring): + """ + Decorator: Combine another function's docstring with a given docstring. + + Parameters + ---------- + f : function + Function of which the docstring is taken. + extra_docstring : str + Docstring to append. + + """ + def _decorator(func): + func.__doc__ = f.__doc__ + extra_docstring + return func + return _decorator #################################################################################################### diff --git a/python/tests/test_Rotation.py b/python/tests/test_Rotation.py index 9361008e5..20eb49b7e 100644 --- a/python/tests/test_Rotation.py +++ b/python/tests/test_Rotation.py @@ -772,15 +772,15 @@ class TestRotation: Rotation.from_random(shape) def test_equal(self): - r = Rotation.from_random(seed=0) + r = Rotation.from_random() assert r == r def test_unequal(self): - r = Rotation.from_random(seed=0) + r = Rotation.from_random() assert not (r != r) def test_inversion(self): - r = Rotation.from_random(seed=0) + r = Rotation.from_random() assert r == ~~r @pytest.mark.parametrize('shape',[None,1,(1,),(4,2),(1,1,1)]) diff --git a/python/tests/test_util.py b/python/tests/test_util.py index 9906689e9..eb1084b09 100644 --- a/python/tests/test_util.py +++ b/python/tests/test_util.py @@ -98,6 +98,6 @@ class TestUtil: def test_shapeblender(self,a,b,answer): assert util.shapeblender(a,b) == answer - @pytest.mark.parametrize('style',[util.emph,util.deemph,util.delete,util.strikeout]) + @pytest.mark.parametrize('style',[util.emph,util.deemph,util.warn,util.strikeout]) def test_decorate(self,style): assert 'DAMASK' in style('DAMASK')