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.3.tar.gz (11.1 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.3-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: expr_dice_roller-0.0.3.tar.gz
  • Upload date:
  • Size: 11.1 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.3.tar.gz
Algorithm Hash digest
SHA256 f93fffa04e4b2363b49514b5bbec1dc19b30259de8f046df9f51b0712a7981c0
MD5 8340d20d70449a10c5a3564829614a29
BLAKE2b-256 95e68b90ccbd12da97fcd645acfc46f82fe16728dc43481319dbf5335c47c457

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for expr_dice_roller-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cacf150148d36e275a240407c43cf0ed7517c7ffa76b939518c39cc817d2217d
MD5 06bdfe70319fb0f548ff4bf4065b96c1
BLAKE2b-256 11794c59ff2db61f67712e01317a7091ea08f9cbc2fd12e3c274355294b8206d

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