From ffecc62155353011d22dde446a78888c983f41d7 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 30 Sep 2024 21:29:13 +0200 Subject: [PATCH] Core: allow random range with negative numbers --- Options.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Options.py b/Options.py index aa6f175fa5..e61190bc90 100644 --- a/Options.py +++ b/Options.py @@ -705,10 +705,26 @@ class Range(NumericOption): f"random-range-high--, or random-range--.") @classmethod - def custom_range(cls, text) -> Range: - textsplit = text.split("-") + def custom_range(cls, text: str) -> Range: + numeric_text: str = text[len("random-range-"):] + if numeric_text.startswith(("low", "middle", "high")): + numeric_text = numeric_text.split("-", 1)[1] + textsplit = numeric_text.split("-") + if len(textsplit) > 2: # looks like there may be minus signs, which will now be empty string from the split + new_textsplit: typing.List[str] = [] + next_negative: bool = False + for element in textsplit: + if not element: # empty string -> next element gets a minus sign in front + next_negative = True + elif next_negative: + new_textsplit.append("-"+element) + next_negative = False + else: + new_textsplit.append(element) + textsplit = new_textsplit + del next_negative, new_textsplit try: - random_range = [int(textsplit[len(textsplit) - 2]), int(textsplit[len(textsplit) - 1])] + random_range = [int(textsplit[0]), int(textsplit[1])] except ValueError: raise ValueError(f"Invalid random range {text} for option {cls.__name__}") random_range.sort()