From 18f9ac7d44bd2b9b2ce80177b4ac071669a00e6e Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Mon, 14 Feb 2022 12:11:01 -0500 Subject: [PATCH] outsource tail-repacking of characters into string (util.tail_repack) --- python/damask/_vtk.py | 7 +------ python/damask/util.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/python/damask/_vtk.py b/python/damask/_vtk.py index 49c7d5d79..16b72e98d 100644 --- a/python/damask/_vtk.py +++ b/python/damask/_vtk.py @@ -408,14 +408,9 @@ class VTK: Comments. """ - if isinstance(comments,list): - i = 0 - while -i < len(comments) and len(comments[i-1]) == 1: i -= 1 # repack any trailing characters - comments = comments[:i] + [''.join(comments[i:])] # that resulted from autosplitting of str to list - s = vtk.vtkStringArray() s.SetName('comments') - for c in [comments] if isinstance(comments,str) else comments: + for c in util.tail_repack(comments,self.comments): s.InsertNextValue(c) self.vtk_data.GetFieldData().AddArray(s) diff --git a/python/damask/util.py b/python/damask/util.py index fc3f8ea8f..96c6f46a6 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -33,7 +33,8 @@ __all__=[ 'extend_docstring', 'extended_docstring', 'Bravais_to_Miller', 'Miller_to_Bravais', 'DREAM3D_base_group', 'DREAM3D_cell_data_group', - 'dict_prune', 'dict_flatten' + 'dict_prune', 'dict_flatten', + 'tail_repack', ] # https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py @@ -722,6 +723,36 @@ def dict_flatten(d: Dict) -> Dict: +def tail_repack(extended: Union[str, Sequence[str]], + existing: List[str] = []) -> List[str]: + """ + Repack tailing characters into single string if all are new. + + Parameters + ---------- + extended : str or list of str + Extended string list with potentially autosplitted tailing string relative to `existing`. + existing : list of str + Base string list. + + Returns + ------- + repacked : list of str + Repacked version of `extended`. + + Examples + -------- + >>> tail_repack(['a','new','e','n','t','r','y'],['a','new']) + ['a','new','entry'] + >>> tail_repack(['a','new','shiny','e','n','t','r','y'],['a','new']) + ['a','new','shiny','e','n','t','r','y'] + + """ + return [extended] if isinstance(extended,str) else existing + \ + ([''.join(extended[len(existing):])] if np.prod([len(i) for i in extended[len(existing):]]) == 1 else + list(extended[len(existing):])) + + #################################################################################################### # Classes ####################################################################################################