Ever had your code crash after 10 hours of heavy computation because of a dumb mistake?
Project description
PlsNoCrash
Setup
This package has no dependencies, you can install it with pip:
pip install plsnocrash
Let Me Try
Have you ever had some compute-intensive code run for hours only for it to crash right
at the end for some inane reason? I have, and it's usually the bit where I save the data
because I used a file path instead of a file object, or mixed up the order of the parameters
for pickle.dump
.
Instead of letting all your data go up in smoke, use the @let_me_try
decorator. It will
drop you to an interactive interpreter if the code you mark raises an exception. It will
also give you access to the variables in the offending function's scope, as well as all
the variables in the scope of every other function on the call stack. From there
you can fix whatever the issue was and resume execution as if nothing even happened.
Here's an example:
root@710027b06106:/plsnocrash/examples# cat let_me_try.py
import plsnocrash
import time
import pickle
def train():
time.sleep(10)
return [1,2,3,4,5]
@plsnocrash.let_me_try
def save(x):
# Oops, that should be a file object, not a string
pickle.dump(x, 'test.pkl')
if __name__ == '__main__':
x = train()
save(x)
print("All done!")
root@710027b06106:/plsnocrash/examples# python let_me_try.py
Caught exception: file must have a 'write' attribute
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/plsnocrash/plsnocrash.py", line 65, in wrapper
return f(*args, **kwargs)
File "let_me_try.py", line 13, in save
pickle.dump(x, 'test.pkl')
TypeError: file must have a 'write' attribute
Call to save(args=([1, 2, 3, 4, 5],), kwargs={}) failed.
Use save(arg1, ...) or resume(arg1, ...) to call the function again with the given arguments and resume execution.
If the function raises another exception, you will end up at another console.
Use skip(return_value) to skip the function call and resume execution as if it had returned 'return_value'.
Global and local variables are available for all scopes on the call stack under the list call_stack.
e.g. call_stack[0]['x'] returns the variable 'x' from save (the failing function),
and call_stack[1]['y'] returns the variable 'y' from the function that called save.
The original positional arguments are available as the tuple 'args',
and keyword arguments are available as the dictionary 'kwargs'.
Use quit() or exit() to give up and stop the whole program.
>>> import pickle
>>> pickle.dump(args[0], open('test.pkl','wb'))
>>> skip()
Call skipped
>>>
Resuming execution
All done!
root@710027b06106:/plsnocrash/examples# ls
test.pkl
Retry
Also available is the @retry(limit=n)
decorator which will rerun a function until it succeeds or
gives up after n
retries.
root@710027b06106:/plsnocrash/examples# cat retry.py
import plsnocrash
fail_counter = 0
@plsnocrash.retry(5)
def get_data():
global fail_counter
# Fail three times before completing
if fail_counter < 3:
fail_counter += 1
raise ValueError("Something went wrong")
return "some data"
if __name__ == '__main__':
print(get_data())
root@710027b06106:/plsnocrash/examples# python retry.py
Caught exception: Something went wrong, retry 1/5
Caught exception: Something went wrong, retry 2/5
Caught exception: Something went wrong, retry 3/5
some data
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
Built Distributions
File details
Details for the file plsnocrash-0.1.3.tar.gz
.
File metadata
- Download URL: plsnocrash-0.1.3.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48831a933ca47f57801ffdfb2d0440839507cb2a26bb8cba1fa64c68461a55cf |
|
MD5 | 65b0e3b5f9d6b81f02e39114b3a4360a |
|
BLAKE2b-256 | 677915526f522faeabbbb9af9243dd4d070753d12eaaeb94510e50cc709d0a2f |
File details
Details for the file plsnocrash-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: plsnocrash-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b1403d47872463762b396c5b6cdf9639f8fb17e129b8e53d0aa597e80ca7f99 |
|
MD5 | e1636073c872b4b9857c1556caa89748 |
|
BLAKE2b-256 | 54f8fb12e2d445a1f3afcbcddbfb06ede4ea76cbe20da6c9ad92f76dddd72cb1 |
File details
Details for the file plsnocrash-0.1.3-py2-none-any.whl
.
File metadata
- Download URL: plsnocrash-0.1.3-py2-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e53f13eb68786578dd88e0a200cc79a345c79cc29a0bf95f5c420a4fb2dc0d12 |
|
MD5 | 5e8b3d6b684e91e658d7c1fc971ab4b0 |
|
BLAKE2b-256 | bba6f5528de4d73e5bef14647fdf3ee17521ef823ee1a09d44c1fc88c4dec6bf |