Skip to content

util

Classes:

  • ExprVars

    A helper class for generating variable names used in RPN expressions.

ExprVars

ExprVars(
    start: SupportsIndex,
    stop: SupportsIndex,
    step: SupportsIndex | None = 1,
    /,
    *,
    expr_src: bool = False,
)
ExprVars(
    start_stop: SupportsIndex | ExprVars | HoldsVideoFormat | VideoFormatLike,
    stop: SupportsIndex | None = None,
    step: SupportsIndex | None = None,
    /,
    *,
    expr_src: bool = False,
)

Bases: Sequence[str], Iterator[str]

A helper class for generating variable names used in RPN expressions.

Initialize an ExprVars instance.

Parameters:

  • start_stop

    (SupportsIndex | ExprVars | HoldsVideoFormat | VideoFormatLike) –

    A start index or an object from which to infer the number of variables (e.g., video format).

  • stop

    (SupportsIndex | None, default: None ) –

    Stop index (exclusive). Required only if start_stop is a numeric start value.

  • step

    (SupportsIndex | None, default: None ) –

    Step size for iteration. Default to 1.

  • expr_src

    (bool, default: False ) –

    Whether to use srcX naming or use alphabetic variables.

Raises:

  • CustomIndexError

    If start is negative or stop is not greater than start.

  • CustomTypeError

    If invalid types are provided.

Methods:

  • cycle

    An infinite generator of variable names, looping through EXPR_VARS then continuing with srcX style.

  • get_var

    Get a variable name for a specific index.

Attributes:

  • curr (int) –

    Current index in iteration.

  • expr_src (bool) –

    If True, variables are named as src0, src1, etc. Otherwise, "x", "y", "z", "a" and so on.

  • start (int) –

    Starting index for variable generation (inclusive).

  • step (int) –

    Step size for iteration.

  • stop (int) –

    Ending index for variable generation (exclusive).

Source code in vsexprtools/util.py
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def __init__(
    self,
    start_stop: SupportsIndex | ExprVars | HoldsVideoFormat | VideoFormatLike,
    stop: SupportsIndex | None = None,
    step: SupportsIndex | None = None,
    /,
    *,
    expr_src: bool = False,
) -> None:
    """
    Initialize an ExprVars instance.

    Args:
        start_stop: A start index or an object from which to infer the number of variables (e.g., video format).
        stop: Stop index (exclusive). Required only if `start_stop` is a numeric start value.
        step: Step size for iteration. Default to 1.
        expr_src: Whether to use `srcX` naming or use alphabetic variables.

    Raises:
        CustomIndexError: If `start` is negative or `stop` is not greater than `start`.
        CustomTypeError: If invalid types are provided.
    """
    if isinstance(start_stop, ExprVars):
        self.start = start_stop.start
        self.stop = start_stop.stop
        self.step = start_stop.step
        self.curr = start_stop.curr
        self.expr_src = start_stop.expr_src
        return

    if stop is None:
        self.start = 0
        self.stop = (
            get_video_format(start_stop).num_planes
            if isinstance(start_stop, HoldsVideoFormat | VideoFormatLike)
            else start_stop.__index__()
        )
    else:
        if isinstance(start_stop, HoldsVideoFormat | VideoFormatLike):
            raise CustomTypeError(
                "start cannot be a video format when stop is provided.", self.__class__, start_stop
            )
        self.start = start_stop.__index__()
        self.stop = stop.__index__()

    self.step = (step or 1).__index__()

    if self.start < 0:
        raise CustomIndexError('"start" must be greater than or equal to 0.', self.__class__, self.start)

    if self.stop <= self.start:
        raise CustomIndexError('"stop" must be greater than "start".', self.__class__, (self.start, self.stop))

    self.expr_src = expr_src
    self.curr = self.start

curr instance-attribute

curr: int = start

Current index in iteration.

expr_src instance-attribute

expr_src: bool = expr_src

If True, variables are named as src0, src1, etc. Otherwise, "x", "y", "z", "a" and so on.

start instance-attribute

start: int

Starting index for variable generation (inclusive).

step instance-attribute

step: int = __index__()

Step size for iteration.

stop instance-attribute

stop: int

Ending index for variable generation (exclusive).

cycle classmethod

cycle() -> Iterator[str]

An infinite generator of variable names, looping through EXPR_VARS then continuing with srcX style.

Returns:

  • Iterator[str]

    An infinite iterator of variable names.

Source code in vsexprtools/util.py
181
182
183
184
185
186
187
188
189
190
@classmethod
def cycle(cls) -> Iterator[str]:
    """
    An infinite generator of variable names, looping through `EXPR_VARS` then continuing with `srcX` style.

    Returns:
        An infinite iterator of variable names.
    """
    for x in count():
        yield cls.get_var(x)

get_var classmethod

get_var(value: SupportsIndex) -> str

Get a variable name for a specific index.

Parameters:

Returns:

  • str

    The variable name.

Raises:

  • CustomIndexError

    If the index is negative.

Source code in vsexprtools/util.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
@classmethod
def get_var(cls, value: SupportsIndex) -> str:
    """
    Get a variable name for a specific index.

    Args:
        value: Index to convert to variable name.

    Returns:
        The variable name.

    Raises:
        CustomIndexError: If the index is negative.
    """
    value = value.__index__()

    if value < 0:
        raise CustomIndexError('"value" should be bigger than 0!')

    return cls._format_var(value, value > 25)