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.

Usage

Any ValueErrors when using evaluate() or format_expression() should be caught in the parent program.

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.6.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.6-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for expr_dice_roller-0.0.6.tar.gz
Algorithm Hash digest
SHA256 2f4c216de617e57a20b199cdc19e616c5e1399483448bfccdc7bc21b5621d53b
MD5 8d1d1244978113963a827429a2e39fe1
BLAKE2b-256 4742a782b1f3a0794ea8c9fe07f3d7078863d96efe9ea87541868fe47a85e95f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for expr_dice_roller-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 58d21e8166212c26dfa11dcf7af89b5bc386ebe8f87dafe1dd3444893ea3a1b1
MD5 19b4207d79cad9ed3db1c87f19e55432
BLAKE2b-256 9acfc4bd85074eee88a39c0dce38f30d3178a440653c4c0c868b7c74370fcd86

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