Basic functions

This module provides a few basic functions for interaction with the outside. These should be enough for anyone who just wants to use this library without extending it.

basic

dndice.basic(expr: Union[str, int, float, dndice.lib.evaltree.EvalTree], mode: dndice.core.Mode = <Mode.NORMAL: 0>, modifiers=0) → Union[int, float]

Roll an expression and return just the end result.

Parameters:
  • expr – The rollable string or precompiled expression tree.
  • mode – Roll this as an average, a critical hit, or to find the maximum value.
  • modifiers – A number that can be added on to the expression at the very end.
Returns:

The final number that is calculated.

verbose

dndice.verbose(expr: Union[str, int, float, dndice.lib.evaltree.EvalTree], mode: dndice.core.Mode = <Mode.NORMAL: 0>, modifiers=0) → str

Create a string that shows the actual values rolled alongside the final value.

Parameters:
  • expr – The rollable string or precompiled expression tree.
  • mode – Roll this as an average, a critical hit, or to find the maximum value.
  • modifiers – A number that can be added on to the expression at the very end.
Returns:

A string showing the expression with rolls evaluated alongside the final result.

compile

dndice.compile(expr: Union[str, int, float], modifiers=0) → dndice.lib.evaltree.EvalTree

Parse an expression into an evaluation tree to save time at later executions.

You want to use this when the particular expression is going to be used many times. For instance, for D&D, d20 rolls, possibly with advantage or disadvantage, are used all over. Precompiling those and referencing the compiled versions is therefore very likely to be worth the extra step.

Parameters:
  • expr – The rollable string.
  • modifiers – A number that can be added on to the expression at the very end.
Returns:

An evaluation tree that can be passed to one of the roll functions or be manipulated on its own.

Mode

class dndice.Mode

Change the way that a roll is performed.

Average makes each die give back the average value, which ends up with halves for the normal even-sided dice. Critical causes a roll to be like the damage roll of a critical hit, which means roll each die twice as many times. Max makes each die give its maximum value. This isn’t really used as far as I can tell.

Higher modes overwrite lower, so MAX supersedes CRIT which supersedes AVERAGE.