[FEATURE] %range function (#1405)

Add `%range` function, equivalent to Python's `range`.
This commit is contained in:
Jesse Bannon 2025-12-31 09:06:08 -08:00 committed by GitHub
parent 869fc0b836
commit c46de048ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 0 deletions

View file

@ -521,6 +521,13 @@ pow
:description:
``**`` operator. Returns the exponential of the base and exponent value.
range
~~~~~
:spec: ``range(end: Integer, start: Optional[Integer], step: Optional[Integer]) -> Array``
:description:
Returns the desired range of Integers in the form of an Array.
sub
~~~
:spec: ``sub(values: Numeric, ...) -> Numeric``

View file

@ -1,5 +1,7 @@
import math
from typing import Optional
from ytdl_sub.script.types.array import Array
from ytdl_sub.script.types.resolvable import AnyArgument
from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import Integer
@ -96,3 +98,18 @@ class NumericFunctions:
Returns min of all values.
"""
return _to_numeric(min(val.value for val in values))
@staticmethod
def range(
end: Integer, start: Optional[Integer] = None, step: Optional[Integer] = None
) -> Array:
"""
:description:
Returns the desired range of Integers in the form of an Array.
"""
if start is None:
start = Integer(0)
if step is None:
step = Integer(1)
return Array(value=[Integer(idx) for idx in range(start.value, end.value, step.value)])

View file

@ -76,3 +76,15 @@ class TestNumericFunctions:
def test_pow(self, values: str, expected_output: float):
output = single_variable_output(f"{{ %pow({values}) }}")
assert output == expected_output
@pytest.mark.parametrize(
"values, expected_output",
[
("5", [0, 1, 2, 3, 4]),
("5, 1", [1, 2, 3, 4]),
("5, 1, 2", [1, 3]),
],
)
def test_range(self, values: str, expected_output: float):
output = single_variable_output(f"{{ %range({values}) }}")
assert output == expected_output