Skip to content

Commit

Permalink
Minor update to misc_utils.right_trim
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Nov 30, 2023
1 parent b27428f commit c0ba3d4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from collections import defaultdict
from datetime import datetime as datetime_type
from dateutil.parser import parse as dateutil_parse
from typing import Any, List, Optional, Tuple, Union
from typing import Any, Callable, List, Optional, Tuple, Union


# Is this the right place for this? I feel like this should be done in an application, not a library.
Expand Down Expand Up @@ -1489,13 +1489,14 @@ def split_string(value: str, delimiter: str, escape: Optional[str] = None) -> Li
return [item for item in result if item]


def right_trim(list_or_tuple: Union[List[Any], Tuple[Any]]) -> Union[List[Any], Tuple[Any]]:
def right_trim(list_or_tuple: Union[List[Any], Tuple[Any]],
remove: Optional[Callable] = None) -> Union[List[Any], Tuple[Any]]:
"""
Removes training None (or emptry string) values from the give tuple or list arnd returns;
does NOT change the given value.
"""
i = len(list_or_tuple) - 1
while i >= 0 and list_or_tuple[i] in (None, ""):
while i >= 0 and ((remove and remove(list_or_tuple[i])) or (not remove and list_or_tuple[i] in (None, ""))):
i -= 1
return list_or_tuple[:i + 1]

Expand Down

0 comments on commit c0ba3d4

Please sign in to comment.