python API
If you want to call ttrpg-dice from within python, you can ...
ttrpg_dice
Various functions for TTRPG Gamesmasters to help with dice rolls.
LazyRollTable
LazyRollTable(maxdice: int, dicetype: int, target: int)
Table of values for lazyrolls of varying numbers of goblins.
Source code in ttrpg_dice/manydice.py
65 66 67 68 69 70 71 72 73 | |
rolls
instance-attribute
rolls = [
lazyroll(i, dicetype, target) for i in _maxdicerange
]
List of lists of resulting lazyrolls - (0-indexed, so includes 0 dice and 0 hits)
__eq__
__eq__(value: object) -> bool
Compare self.rolls if value is not another LazyRollTable.
Source code in ttrpg_dice/manydice.py
75 76 77 78 79 | |
__str__
__str__() -> str
Format as a nice table ignoring zero dice and zero hits.
Source code in ttrpg_dice/manydice.py
84 85 86 87 88 89 90 91 92 93 94 95 96 | |
PoolComparison
Comparison of related dicepools.
Source code in ttrpg_dice/manydice.py
102 103 104 105 106 107 108 109 110 111 112 113 114 | |
chances
instance-attribute
chances = {
(pool, outcome): sum(die[index])
for (pool, die) in items()
for (outcome, index) in items()
}
Dict of chances indexed by (pool, outcome)
__str__
__str__() -> str
Nicely formatted table.
Source code in ttrpg_dice/manydice.py
116 117 118 119 120 | |
plot
plot() -> tuple[Figure, Axes3D]
Plot as a 3d Bar with matplotlib and return the Axes.
Source code in ttrpg_dice/manydice.py
122 123 124 125 126 127 128 | |
plotable
plotable() -> dict[str, list]
Return bar location and sizes suitable for passing directly to matplotlib bar3d().
Source code in ttrpg_dice/manydice.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
StatBlock
StatBlock(*_args, **_kwargs)
A TTRPG StatBlock, acts as a mapping of stats.
Source code in ttrpg_dice/statblock.py
24 25 26 27 28 | |
__add__
__add__(other: Self) -> Self
Adds each stat, raises AttributeError if stat missing in other.
Source code in ttrpg_dice/statblock.py
49 50 51 52 53 54 | |
__getitem__
__getitem__(stat: str) -> int | Dice
Get a specific stat by subscripting.
Source code in ttrpg_dice/statblock.py
72 73 74 75 76 77 | |
__iter__
__iter__() -> Iterator
Iterate over stats.
Source code in ttrpg_dice/statblock.py
83 84 85 | |
__len__
__len__() -> int
Number of stats.
Source code in ttrpg_dice/statblock.py
79 80 81 | |
__or__
__or__(other: Self) -> Self
Merge stats, keeping the highest.
Source code in ttrpg_dice/statblock.py
67 68 69 70 | |
__repr__
__repr__() -> str
Statblock type as per str plus the stats and their challenge rolls.
Source code in ttrpg_dice/statblock.py
94 95 96 97 98 99 100 101 | |
__str__
__str__() -> str
A description of the Statblock type e.g. 'Human Warhammer StatBlock'.
Source code in ttrpg_dice/statblock.py
87 88 89 90 91 92 | |
__sub__
__sub__(other: Self) -> Self
Subtracts each stat, raises AttributeError if stat missing in other.
For example, if you want to take an NPC and remove a specific career.
Source code in ttrpg_dice/statblock.py
56 57 58 59 60 61 62 63 64 65 | |
as_table
as_table() -> str
Render the StatBlock as a github markdown table.
Source code in ttrpg_dice/statblock.py
103 104 105 | |
d
d(faces: int)
A Dice class.
Source code in ttrpg_dice/dice.py
78 79 80 | |
weighted
property
weighted: bool
Is this Dice weighted, or are all results equally likely?
__add__
__add__(other: Self | SupportsInt) -> Self
Adding two Dice to gives the combined roll.
Source code in ttrpg_dice/dice.py
211 212 213 214 215 216 217 218 219 220 221 222 | |
__eq__
__eq__(value: object) -> bool
Dice are equal if they give the same probabilities, even with different contents.
Source code in ttrpg_dice/dice.py
167 168 169 170 171 172 | |
__getitem__
__getitem__(
index: int | slice,
) -> float | list[float] | None
Get the probability of a specific result, or a list of probabilities in the case of a slice.
This handles the fact that dice faces are numbered from 1, not 0. Slicing with a step > 1 will return the probabilities of results beginning with the given step, not beginning with 1.
Example
>>> from ttrpg_dice import d
>>> dice = 2 * d(2)
>>> list(dice)
[0.0, 0.25, 0.5, 0.25]
>>> dice[:]
[0.0, 0.25, 0.5, 0.25]
>>> dice[1]
0.0
>>> dice[::2] # evens
[0.25, 0.25]
>>> dice[1::2] # odds
[0.0, 0.5]
Source code in ttrpg_dice/dice.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
__hash__
__hash__() -> int
Use contents for hashing - but NOT equality.
Source code in ttrpg_dice/dice.py
174 175 176 | |
__iter__
__iter__() -> Iterator
Iterating over a Dice yields the probabilities starting with P(1).
Source code in ttrpg_dice/dice.py
111 112 113 | |
__len__
__len__() -> int
Number of faces.
Source code in ttrpg_dice/dice.py
178 179 180 | |
__repr__
__repr__() -> str
Classname: ndX (contents).
Source code in ttrpg_dice/dice.py
197 198 199 200 | |
__rmul__
__rmul__(other: SupportsInt) -> Self
2 * Dice(4) returns a Dice with probabilities for 2d4.
Source code in ttrpg_dice/dice.py
206 207 208 209 | |
__str__
__str__() -> str
The type of Dice in NdX notation.
Source code in ttrpg_dice/dice.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
from_contents
classmethod
from_contents(contents: dict) -> Self
Create a new die from a dict of contents.
Source code in ttrpg_dice/dice.py
224 225 226 227 228 229 | |
from_str
classmethod
from_str(description: str) -> Self
Create a new die from ndX notation.
Source code in ttrpg_dice/dice.py
231 232 233 234 235 236 237 238 239 240 | |
lazyroll
lazyroll(
numdice: int, dicetype: int, target: int
) -> list[int]
Calculate equivalent single roll instead of rolling multiple dice targetting the same success value.
| PARAMETER | DESCRIPTION |
|---|---|
numdice
|
the number of identical dice to roll
TYPE:
|
dicetype
|
the number of faces on the dice to roll
TYPE:
|
target
|
the target for a successful test
TYPE:
|
Examples:
Instead of rolling 4d100 to see which of your (skilled) goblins hit your party with their arrows you can use:
>>> from ttrpg_dice import lazyroll
>>> lazyroll (4, 100, 33)
[100, 80, 40, 11, 1]
81-100: 0 hits
41-80: 1 hit
12-40: 2 hits
2-11: 3 hits
1: 4 hits
Source code in ttrpg_dice/manydice.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |