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

Uploaded Python 3

File details

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

File metadata

  • Download URL: expr_dice_roller-0.0.5.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.5.tar.gz
Algorithm Hash digest
SHA256 ff446f3a27a7a96aaf6e41c48ea8433a3458ddeb11fd9d06f1bdd9ed354bd337
MD5 b78dcf2d510945c2b94312abbb5afd9a
BLAKE2b-256 3257c56ddc8cfe640ac135ff93a3cc082eb4fc961a65570ebf8075494726e9d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for expr_dice_roller-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 837f07d28eede65c9595bb652bad682f195d45639ceaf40305e8dbb151fed9a1
MD5 56d74970a462eaa3befe9e525793091b
BLAKE2b-256 f135e33572fcdb3d025b2d1f327afe0758c7327313f9ea618e2754fa79be4616

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