Skip to content

Commit

Permalink
feat(misc): add function make list int (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankrb authored and JesperDramsch committed Dec 17, 2024
1 parent 1f1404a commit 30570e8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/anemoi/utils/humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,36 @@ def print_dates(dates) -> None:
A list of dates, as datetime objects or strings.
"""
print(compress_dates(dates))


def make_list_int(value) -> list:
"""Convert a string like "1/2/3" or "1/to/3" or "1/to/10/by/2" to a list of integers.
Parameters
----------
value : str, list, tuple, int
The value to convert to a list of integers.
Returns
-------
list
A list of integers.
"""
if isinstance(value, str):
if "/" not in value:
return [int(value)]
bits = value.split("/")
if len(bits) == 3 and bits[1].lower() == "to":
value = list(range(int(bits[0]), int(bits[2]) + 1, 1))

elif len(bits) == 5 and bits[1].lower() == "to" and bits[3].lower() == "by":
value = list(range(int(bits[0]), int(bits[2]) + int(bits[4]), int(bits[4])))

if isinstance(value, list):
return value
if isinstance(value, tuple):
return value
if isinstance(value, int):
return [value]

raise ValueError(f"Cannot make list from {value}")

0 comments on commit 30570e8

Please sign in to comment.