Skip to content

Instantly share code, notes, and snippets.

@AusIV
AusIV / dice_roll.py
Last active May 7, 2021 15:25
A Python script that uses Parsley to roll dice in the usual D&D notation
import random
import parsley
def roll(x, y, k=None):
k = k or x
if k > 0:
s = slice(0, k)
else:
s = slice(k, x)
return sum(sorted((random.randrange(1, y+1) for _ in range(x)), reverse=True )[s])

Keybase proof

I hereby claim:

  • I am ausiv on github.
  • I am ausiv (https://keybase.io/ausiv) on keybase.
  • I have a public key ASAvdrYDUc3uV4RYYZrJ8Fn0h3wElrP9x44SXwIdjs9mnwo

To claim this, I am signing this object:

0x324454186BB728a3ea55750E0618ff1B18ce6Cf8

This post presents an interesting way to make error handling more brief in Golang 1.18

One of the downsides to this approach is that defer functions and catching panics can be generally more expensive than using if/return for error handling. I decided to benchmark to see how significant the penalty turns out to be.

For some boilerplate, we have a giveError function:

func giveError(b bool) (int, error) {
  if b {
    return 0, fmt.Errorf("given error")