Skip to content

Context

pyrung.core.program.context

Program

Container for PLC logic (rungs and subroutines).

Used as a context manager to capture rungs

with Program() as logic: with Rung(Button): out(Light)

Also works with PLCRunner

runner = PLCRunner(logic)

add_rung

add_rung(rung: Rung) -> None

Add a rung to the program or current subroutine.

start_subroutine

start_subroutine(name: str) -> None

Start defining a subroutine.

end_subroutine

end_subroutine() -> None

End subroutine definition.

call_subroutine

call_subroutine(
    name: str, state: SystemState
) -> SystemState

Execute a subroutine by name (legacy state-based API).

call_subroutine_ctx

call_subroutine_ctx(name: str, ctx: ScanContext) -> None

Execute a subroutine by name within a ScanContext.

current classmethod

current() -> Program | None

Get the current program context (if any).

register_dialect classmethod

register_dialect(
    name: str, validator: DialectValidator
) -> None

Register a portability validator callback for a dialect name.

registered_dialects classmethod

registered_dialects() -> tuple[str, ...]

Return registered dialect names in deterministic order.

validate

validate(
    dialect: str, *, mode: str = "warn", **kwargs: Any
) -> Any

Run dialect-specific portability validation for this Program.

evaluate

evaluate(ctx: ScanContext) -> None

Evaluate all main rungs in order (not subroutines) within a ScanContext.

Rung

Context manager for defining a rung.

Example

with Rung(Button): out(Light)

with Rung(Step == 0): out(Light1) copy(1, Step, oneshot=True)

comment property writable

comment: str | None

Rung comment (max 1400 chars).

Subroutine

Context manager for defining a subroutine.

Subroutines are named blocks of rungs that are only executed when called.

Example

with subroutine("my_sub"): with Rung(): out(Light)

SubroutineFunc

A decorated function that represents a subroutine.

Created by using @subroutine("name") as a decorator. When passed to call(), auto-registers with the current Program on first use.

Example

@subroutine("init") def init_sequence(): with Rung(): out(Light)

with Program() as logic: with Rung(Button): call(init_sequence)

name property

name: str

The subroutine name.

ForLoop

Context manager for a repeated instruction block within a rung.

Branch

Context manager for a parallel branch within a rung.

A branch executes when both the parent rung conditions AND the branch's own conditions are true.

Example

with Rung(Step == 0): out(Light1) with branch(AutoMode): # Only executes if Step==0 AND AutoMode out(Light2) copy(1, Step, oneshot=True)

subroutine

subroutine(name: str, *, strict: bool = True) -> Subroutine

Define a named subroutine.

Subroutines are only executed when called via call(). They are NOT executed during normal program scan.

Example

with Program() as logic: with Rung(Button): call("my_sub")

with subroutine("my_sub"):
    with Rung():
        out(Light)

forloop

forloop(count: Tag | int, oneshot: bool = False) -> ForLoop

Create a repeated instruction block context.

Example

with Rung(Enable): with forloop(10) as loop: copy(Source[loop.idx + 1], Dest[loop.idx + 1])

branch

branch(*conditions: ConditionTerm) -> Branch

Create a parallel branch within a rung.

A branch executes when both the parent rung conditions AND the branch's own conditions are true.

Example

with Rung(Step == 0): out(Light1) with branch(AutoMode): # Only executes if Step==0 AND AutoMode out(Light2) copy(1, Step, oneshot=True)

Parameters:

Name Type Description Default
conditions ConditionTerm

Conditions that must be true (in addition to parent rung) for this branch's instructions to execute.

()

Returns:

Type Description
Branch

Branch context manager.