Skip to content

Commit 5d0bc2e

Browse files
committed
Use pyproject.toml for metadata, update README
1 parent c90d3a4 commit 5d0bc2e

6 files changed

Lines changed: 209 additions & 241 deletions

File tree

.envrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

LICENCE renamed to LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2013 Sam Clements
3+
Copyright (c) 2013-2023 Sam Clements, 2017 Caleb Johnson
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# dice
2+
3+
A Python library and command line tool for parsing and evaluating dice notation.
4+
5+
_I consider this library "finished", and don't expect to add any more features to it. Bug and security fixes may still be released, especially if you find any bugs after all this time. If you're interested in other libraries in this space, [dyce] has [a great comparison table][comparison-to-alternatives] of Python dice rolling libraries._
6+
7+
[dyce]: https://posita.github.io/dyce/latest/
8+
[comparison-to-alternatives]: https://posita.github.io/dyce/0.6/#comparison-to-alternatives
9+
10+
## Quickstart
11+
12+
### Command-line
13+
14+
```shell
15+
$ roll 3d6
16+
```
17+
18+
The command line arguments are as follows:
19+
20+
* `-m` `--min` Make all rolls the lowest possible result
21+
* `-M` `--max` Make all rolls the highest possible result
22+
* `-h` `--help` Show this help text
23+
* `-v` `--verbose` Show additional output
24+
* `-V` `--version` Show the package version
25+
26+
If your expression begins with a dash (`-`), then put a double dash (`--`)
27+
before it to prevent docopt from trying to process it as a command option.
28+
Example: `roll -- -10d6`. Alternatively, use parenthesis: `roll (-10d6)`.
29+
30+
### Python API
31+
32+
Invoking from python:
33+
34+
```python
35+
import dice
36+
dice.roll('3d6')
37+
```
38+
39+
This returns an `Element` which is the result of the roll, which can be a
40+
`list`, `int`, or subclass thereof, depending on the top-level operator.
41+
42+
## Usage
43+
44+
### Notation
45+
46+
The expression works like a simple equation parser with some extra operators.
47+
48+
*The following operators are listed in order of precedence. Parentheses may
49+
be used to force an alternate order of evaluation.*
50+
51+
The dice (`[N]dS`) operator takes an amount (N) and a number of sides (S), and
52+
returns a list of N random numbers between 1 and S. For example: `4d6` may
53+
return `[6, 3, 2, 4]`. Using a `%` as the second operand is shorthand for
54+
rolling a d100, and a using `f` is shorthand for ±1 fudge dice.
55+
56+
The fudge dice (`[N]uS`) operator is interchangeable with the dice operator,
57+
but makes the dice range from -S to S instead of 1 to S. This includes 0.
58+
59+
A wild dice (`[N]wS`) roll is special. The last roll in this set is called the
60+
"wild die". If this die's roll is the maximum value, the second-highest roll
61+
in the set is set to the maximum value. If its roll is the minimum, then
62+
both it and the highest roll in the set aer set to zero. Then another die is
63+
rolled. If this roll is the minimum value again, then ALL die are set to zero.
64+
If a single-sided wild die is rolled, the roll behaves like a normal one.
65+
66+
If N is not specified, it is assumed you want to roll a single die.
67+
`d6` is equivalent to `1d6`.
68+
69+
Rolls can be exploded with the `x` operator, which adds additional dice
70+
to the set for each roll above a given threshold. If a threshold isn't given,
71+
it defaults to the maximum possible roll. If the extra dice exceed this
72+
threshold, they "explode" again! Safeguards are in place to prevent this from
73+
crashing the parser with infinite explosions.
74+
75+
You can make the parser re-roll dice below a certain threshold with the `r`
76+
and `rr` operators. The single `r` variety allows the new roll to be below
77+
the threshold, whereas the double variety's roll *changes* the roll range to
78+
have a minimum of the threshold. The threshold defaults to the minimum roll.
79+
80+
The highest, middle or lowest rolls or list entries can be selected with
81+
(`^` or `h`), (`m` or `o`), or (`v` or `l`) respectively.
82+
`6d6^3` will keep the highest 3 rolls, whereas `6d6v3` will select
83+
the lowest 3 rolls. If a number isn't specified, it defaults to keeping all
84+
but one for highest and lowest, and all but two for the middle. If a negative
85+
value is given as the operand for any of these operators, this operation will
86+
drop that many elements from the result. For example, `6d6^-2` will drop the
87+
two lowest values from the set, leaving the 4 highest. Zero has no effect.
88+
89+
A variant of the "explode" operator is the `a` ("again") operator. Instead of
90+
re-rolling values equal to or greater than the threshold (or max value), this
91+
operator doubles values *equal* to the provided threshold (or max value). When
92+
no right-side operand is specified, the left side must be a dice expression.
93+
94+
There are two operators for taking a set of rolls or numbers and counting the
95+
number of elements at or above a certain threshold, or "successes". Both
96+
require a right-hand operand for the threshold. The first, `e`, only counts
97+
successes. The second, `f`, counts successes minus failures, which are when
98+
a roll is the minimum possible value for the die element, or 1 for lists.
99+
100+
A list or set of rolls can be turned into an integer with the total (`t`)
101+
operator. `6d1t` will return `6` instead of `[1, 1, 1, 1, 1, 1]`.
102+
Applying integer operations to a list of rolls will total them automatically.
103+
104+
A set of dice rolls can be sorted with the sort (`s`) operator. `4d6s`
105+
will not change the return value, but the dice will be sorted from lowest to
106+
highest.
107+
108+
The `+-` operator is a special prefix for sets of rolls and lists. It
109+
negates odd roles within a list. Example: `[1, 2, 3]` -> `[-1, 2, -3]`.
110+
There is also a negate (`-`) operator, which works on either single
111+
elements, sets or rolls, or lists. There is also an identity `+` operator.
112+
113+
Values can be added or subtracted from each element of a list or set of rolls
114+
with the point-wise add (`.+`) and subtract (`.-`) operators. For example:
115+
`4d1 .+ 3` will return `[4, 4, 4, 4]`.
116+
117+
Basic integer operations are also available: `(16 / 8 * 4 - 2 + 1) % 4 -> 3`.
118+
119+
120+
Finally, there are two operators for building and extending lists. To build a
121+
list, use a comma to separate elements. If any comma-seperated item isn't a
122+
scalar (e.g. a roll), it is flattened into one by taking its total. The
123+
"extend" operator (`|`) is used to merge two lists into one, or append single
124+
elements to the beginning or end of a list.
125+
126+
### Python API
127+
128+
The calls to `dice.roll()` above may be replaced with `dice.roll_min()` or
129+
`dice.roll_max()` to force ALL rolls to their highest or lowest values
130+
respectively. This might be useful to see what the minimum and maximum
131+
possible values for a given expression are. Beware that this causes wild dice
132+
rolls to act like normal ones, and rolls performed as explosions are not
133+
forced high or low.
134+
135+
The `roll()` function and variants take a boolean `raw` parameter which
136+
makes the library return the element instead of the result. Note that the
137+
`evaluate_cached` method is called as part of `roll()`, which populates
138+
`element.result`. Calling `element.evaluate()` will not reset this value.
139+
140+
To display a verbose breakdown of the element tree, the
141+
`dice.utilities.verbose_print(element)` function is available.
142+
If `element.result` has not yet been populated, the function calls
143+
`evaluate_cached()` first. Keep this in mind if you want to print the result
144+
of an evaluation with custom arguments. `verbose_print()` returns a `str`.
145+
146+
Most evaluation errors will raise `DiceError` or `DiceFatalError`, both of
147+
which are subclasses of `DiceBaseError`. These exceptions have a method
148+
named `pretty_print`, which will output a string indicating where the error
149+
happened::
150+
151+
```python-repl
152+
>>> try:
153+
... dice.roll('1/0')
154+
... except dice.DiceBaseException as e:
155+
... print(e.pretty_print())
156+
...
157+
1/0
158+
^ Division by zero
159+
>>>
160+
```

README.rst

Lines changed: 0 additions & 192 deletions
This file was deleted.

0 commit comments

Comments
 (0)