xnano.operations
xnano.operations
¶
xnano.operations
Operations that change or animate a buffer, placement or canvas.
Pass operations to update or pipe them with |. Create a reusable hook from a function
with hook. Animated effects such as Effect
(Effect.fade_to(...), Effect.dissolve(...)) and
CancelEffect are operations as well and apply the same way.
The Dynamic operations (DynamicFill,
DynamicWrite,
DynamicLayout,
DynamicFuzz,
DynamicPixels and
DynamicRaster) are operations whose settings are dataclass
fields holding plain values: numbers, text, cells and styles. The fields are read every time the
operation is applied, and assigning a field of an applied operation applies it again with the
new value. A field does not follow an expression over time. To make a position or size follow
an expression, give the expression to the placement itself (placement.x = ...) or change the
field from a hook. The parameters of an effect, which can follow an expression while it runs,
are built with effect.
Classes:
-
DynamicFillSchema–The settings of
DynamicFill, as keyword arguments. -
DynamicFuzzSchema–The settings of
DynamicFuzz, as keyword arguments. -
DynamicLayoutSchema–The settings of
DynamicLayout, as keyword arguments. -
DynamicPixelsSchema–The settings of
DynamicPixels, as keyword arguments. -
DynamicRasterSchema–The settings of
DynamicRaster, as keyword arguments. -
DynamicWriteSchema–The settings of
DynamicWrite, as keyword arguments. -
ContextualOperation–A reusable hook that can be piped onto any canvas or placement.
-
DynamicFill–An operation that fills every cell of its target with a character or a
-
DynamicWrite–An operation that writes text into the cells of its target.
-
DynamicLayout–An operation that arranges the children attached to its target in a row, a column or an
-
DynamicFuzz–An operation that replaces a percentage of the visible glyphs of its target with noise.
-
DynamicPixels–An operation that colors pixels from a shader function or from an explicit iterable of
-
DynamicRaster–An operation that draws a row-major RGBA8 buffer as pixels.
Attributes:
-
OperationResult(TypeAlias) –What a function decorated with
hookmay return: an operation (an effect or a -
ApplyStyle–An operation that patches a style onto every cell of its target.
-
Background–An operation that sets the background color of every cell of its target.
-
Border–An operation that draws a border around the edge of its target.
-
CancelEffect–An operation that stops the running effect that was started with a key.
-
Clear–A widget of blank cells that hide whatever is behind them.
-
Dissolve–An effect that dissolves the glyphs of its target away, cell by cell.
-
Effect–A composable animation of colors or glyphs, applied like any other operation.
-
Fade–An effect that fades the colors of its target toward a foreground and a background color.
-
Fill–An operation that fills every cell of its target with one character or
-
Foreground–An operation that sets the foreground color of every cell of its target.
-
Fuzz–An operation that replaces a percentage of the visible glyphs of its target with noise.
-
Glitch–An effect that randomly corrupts and displaces glyphs of its target.
-
Layout–An operation that arranges the children attached to a buffer in a row, a column or an
-
Margin–Horizontal and vertical inset distances, in cells.
-
Move–An operation that moves its target relative to its current position in its parent.
-
Operation–The base every operation shares: an action that changes a buffer, a placement or a canvas.
-
Paste–An operation that copies the cells of another buffer into its target.
-
Place–An operation that moves its target to an absolute position in its parent.
-
Resize–An operation that resizes its target to a width and a height in cells.
-
Write–An operation that writes text into its target at a cell position.
OperationResult
module-attribute
¶
OperationResult: TypeAlias = '_core.Operation | _core.Style | None'
What a function decorated with hook may return: an operation (an effect or a
Dynamic operation included), a style or None.
ApplyStyle
module-attribute
¶
ApplyStyle = _CoreApplyStyle
An operation that patches a style onto every cell of its target.
Colors and modifiers that the Style leaves unset stay as they
are, so it adds to the look of the target instead of replacing it.
Background
module-attribute
¶
Background = _CoreBackground
An operation that sets the background color of every cell of its target.
The color is a name, a hex string, a (red, green, blue) tuple or a
Color.
Border
module-attribute
¶
Border = _CoreBorder
An operation that draws a border around the edge of its target.
The kind is 'plain', 'rounded', 'double' or 'thick', and a
Style colors the border cells.
Maps to ratatui::widgets::Block
with BorderType.
CancelEffect
module-attribute
¶
CancelEffect = _CoreCancelEffect
An operation that stops the running effect that was started with a key.
Give an effect a key when it is created, then apply CancelEffect(key) to stop it
before it ends. The target looks as it did before the effect.
import xnano
from xnano.operations import CancelEffect, Dissolve
with xnano.new(width=10, height=1) as canvas:
text = canvas.screen.write('hello')
text | Dissolve(duration_milliseconds=100, seed=1, key='fade-out')
text | CancelEffect('fade-out')
canvas.clock.step(delta_seconds=0.05)
canvas.clock.step(delta_seconds=0.05)
assert canvas.screen.render() == 'hello'
Clear
module-attribute
¶
Clear = _CoreClear
A widget of blank cells that hide whatever is behind them.
Write one onto a canvas to erase the area it covers. Its symbol is the one-column
character every cell shows, a space by default.
Maps to ratatui::widgets::Clear.
Dissolve
module-attribute
¶
Dissolve = _CoreDissolve
An effect that dissolves the glyphs of its target away, cell by cell.
With should_reverse=True the glyphs appear instead. A seed makes the order of the
cells repeatable, and a key lets CancelEffect stop
it. It is the class form of Effect.dissolve(...).
Maps to tachyonfx::fx::dissolve
and tachyonfx::fx::coalesce.
import xnano
from xnano.operations import Dissolve
with xnano.new(width=10, height=1) as canvas:
text = canvas.screen.write('hello')
text | Dissolve(duration_milliseconds=100, seed=1)
canvas.clock.step(delta_seconds=0.05)
canvas.clock.step(delta_seconds=0.05)
assert canvas.screen.render() != 'hello'
Effect
module-attribute
¶
Effect = _CoreEffect
A composable animation of colors or glyphs, applied like any other operation.
The method constructors Effect.fade_to(...), Effect.dissolve(...) and the other
named effects are the operation form of effect. The settings of
an effect, such as foreground and duration_milliseconds, can be read and assigned,
and effects combine with then and repeat.
import xnano
from xnano.operations import Effect
with xnano.new(width=10, height=1) as canvas:
text = canvas.screen.write('hello')
before = canvas.screen.render(format='ansi')
fade = Effect.fade_to('red', duration_milliseconds=100)
fade.duration_milliseconds = 200
text | fade
canvas.clock.step(delta_seconds=0.05)
canvas.clock.step(delta_seconds=0.05)
assert canvas.screen.render(format='ansi') != before
Fade
module-attribute
¶
Fade = _CoreFade
An effect that fades the colors of its target toward a foreground and a background color.
Leave a color out to keep it as it is. It is the class form of Effect.fade_to(...).
Maps to tachyonfx::fx::fade_to.
import xnano
from xnano.operations import Fade
with xnano.new(width=10, height=1) as canvas:
text = canvas.screen.write('hello')
before = canvas.screen.render(format='ansi')
text | Fade(foreground='red', duration_milliseconds=100)
canvas.clock.step(delta_seconds=0.05)
canvas.clock.step(delta_seconds=0.05)
assert canvas.screen.render(format='ansi') != before
Fill
module-attribute
¶
Fill = _CoreFill
An operation that fills every cell of its target with one character or
Cell.
Use DynamicFill for the form whose cell can be changed
after it is applied.
Foreground
module-attribute
¶
Foreground = _CoreForeground
An operation that sets the foreground color of every cell of its target.
The color is a name, a hex string, a (red, green, blue) tuple or a
Color.
Fuzz
module-attribute
¶
Fuzz = _CoreFuzz
An operation that replaces a percentage of the visible glyphs of its target with noise.
It changes the cells once, when it is applied, and does not need a running canvas. Blank
and wide cells are left alone. A seed makes the noise repeatable. Use
DynamicFuzz for the form whose settings can be changed
after it is applied.
Glitch
module-attribute
¶
Glitch = _CoreGlitch
An effect that randomly corrupts and displaces glyphs of its target.
The intensity is the fraction of cells affected, from 0.0 to 1.0, and a seed
makes the result repeatable. It is the class form of Effect.glitch(...).
Maps to tachyonfx::fx::Glitch.
import xnano
from xnano.operations import Glitch
with xnano.new(width=10, height=1) as canvas:
text = canvas.screen.write('hello')
text | Glitch(intensity=1.0, duration_milliseconds=100, seed=1)
canvas.clock.step(delta_seconds=0.05)
canvas.clock.step(delta_seconds=0.05)
assert canvas.screen.render() != 'hello'
Layout
module-attribute
¶
Layout = _CoreLayout
An operation that arranges the children attached to a buffer in a row, a column or an overlay.
The sizes are one SizeRule per child along the main axis,
spacing is the number of cells between children and padding the number of cells
between the edge of the parent and its children. Use
DynamicLayout for the form whose settings can be
changed after it is applied.
Maps to ratatui::layout::Layout
for 'row' and 'column'.
from xnano.buffers import Buffer
from xnano.layout import SizeRule
from xnano.operations import Layout
parent = Buffer(width=12, height=3)
left, right = Buffer(), Buffer()
parent.attach(left, geometry='inherit')
parent.attach(right, geometry='inherit')
parent.update(Layout('row', sizes=[SizeRule.cells(4), SizeRule.fill()], spacing=1))
assert (left.width, right.x, right.width) == (4, 5, 7)
Margin
module-attribute
¶
Margin = _CoreMargin
Horizontal and vertical inset distances, in cells.
Reach for it to shrink or grow a Rectangle. To give a
placement a margin, set its margin instead, as in canvas.screen.write('hi', margin=1).
Move
module-attribute
¶
Move = _CoreMove
An operation that moves its target relative to its current position in its parent.
x moves it right and y moves it down; negative numbers move it left and up. Use
Place to move it to an absolute position instead.
import xnano
from xnano.operations import Move
with xnano.new(width=10, height=3) as canvas:
label = canvas.screen.write('hi')
label | Move(2, 1)
assert (label.x, label.y) == (2, 1)
label | Move(x=-1)
assert (label.x, label.y) == (1, 1)
Operation
module-attribute
¶
Operation = _CoreOperation
The base every operation shares: an action that changes a buffer, a placement or a canvas.
Apply an operation with | or update. Combine operations with | or then and
they run in order; the combination is an operation as well.
Paste
module-attribute
¶
Paste = _CorePaste
An operation that copies the cells of another buffer into its target.
The source is copied when the operation is created, so applying it again copies the same
cells. x and y are where the top-left cell of the source lands, and mode is
'replace' or 'preserve_background'.
Place
module-attribute
¶
Place = _CorePlace
An operation that moves its target to an absolute position in its parent.
Use Move to move it relative to where it is instead.
import xnano
from xnano.operations import Place
with xnano.new(width=10, height=3) as canvas:
label = canvas.screen.write('hi')
label | Place(5, 2)
assert (label.x, label.y) == (5, 2)
Resize
module-attribute
¶
Resize = _CoreResize
An operation that resizes its target to a width and a height in cells.
Content is kept where it still fits.
Write
module-attribute
¶
Write = _CoreWrite
An operation that writes text into its target at a cell position.
Text is clipped by display columns, and mode is 'replace' or
'preserve_background'. Use DynamicWrite for the
form whose text and position can be changed after it is applied.
DynamicFillSchema
¶
DynamicFuzzSchema
¶
DynamicLayoutSchema
¶
Bases: TypedDict
The settings of DynamicLayout, as keyword arguments.
Attributes:
DynamicPixelsSchema
¶
Bases: TypedDict
The settings of DynamicPixels, as keyword arguments.
Attributes:
-
shader(Callable[[int, int], ColorValue | None] | None) –Optional function
(x, y) -> colorevaluated across the target's pixel grid. -
pixels(Iterable[PixelEntry] | None) –Optional explicit iterable of
(x, y, (red, green, blue))pixels. -
encoding(PixelEncoding) –'half_blocks'or'braille'. -
clip(bool) –Whether out-of-bounds pixels are clipped instead of raising
IndexError. -
x(int | None) –Column offset of the explicit pixels, or
None. -
y(int | None) –Row offset of the explicit pixels, or
None.
shader
instance-attribute
¶
Optional function (x, y) -> color evaluated across the target's pixel grid.
pixels
instance-attribute
¶
pixels: Iterable[PixelEntry] | None
Optional explicit iterable of (x, y, (red, green, blue)) pixels.
DynamicRasterSchema
¶
Bases: TypedDict
The settings of DynamicRaster, as keyword arguments.
Attributes:
-
rgba(bytes) –width * height * 4bytes of RGBA8 pixel data. -
width(int) –Image width in pixels.
-
height(int) –Image height in pixels.
-
x(int) –Top-left pixel column.
-
y(int) –Top-left pixel row.
-
encoding(PixelEncoding) –'half_blocks'or'braille'. -
mode(RasterMode) –'over'or'replace'.
DynamicWriteSchema
¶
Bases: TypedDict
The settings of DynamicWrite, as keyword arguments.
Attributes:
-
text(TextContent) –Text or styled text to write.
-
x(int) –Starting column.
-
y(int) –Starting row.
-
style(Style | None) –Optional style applied to the written text.
-
mode(WriteMode) –Write blending mode (
'replace','preserve_background').
ContextualOperation
¶
ContextualOperation(callback: Callable[[Context[DepsT, StateT]], object] | None = None, *, on: HookTrigger[DepsT, StateT] | None = None, every: float | None = None, after: float | None = None, keys: tuple[str, ...] = (), requires_deps: bool = False, if_running: IfRunning = 'replace', steps: Sequence[Operation] = ())
Bases: Operation, Generic[DepsT, StateT]
A reusable hook that can be piped onto any canvas or placement.
Build one with hook. Apply it with |, |= or update,
and it attaches the same hook that target.hook(function, ...) would.
Attributes:
-
on(HookTrigger[DepsT, StateT] | None) –When the hook runs, or
Nonefor every input event. -
every(float | None) –Seconds (
'tick') or beats ('beat') between runs, orNone. -
after(float | None) –Delay before the one run, or
None. -
keys(tuple[str, ...]) –Key names an
on='key'hook reacts to.
Examples:
import xnano
counted = []
@xnano.hook(on='tick', every=0.5)
def count(context: xnano.Context) -> None:
counted.append(context.clock.elapsed_seconds)
with xnano.new(width=16, height=1) as canvas:
canvas.screen | count
canvas.clock.step(delta_seconds=1.0)
assert len(counted) == 2
Create a reusable hook. Use hook instead of calling this.
Parameters:
-
callback(Callable[[Context[DepsT, StateT]], object] | None, default:None) –Function called with a
Contextwhen the hook runs. -
on(HookTrigger[DepsT, StateT] | None, default:None) –When the hook runs: a kind of event, the name of a custom event, or a condition.
-
every(float | None, default:None) –Interval between runs.
-
after(float | None, default:None) –Delay before the one run.
-
keys(tuple[str, ...], default:()) –Key names to react to when
on='key'. -
requires_deps(bool, default:False) –Whether the hook needs the canvas's dependencies.
-
if_running(IfRunning, default:'replace') –What an
async defhook does when it runs again before its previous run has finished. -
steps(Sequence[Operation], default:()) –Chained operations to apply in sequence.
Methods:
-
then–Return a new operation that applies this operation and then
other. -
__or__–Combine this operation with another operation or style.
-
__ror__–Apply this operation to
targetwhen piped withtarget | operation. -
apply_to_surface–Apply the chained steps of this operation to a core
Surface. -
apply_to–Attach this hook to
target, or apply its chained steps. -
evaluate–Compute the expression's value.
-
when–An operation that applies only while
conditionis true.
Source code in xnano/operations.py
def __init__(
self,
callback: Callable[[Context[DepsT, StateT]], object] | None = None,
*,
on: HookTrigger[DepsT, StateT] | None = None,
every: float | None = None,
after: float | None = None,
keys: tuple[str, ...] = (),
requires_deps: bool = False,
if_running: IfRunning = 'replace',
steps: Sequence[_core.Operation] = (),
) -> None:
"""
Create a reusable hook. Use [`hook`][xnano.hooks.hook] instead of calling this.
Args:
callback: Function called with a [`Context`][xnano.hooks.Context] when the hook
runs.
on: When the hook runs: a kind of event, the name of a custom event, or a
condition.
every: Interval between runs.
after: Delay before the one run.
keys: Key names to react to when ``on='key'``.
requires_deps: Whether the hook needs the canvas's dependencies.
if_running: What an ``async def`` hook does when it runs again before its
previous run has finished.
steps: Chained operations to apply in sequence.
"""
super().__init__()
self._callback = callback
self._hook_on = on
self._every = every
self._after = after
self._keys = tuple(keys)
self._requires_deps = requires_deps
self._if_running: IfRunning = if_running
self._steps: tuple[_core.Operation, ...] = tuple(steps)
on
property
¶
on: HookTrigger[DepsT, StateT] | None
When the hook runs, as it was given, or None for every input event.
then
¶
then(other: Operation) -> ContextualOperation[DepsT, StateT]
Return a new operation that applies this operation and then other.
Parameters:
-
other(Operation) –The next operation to run.
Source code in xnano/operations.py
def then(self, other: _core.Operation) -> ContextualOperation[DepsT, StateT]:
"""
Return a new operation that applies this operation and then ``other``.
Args:
other: The next operation to run.
"""
combined: list[_core.Operation] = list(self._steps) if self._steps else [self]
if isinstance(other, ContextualOperation) and other._steps:
combined.extend(other._steps)
else:
combined.append(other)
return ContextualOperation(steps=combined)
__or__
¶
__or__(other: Operation | Style) -> ContextualOperation[DepsT, StateT]
Combine this operation with another operation or style.
Source code in xnano/operations.py
__ror__
¶
Apply this operation to target when piped with target | operation.
Source code in xnano/operations.py
def __ror__(self, target: TargetT) -> TargetT:
"""Apply this operation to ``target`` when piped with ``target | operation``."""
if isinstance(target, _core.Operation):
return cast('TargetT', ContextualOperation(steps=(target, self)))
self.apply_to(
cast(
'Canvas[DepsT, StateT] | Placement[DepsT, StateT] | _core.Surface',
target,
)
)
return target
apply_to_surface
¶
apply_to_surface(surface: Surface) -> None
Apply the chained steps of this operation to a core Surface.
A hook has no surface to listen on, so only its chained steps apply.
Parameters:
-
surface(Surface) –The buffer or region to modify.
Source code in xnano/operations.py
def apply_to_surface(self, surface: _core.Surface) -> None:
"""
Apply the chained steps of this operation to a core [`Surface`][xnano_core.Surface].
A hook has no surface to listen on, so only its chained steps apply.
Args:
surface: The buffer or region to modify.
"""
for step in self._steps:
if isinstance(step, (ContextualOperation, DynamicOperation)):
step.apply_to_surface(surface)
else:
surface.update(step)
apply_to
¶
Attach this hook to target, or apply its chained steps.
Parameters:
Source code in xnano/operations.py
def apply_to(
self,
target: Canvas[DepsT, StateT] | Placement[DepsT, StateT] | _core.Surface,
) -> None:
"""
Attach this hook to ``target``, or apply its chained steps.
Args:
target: A [`Canvas`][xnano.canvas.Canvas], a
[`Placement`][xnano.layout.Placement] or a [`Surface`][xnano_core.Surface].
"""
if isinstance(target, _core.Surface):
self.apply_to_surface(target)
return
if self._steps:
from xnano.canvas import Canvas
# What draws goes on a canvas's screen; what listens attaches to the canvas.
surface = target.screen if isinstance(target, Canvas) else target
for step in self._steps:
if isinstance(step, (ContextualOperation, DynamicOperation)):
step.apply_to(
cast('Canvas[object, object] | Placement[object, object]', target)
)
else:
surface.update(step)
return
callback = self._callback
if callback is None:
return
target.hook(
callback,
on=self._hook_on,
every=self._every,
after=self._after,
keys=self._keys or None,
requires_deps=self._requires_deps,
if_running=self._if_running,
)
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
DynamicFill
dataclass
¶
Bases: DynamicOperation
An operation that fills every cell of its target with a character or a
Cell.
The cell setting is a plain value. It is read every time the operation is applied, and
assigning it on an applied operation applies the operation again with the new value.
Attributes:
Examples:
import xnano
from xnano.operations import DynamicFill
dots = DynamicFill(cell='.')
with xnano.new('hi', width=4, height=1) as canvas:
canvas.screen | dots
dots.cell = '#'
assert '#' in canvas.screen.render()
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to_surface–Apply this customization to a buffer, widget or region.
-
apply_to–Apply this operation to a canvas, placement or surface.
cell
class-attribute
instance-attribute
¶
The character or cell used to fill the target.
parameters
property
¶
The parameters by name, with defaults filled in.
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it again, so the operation is a live handle on what it changed. A running effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the operation to.
Source code in xnano/_internal/_dynamic_operations.py
def apply_to(
self,
target: Canvas[object, object] | Placement[object, object] | _core.Surface,
) -> None:
"""
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so the operation is a live handle on what it changed. A running effect is
stopped and started again with the new values.
Args:
target: The target to apply the operation to.
"""
from xnano.canvas import Canvas
from xnano.layout._placement import Placement
self._attach(target)
if isinstance(target, (Placement, Canvas)):
target._buffer.update(self)
if self.customization_kind.startswith('effect.'):
target._canvas._record_effect(cast('EffectParameters', self), target)
elif isinstance(target, _core.Surface):
target.update(self)
DynamicWrite
dataclass
¶
DynamicWrite(text: TextContent = '', x: int = 0, y: int = 0, style: Style | None = None, mode: WriteMode = 'replace')
Bases: DynamicOperation
An operation that writes text into the cells of its target.
Its settings are plain values: text, a position, a style and a blending mode. They are read
every time the operation is applied, and assigning one on an applied operation applies the
operation again with the new value. A position does not follow an expression over time: to
make the text move with an expression, give the expression to a placement
(placement.x = ...) or change x and y from a hook.
Attributes:
-
text(TextContent) –Text or styled text to write.
-
x(int) –Starting column.
-
y(int) –Starting row.
-
style(Style | None) –Optional style applied to the written text.
-
mode(WriteMode) –Write blending mode (
'replace','preserve_background').
Examples:
import xnano
from xnano.operations import DynamicWrite
label = DynamicWrite(text='ab')
with xnano.new(width=4, height=1) as canvas:
canvas.screen | label
label.text = 'cd'
assert 'cd' in canvas.screen.render()
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to_surface–Apply this customization to a buffer, widget or region.
-
apply_to–Apply this operation to a canvas, placement or surface.
style
class-attribute
instance-attribute
¶
style: Style | None = None
Optional style applied to the written text.
mode
class-attribute
instance-attribute
¶
mode: WriteMode = 'replace'
Write blending mode ('replace', 'preserve_background').
parameters
property
¶
The parameters by name, with defaults filled in.
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it again, so the operation is a live handle on what it changed. A running effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the operation to.
Source code in xnano/_internal/_dynamic_operations.py
def apply_to(
self,
target: Canvas[object, object] | Placement[object, object] | _core.Surface,
) -> None:
"""
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so the operation is a live handle on what it changed. A running effect is
stopped and started again with the new values.
Args:
target: The target to apply the operation to.
"""
from xnano.canvas import Canvas
from xnano.layout._placement import Placement
self._attach(target)
if isinstance(target, (Placement, Canvas)):
target._buffer.update(self)
if self.customization_kind.startswith('effect.'):
target._canvas._record_effect(cast('EffectParameters', self), target)
elif isinstance(target, _core.Surface):
target.update(self)
DynamicLayout
dataclass
¶
DynamicLayout(arrangement: Literal['row', 'column', 'overlay'] = 'column', sizes: Sequence[SizeRule] = (), spacing: int = 0, padding: int = 0)
Bases: DynamicOperation
An operation that arranges the children attached to its target in a row, a column or an overlay.
Its settings are plain values: the arrangement, the size rules, the spacing and the padding. They are read every time the operation is applied, and assigning one on an applied operation applies the operation again with the new value.
Attributes:
-
arrangement(Literal['row', 'column', 'overlay']) –'row','column'or'overlay'. -
sizes(Sequence[SizeRule]) –Size rules for each child along the main axis.
-
spacing(int) –Cells between adjacent children.
-
padding(int) –Cells inset from the parent's edges.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to_surface–Apply this customization to a buffer, widget or region.
-
apply_to–Apply this operation to a canvas, placement or surface.
arrangement
class-attribute
instance-attribute
¶
arrangement: Literal['row', 'column', 'overlay'] = 'column'
'row', 'column' or 'overlay'.
sizes
class-attribute
instance-attribute
¶
Size rules for each child along the main axis.
parameters
property
¶
The parameters by name, with defaults filled in.
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it again, so the operation is a live handle on what it changed. A running effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the operation to.
Source code in xnano/_internal/_dynamic_operations.py
def apply_to(
self,
target: Canvas[object, object] | Placement[object, object] | _core.Surface,
) -> None:
"""
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so the operation is a live handle on what it changed. A running effect is
stopped and started again with the new values.
Args:
target: The target to apply the operation to.
"""
from xnano.canvas import Canvas
from xnano.layout._placement import Placement
self._attach(target)
if isinstance(target, (Placement, Canvas)):
target._buffer.update(self)
if self.customization_kind.startswith('effect.'):
target._canvas._record_effect(cast('EffectParameters', self), target)
elif isinstance(target, _core.Surface):
target.update(self)
DynamicFuzz
dataclass
¶
Bases: DynamicOperation
An operation that replaces a percentage of the visible glyphs of its target with noise.
Its settings are plain values: an intensity and an optional seed. They are read every time the operation is applied, and assigning one on an applied operation applies the operation again with the new value.
Attributes:
-
intensity(int) –Percentage of glyphs to replace (
0to100). -
seed(int | None) –Optional random seed for repeatable noise.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to_surface–Apply this customization to a buffer, widget or region.
-
apply_to–Apply this operation to a canvas, placement or surface.
intensity
class-attribute
instance-attribute
¶
intensity: int = 10
Percentage of glyphs to replace (0 to 100).
seed
class-attribute
instance-attribute
¶
seed: int | None = None
Optional random seed for repeatable noise.
parameters
property
¶
The parameters by name, with defaults filled in.
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it again, so the operation is a live handle on what it changed. A running effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the operation to.
Source code in xnano/_internal/_dynamic_operations.py
def apply_to(
self,
target: Canvas[object, object] | Placement[object, object] | _core.Surface,
) -> None:
"""
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so the operation is a live handle on what it changed. A running effect is
stopped and started again with the new values.
Args:
target: The target to apply the operation to.
"""
from xnano.canvas import Canvas
from xnano.layout._placement import Placement
self._attach(target)
if isinstance(target, (Placement, Canvas)):
target._buffer.update(self)
if self.customization_kind.startswith('effect.'):
target._canvas._record_effect(cast('EffectParameters', self), target)
elif isinstance(target, _core.Surface):
target.update(self)
DynamicPixels
dataclass
¶
DynamicPixels(shader: Callable[[int, int], ColorValue | None] | None = None, pixels: Iterable[PixelEntry] | None = None, encoding: PixelEncoding = 'half_blocks', clip: bool = True, x: int | None = None, y: int | None = None)
Bases: DynamicOperation
An operation that colors pixels from a shader function or from an explicit iterable of pixels.
The shader is called with the column and row of every pixel of the target's pixel grid
and returns a color, or None to leave the pixel as it is. The settings are plain values
and functions. They are read every time the operation is applied, and assigning one on an
applied operation applies the operation again with the new value.
Attributes:
-
shader(Callable[[int, int], ColorValue | None] | None) –Optional function
(x, y) -> colorevaluated across the target's pixel grid. -
pixels(Iterable[PixelEntry] | None) –Optional explicit iterable of
(x, y, (red, green, blue))pixels. -
encoding(PixelEncoding) –'half_blocks'or'braille'. -
clip(bool) –Whether out-of-bounds pixels are clipped instead of raising
IndexError. -
x(int | None) –Column offset of the explicit pixels, or
None. -
y(int | None) –Row offset of the explicit pixels, or
None.
Methods:
-
apply_to_surface–Shade or write the pixels into a core
Surface. -
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to–Apply this operation to a canvas, placement or surface.
shader
class-attribute
instance-attribute
¶
Optional function (x, y) -> color evaluated across the target's pixel grid.
pixels
class-attribute
instance-attribute
¶
pixels: Iterable[PixelEntry] | None = None
Optional explicit iterable of (x, y, (red, green, blue)) pixels.
encoding
class-attribute
instance-attribute
¶
encoding: PixelEncoding = 'half_blocks'
'half_blocks' or 'braille'.
clip
class-attribute
instance-attribute
¶
clip: bool = True
Whether out-of-bounds pixels are clipped instead of raising IndexError.
x
class-attribute
instance-attribute
¶
x: int | None = None
Column offset of the explicit pixels, or None.
y
class-attribute
instance-attribute
¶
y: int | None = None
Row offset of the explicit pixels, or None.
parameters
property
¶
The parameters by name, with defaults filled in.
apply_to_surface
¶
apply_to_surface(surface: Surface) -> None
Shade or write the pixels into a core Surface.
Parameters:
-
surface(Surface) –The buffer or region to modify.
Source code in xnano/operations.py
def apply_to_surface(self, surface: _core.Surface) -> None:
"""
Shade or write the pixels into a core [`Surface`][xnano_core.Surface].
Args:
surface: The buffer or region to modify.
"""
if self.pixels is not None:
surface.update(
_core.Pixels(
self.pixels,
encoding=self.encoding,
clip=self.clip,
x=self.x,
y=self.y,
)
)
if self.shader is not None:
view = surface.pixels(encoding=self.encoding)
colored: list[tuple[int, int, RGB]] = []
for row in range(view.height):
for column in range(view.width):
color = self.shader(column, row)
if color is not None:
colored.append((column, row, to_rgb(color)))
view.write(colored)
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it again, so the operation is a live handle on what it changed. A running effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the operation to.
Source code in xnano/_internal/_dynamic_operations.py
def apply_to(
self,
target: Canvas[object, object] | Placement[object, object] | _core.Surface,
) -> None:
"""
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so the operation is a live handle on what it changed. A running effect is
stopped and started again with the new values.
Args:
target: The target to apply the operation to.
"""
from xnano.canvas import Canvas
from xnano.layout._placement import Placement
self._attach(target)
if isinstance(target, (Placement, Canvas)):
target._buffer.update(self)
if self.customization_kind.startswith('effect.'):
target._canvas._record_effect(cast('EffectParameters', self), target)
elif isinstance(target, _core.Surface):
target.update(self)
DynamicRaster
dataclass
¶
DynamicRaster(rgba: bytes = b'', width: int = 0, height: int = 0, x: int = 0, y: int = 0, encoding: PixelEncoding = 'half_blocks', mode: RasterMode = 'over')
Bases: DynamicOperation
An operation that draws a row-major RGBA8 buffer as pixels.
Its settings are plain values: the bytes, the size of the image, its position, the encoding and the mode. They are read every time the operation is applied, and assigning one on an applied operation applies the operation again with the new value.
Attributes:
-
rgba(bytes) –width * height * 4bytes of RGBA8 pixel data. -
width(int) –Image width in pixels.
-
height(int) –Image height in pixels.
-
x(int) –Top-left pixel column.
-
y(int) –Top-left pixel row.
-
encoding(PixelEncoding) –'half_blocks'or'braille'. -
mode(RasterMode) –'over'or'replace'.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to_surface–Apply this customization to a buffer, widget or region.
-
apply_to–Apply this operation to a canvas, placement or surface.
rgba
class-attribute
instance-attribute
¶
rgba: bytes = b''
width * height * 4 bytes of RGBA8 pixel data.
encoding
class-attribute
instance-attribute
¶
encoding: PixelEncoding = 'half_blocks'
'half_blocks' or 'braille'.
parameters
property
¶
The parameters by name, with defaults filled in.
evaluate
¶
evaluate(context: EvaluationContext) -> ValueType_co
Compute the expression's value.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
"""
Compute the expression's value.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
```
"""
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def when(self, condition: Predicate) -> Operation:
"""
An operation that applies only while ``condition`` is true.
Examples:
```python
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
```
"""
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it again, so the operation is a live handle on what it changed. A running effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the operation to.
Source code in xnano/_internal/_dynamic_operations.py
def apply_to(
self,
target: Canvas[object, object] | Placement[object, object] | _core.Surface,
) -> None:
"""
Apply this operation to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so the operation is a live handle on what it changed. A running effect is
stopped and started again with the new values.
Args:
target: The target to apply the operation to.
"""
from xnano.canvas import Canvas
from xnano.layout._placement import Placement
self._attach(target)
if isinstance(target, (Placement, Canvas)):
target._buffer.update(self)
if self.customization_kind.startswith('effect.'):
target._canvas._record_effect(cast('EffectParameters', self), target)
elif isinstance(target, _core.Surface):
target.update(self)
__getattr__
¶
Import an object from xnano_core the first time it is read, then keep it.
Source code in xnano/operations.py
def __getattr__(name: str) -> object:
"""Import an object from ``xnano_core`` the first time it is read, then keep it."""
core_name = _CORE_NAMES.get(name)
if core_name is None:
raise AttributeError(
f'xnano.operations has no name {name!r}. Its names are {", ".join(__all__)}.'
)
value = getattr(_core, core_name)
globals()[name] = value
return value