Function Receipts

class slurmjobs.receipt.Receipt(name='', *a, receipt_id=None, __dir__=None, **kw)[source]

Make a receipt for a function call. This allows you skip over a function if it was successfully ran. This is useful if a long script fails in the middle and you want to re-run it, but you don’t need to re-run the first part.

This will cache the function execution history using the string representation of the function’s arguments. This works well as 99% of things have some sort of string representation, however, if you have a string representation that doesn’t stay consistent, like an object that prints out its ID, then the receipt won’t work.

To resolve this, you can either:

  • subclass this method and override Receipt.hash_args(*a, **kw) to return something invariant for your

  • Provide your own receipt_id to the function call.

  • submit a PR for a better hash function!

receipt = Receipt(func.__name__, *a, **kw)

if not receipt.exists():
    try:
        do_something()
        receipt.make()
        # yay we can skip it next time!
    except Exception:
        """Oh well. It failed, but we'll just try again next time."""

else:
    """Oh good it ran completely last time so we can skip it and move to the next one."""
hash_args(*a, **kw)[source]

Take the function arguments and return a hash string for them.

slurmjobs.receipt.use_receipt(func, receipt_dir=None, test=None)[source]

Use a receipt for a function call, which lets us skip a result if the function completed successfully the last run. This is just a wrapper around Receipt that handles the receipt checking/making logic for you.

# do step 1. If it already ran successfully it will skip and do nothing.
use_receipt(my_step1_function)(**step1_kwargs)

# do step 2. here we're passing a custom receipt ID
custom_receipt_id = ...
use_receipt(my_step2_function)(**step2_kwargs, receipt_id=custom_receipt_id)

# do step 3
use_receipt(my_step3_function)(**step3_kwargs)