Skip to main content

A dice roller expression language with function support.

Project description

Expression Dice Roller

Install from PyPI: pip install expr_dice_roller

Quick command-line dice roller set up:

from expr_dice_roller import evaluate
result = evaluate("3d20d1") # 3d20 and drop only the lowest roll.
print(result.value)

It is possible to serialize an expression by using format_expression(), which can then be used in evaluate().

It is also possible to serialize and deserialize an Environment.

Language Specifications

Arithmetic words with the operators +, -, *, /, and ^. Unary operators - and + are supported.

The language closely mirrors RPG Dice Roller's notations, with a few unimplemented or extra parts:

  • Fudge dice is not yet implemented.
  • Critical success / failures are not yet implemented.
  • Grouping rolls are not yet implemented.
  • New features includes variables and functions.

Variables

Variables have a name comprised of any string of characters that are not part of the language itself. There are no built-in variables except for the _ variable when evaluate() is called with assign_last_eval=True.

The _ variable, if provided, will point to the value of the last successful eval.

Functions

Functions are declared like so: f(x, y, z, ...) = expression. They can be immediately called if surrounded by brackets.

Example functions:

  • f(x) = 2 * x - doubles the input.
  • dice(count, sides) = (count)d(sides) - rolls count dice each with sides sides.
  • dice(4, 3) - calls the dice function, effectively rolling a 4d3.
  • (g(x, y)=f(x)-f(y))(2, 3) - immediately invoked function that calls another function.

Extraneous arguments beyond the arity of the function will be voided. Similarly, missing arguments below the arity of the function will be turned to 0.

Functions cannot be arbitrarily chained yet; currently, functions are parsed as special exceptions to certain rules.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

expr_dice_roller-0.0.4.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

expr_dice_roller-0.0.4-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file expr_dice_roller-0.0.4.tar.gz.

File metadata

  • Download URL: expr_dice_roller-0.0.4.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for expr_dice_roller-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1e76bd07a07c6e260cd251512c41248b0482e0fb5d44136834ecfe17d6cac711
MD5 56651ea0d9cfee7a4ff4f2c53064ab5c
BLAKE2b-256 8b662f55124b962035eadcdb4bad2fcb3d96d06def30ed15769b24ffd01f5e9d

See more details on using hashes here.

File details

Details for the file expr_dice_roller-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for expr_dice_roller-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 707c2317b3ca65c07a335007517857ad079d56d632d822f3a437d826f712707b
MD5 eeb78da5cb746c5ed2d55ea5d047f065
BLAKE2b-256 cebb1191729663b2a543a0eb9b4e116b1cdce9fc43a3b51bac213e9ad3f628b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page