Skip to content

y5gfunc.expr.python2infix

python2infix

Python interface to generate infix DSL code, that can be further converted to vapoursynth Expr RPN code.

Refer to this document for documentations about the Python interface.

Classes:

Name Description
DSLExpr
SourceClip
Constant
BuiltInFunc

Functions:

Name Description
varname_toggle

A context manager to temporarily enable or disable the use of the varname package.

Attributes:

Name Type Description
ExprLike TypeAlias

ExprLike module-attribute

ExprLike: TypeAlias = Union['DSLExpr', int, float]

DSLExpr

DSLExpr(node: _Node)

Methods:

Name Description
__bool__
__add__
__sub__
__mul__
__truediv__
__pow__
__mod__
__lt__
__le__
__gt__
__ge__
__eq__
__ne__
__and__
__or__
__radd__
__rsub__
__rmul__
__rtruediv__
__rpow__
__rmod__
__rand__
__ror__
__neg__
__invert__

Attributes:

Name Type Description
dsl str
Source code in y5gfunc/expr/python2infix.py
def __init__(self, node: "_Node"):
    self._node = node

dsl property

dsl: str

__bool__

__bool__()
Source code in y5gfunc/expr/python2infix.py
def __bool__(self):
    raise TypeError(
        "Cannot evaluate a DSLExpr to a boolean. DSL expressions do not have a "
        "truth value in Python at generation time.\n"
        "Use 'BuiltInFunc.if_then_else(condition, true_expr, false_expr)' "
        "for conditional logic."
    )

__add__

__add__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __add__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("+", self, other))

__sub__

__sub__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __sub__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("-", self, other))

__mul__

__mul__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __mul__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("*", self, other))

__truediv__

__truediv__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __truediv__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("/", self, other))

__pow__

__pow__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __pow__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("**", self, other))

__mod__

__mod__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __mod__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("%", self, other))

__lt__

__lt__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __lt__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("<", self, other))

__le__

__le__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __le__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("<=", self, other))

__gt__

__gt__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __gt__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode(">", self, other))

__ge__

__ge__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __ge__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode(">=", self, other))

__eq__

__eq__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __eq__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("==", self, other))

__ne__

__ne__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __ne__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("!=", self, other))

__and__

__and__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __and__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("&&", self, other))

__or__

__or__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __or__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("||", self, other))

__radd__

__radd__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __radd__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("+", other, self))

__rsub__

__rsub__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rsub__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("-", other, self))

__rmul__

__rmul__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rmul__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("*", other, self))

__rtruediv__

__rtruediv__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rtruediv__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("/", other, self))

__rpow__

__rpow__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rpow__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("**", other, self))

__rmod__

__rmod__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rmod__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("%", other, self))

__rand__

__rand__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rand__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("&&", other, self))

__ror__

__ror__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __ror__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("||", other, self))

__neg__

__neg__()
Source code in y5gfunc/expr/python2infix.py
def __neg__(self):
    return DSLExpr(_UnaryOpNode("-", self))

__invert__

__invert__()
Source code in y5gfunc/expr/python2infix.py
def __invert__(self):
    return DSLExpr(_UnaryOpNode("~", self))

SourceClip

SourceClip(identifier: Union[str, int])

Bases: DSLExpr

Methods:

Name Description
__getitem__
access
__bool__
__add__
__sub__
__mul__
__truediv__
__pow__
__mod__
__lt__
__le__
__gt__
__ge__
__eq__
__ne__
__and__
__or__
__radd__
__rsub__
__rmul__
__rtruediv__
__rpow__
__rmod__
__rand__
__ror__
__neg__
__invert__

Attributes:

Name Type Description
props _PropertyAccessor
dsl str
Source code in y5gfunc/expr/python2infix.py
def __init__(self, identifier: Union[str, int]):
    if isinstance(identifier, str):
        if identifier in self._ALIASES:
            name = f"src{self._ALIASES.index(identifier)}"
        elif identifier.startswith("src"):
            name = identifier
        else:
            raise ValueError(f"Invalid string identifier: {identifier}")
    elif isinstance(identifier, int):
        name = f"src{identifier}"
    else:
        raise TypeError("SourceClip identifier must be a string or integer.")
    super().__init__(_InputNode(name))

props property

props: _PropertyAccessor

dsl property

dsl: str

__getitem__

__getitem__(key)
Source code in y5gfunc/expr/python2infix.py
def __getitem__(self, key):
    if not isinstance(self._node, _InputNode):
        raise TypeError("Pixel access is only valid on a direct source clip.")
    if not isinstance(key, tuple) or len(key) != 2:
        raise TypeError(
            "Pixel access requires a tuple of two indices, e.g., a[x, y]."
        )

    x, y = key
    if not isinstance(x, int) or not isinstance(y, int):
        raise TypeError("Pixel access indices must be integers, not expressions.")

    x_node = _to_node(x)
    y_node = _to_node(y)

    return DSLExpr(_PixelAccessNode(self._node, x_node, y_node))

access

access(x_expr: ExprLike, y_expr: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
def access(
    self,
    x_expr: ExprLike,
    y_expr: ExprLike,
) -> "DSLExpr":
    return DSLExpr(_FunctionCallNode("dyn", [self, x_expr, y_expr]))

__bool__

__bool__()
Source code in y5gfunc/expr/python2infix.py
def __bool__(self):
    raise TypeError(
        "Cannot evaluate a DSLExpr to a boolean. DSL expressions do not have a "
        "truth value in Python at generation time.\n"
        "Use 'BuiltInFunc.if_then_else(condition, true_expr, false_expr)' "
        "for conditional logic."
    )

__add__

__add__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __add__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("+", self, other))

__sub__

__sub__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __sub__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("-", self, other))

__mul__

__mul__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __mul__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("*", self, other))

__truediv__

__truediv__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __truediv__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("/", self, other))

__pow__

__pow__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __pow__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("**", self, other))

__mod__

__mod__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __mod__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("%", self, other))

__lt__

__lt__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __lt__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("<", self, other))

__le__

__le__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __le__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("<=", self, other))

__gt__

__gt__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __gt__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode(">", self, other))

__ge__

__ge__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __ge__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode(">=", self, other))

__eq__

__eq__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __eq__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("==", self, other))

__ne__

__ne__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __ne__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("!=", self, other))

__and__

__and__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __and__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("&&", self, other))

__or__

__or__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __or__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("||", self, other))

__radd__

__radd__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __radd__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("+", other, self))

__rsub__

__rsub__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rsub__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("-", other, self))

__rmul__

__rmul__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rmul__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("*", other, self))

__rtruediv__

__rtruediv__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rtruediv__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("/", other, self))

__rpow__

__rpow__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rpow__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("**", other, self))

__rmod__

__rmod__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rmod__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("%", other, self))

__rand__

__rand__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __rand__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("&&", other, self))

__ror__

__ror__(other: ExprLike)
Source code in y5gfunc/expr/python2infix.py
def __ror__(self, other: ExprLike):
    return DSLExpr(_BinaryOpNode("||", other, self))

__neg__

__neg__()
Source code in y5gfunc/expr/python2infix.py
def __neg__(self):
    return DSLExpr(_UnaryOpNode("-", self))

__invert__

__invert__()
Source code in y5gfunc/expr/python2infix.py
def __invert__(self):
    return DSLExpr(_UnaryOpNode("~", self))

Constant

Attributes:

Name Type Description
pi
N
X
Y
width
height

pi class-attribute instance-attribute

pi = DSLExpr(_ConstantNode('pi'))

N class-attribute instance-attribute

N = DSLExpr(_ConstantNode('N'))

X class-attribute instance-attribute

X = DSLExpr(_ConstantNode('X'))

Y class-attribute instance-attribute

Y = DSLExpr(_ConstantNode('Y'))

width class-attribute instance-attribute

width = DSLExpr(_ConstantNode('width'))

height class-attribute instance-attribute

height = DSLExpr(_ConstantNode('height'))

BuiltInFunc

Methods:

Name Description
sin
cos
log
exp
sqrt
abs
min
max
clamp
sort
trunc
round
floor
if_then_else
logical_not
bitwise_or
bitwise_xor
bitwise_and

sin staticmethod

sin(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def sin(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("sin", [x]))

cos staticmethod

cos(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def cos(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("cos", [x]))

log staticmethod

log(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def log(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("log", [x]))

exp staticmethod

exp(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def exp(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("exp", [x]))

sqrt staticmethod

sqrt(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def sqrt(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("sqrt", [x]))

abs staticmethod

abs(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def abs(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("abs", [x]))

min staticmethod

min(x: ExprLike, y: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def min(x: ExprLike, y: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("min", [x, y]))

max staticmethod

max(x: ExprLike, y: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def max(x: ExprLike, y: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("max", [x, y]))

clamp staticmethod

clamp(x: ExprLike, min_val: ExprLike, max_val: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def clamp(
    x: ExprLike,
    min_val: ExprLike,
    max_val: ExprLike,
) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("clamp", [x, min_val, max_val]))

sort staticmethod

sort(items: list[ExprLike]) -> list[DSLExpr]
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def sort(items: list[ExprLike]) -> list[DSLExpr]:
    if not isinstance(items, list) or not items:
        raise TypeError("Input for sort must be a non-empty list of expressions.")
    return [
        DSLExpr(_FunctionCallNode(f"nth_{i + 1}", items)) for i in range(len(items))
    ]

trunc staticmethod

trunc(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def trunc(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("trunc", [x]))

round staticmethod

round(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def round(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("round", [x]))

floor staticmethod

floor(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def floor(x: ExprLike) -> DSLExpr:
    return DSLExpr(_FunctionCallNode("floor", [x]))

if_then_else staticmethod

if_then_else(cond: ExprLike, true_expr: ExprLike, false_expr: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def if_then_else(
    cond: ExprLike,
    true_expr: ExprLike,
    false_expr: ExprLike,
) -> DSLExpr:
    return DSLExpr(_TernaryOpNode(cond, true_expr, false_expr))

logical_not staticmethod

logical_not(x: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def logical_not(x: ExprLike) -> DSLExpr:
    return DSLExpr(_UnaryOpNode("!", x))

bitwise_or staticmethod

bitwise_or(x: ExprLike, y: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def bitwise_or(x: ExprLike, y: ExprLike) -> DSLExpr:
    return DSLExpr(_BinaryOpNode("|", x, y))

bitwise_xor staticmethod

bitwise_xor(x: ExprLike, y: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def bitwise_xor(x: ExprLike, y: ExprLike) -> DSLExpr:
    return DSLExpr(_BinaryOpNode("^", x, y))

bitwise_and staticmethod

bitwise_and(x: ExprLike, y: ExprLike) -> DSLExpr
Source code in y5gfunc/expr/python2infix.py
@staticmethod
def bitwise_and(x: ExprLike, y: ExprLike) -> DSLExpr:
    return DSLExpr(_BinaryOpNode("&", x, y))

varname_toggle

varname_toggle(enable: bool)

A context manager to temporarily enable or disable the use of the varname package. Disabling this can significantly speed up the generation of complex DSL expressions. It is disabled by default for performance reasons.

Usage

with varname_toggle(True): # code where varname is enabled ...

varname is disabled again here

Source code in y5gfunc/expr/python2infix.py
@contextlib.contextmanager
def varname_toggle(enable: bool):
    """
    A context manager to temporarily enable or disable the use of the `varname` package.
    Disabling this can significantly speed up the generation of complex DSL expressions.
    It is disabled by default for performance reasons.

    Usage:
        with varname_toggle(True):
            # code where varname is enabled
            ...
        # varname is disabled again here
    """
    global _USE_VARNAME
    old_value = _USE_VARNAME
    _USE_VARNAME = enable
    try:
        yield
    finally:
        _USE_VARNAME = old_value