Expression Tree

This page documents the structure of the Expression object as returned by roll(...).expr. If you’re looking for the Expression object returned by parse(...), check out Abstract Syntax Tree.

class d20.Number(kept=True, annotation=None)[source]

Bases: abc.ABC, d20.diceast.ChildMixin

The base class for all expression objects.

Note that Numbers implement all the methods of a ChildMixin.

kept: bool

Whether this Number was kept or dropped in the final calculation.

annotation: Optional[str]

The annotation on this Number, if any.

property children

(read-only) The children of this Number, usually used for traversing the expression tree.

Return type

list[Number]

property left

The leftmost child of this Number, usually used for traversing the expression tree.

Return type

Number

property right

The rightmost child of this Number, usually used for traversing the expression tree.

Return type

Number

set_child(index, value)[source]

Sets the ith child of this Number.

Parameters
  • index (int) – Which child to set.

  • value (Number) – The Number to set it to.

drop()[source]

Makes the value of this Number node not count towards a total.

property keptset

Returns the set representation of this object, but only including children whose values were not dropped.

Return type

list[Number]

property number

Returns the numerical value of this object.

Return type

int or float

property set

Returns the set representation of this object.

Return type

list[Number]

property total

Returns the numerical value of this object with respect to whether it’s kept. Generally, this is preferred to use over number, as this will return 0 if the number node was dropped.

Return type

int or float

class d20.Expression(roll, comment, **kwargs)[source]

Bases: d20.expression.Number

Expressions are usually the root of all Number trees.

roll: d20.Number

The roll of this expression.

comment: Optional[str]

The comment of this expression.

class d20.Literal(value, **kwargs)[source]

Bases: d20.expression.Number

A literal integer or float.

values: list[Union[int, float]]

The history of numbers that this literal has been.

exploded: bool

Whether this literal was a value in a Die object that caused it to explode.

explode()[source]

Marks that this literal was a value in a Die object that caused it to explode.

update(value)[source]

Changes the literal value this literal represents.

Parameters

value (Union[int, float]) – The new value.

class d20.UnOp(op, value, **kwargs)[source]

Bases: d20.expression.Number

Represents a unary operation.

op: str

The unary operation.

value: d20.Number

The subtree that the operation operates on.

class d20.BinOp(left, op, right, **kwargs)[source]

Bases: d20.expression.Number

Represents a binary operation.

op: str

The binary operation.

left: d20.Number

The left subtree that the operation operates on.

right: d20.Number

The right subtree that the operation operates on.

class d20.Parenthetical(value, operations=None, **kwargs)[source]

Bases: d20.expression.Number

Represents a value inside parentheses.

value: d20.Number

The subtree inside the parentheses.

operations: list[d20.SetOperation]

If the value inside the parentheses is a Set, the operations to run on it.

class d20.Set(values, operations=None, **kwargs)[source]

Bases: d20.expression.Number

Represents a set of values.

values: list[d20.Number]

The elements of the set.

operations: list[d20.SetOperation]

The operations to run on the set.

class d20.Dice(num, size, values, operations=None, context=None, **kwargs)[source]

Bases: d20.expression.Set

A set of Die.

num: int

How many Die in this set of dice.

size: int

The size of each Die in this set of dice.

values: list[d20.Die]

The elements of the set.

operations: list[d20.SetOperation]

The operations to run on the set.

roll_another()[source]

Rolls another Die of the appropriate size and adds it to this set.

class d20.Die(size, values, context=None)[source]

Bases: d20.expression.Number

Represents a single die.

size: int

How many sides this Die has.

values: list[d20.Literal]

The history of values this die has rolled.

class d20.SetOperator(op, sels)[source]

Represents an operation on a set.

op: str

The operation to run on the selected elements of the set.

sels: list[d20.SetSelector]

The selectors that describe how to select operands.

select(target, max_targets=None)[source]

Selects the operands in a target set.

Parameters
  • target (Number) – The source of the operands.

  • max_targets (Optional[int]) – The maximum number of targets to select.

operate(target)[source]

Operates in place on the values in a target set.

Parameters

target (Number) – The source of the operands.

class d20.SetSelector(cat, num)[source]

Represents a selection on a set.

cat: Optional[str]

The type of selection (lowest, highest, literal, etc).

num: int

The number to select (the N in lowest/highest/etc N, or literally N).

select(target, max_targets=None)[source]

Selects operands from a target set.

Parameters
  • target (Number) – The source of the operands.

  • max_targets (int) – The maximum number of targets to select.

Returns

The targets in the set.

Return type

set of Number