outsource tail-repacking of characters into string (util.tail_repack)

This commit is contained in:
Philip Eisenlohr 2022-02-14 12:11:01 -05:00
parent 2ce464c48e
commit 18f9ac7d44
2 changed files with 33 additions and 7 deletions

View File

@ -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)

View File

@ -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
####################################################################################################