Persistent results
Persistent results is a Python class that ensures the results will be available even if interruptions occur.
Installation:
pip install pers
Usage:
Example 1
If the code below breaks for some reason (let's say the computer shuts down because of a power outage or some exception), you can restart it to continue from the step it finished.
from pers import PersistentResults
import pandas as pd # pandas is not required
results = PersistentResults(
'test.pickle', # filename for result caching
interval=1, # how often dump the results
tmpfilename='~test.pickle' # tmp cache file (optional)
)
fun = lambda x, y, a, b: x**2 + y
for x in range(10):
for y in range(11):
results.append(fun, x, y, a=x, b=y)
results.save()
print(pd.DataFrame(results.data))
Output:
{'result': 66, 'x': 8, 'y': 2, 'a': 8, 'b': 2}
110
| | result | x | y | a | b |
|----:|---------:|----:|----:|----:|----:|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 | 0 | 1 |
| 2 | 2 | 0 | 2 | 0 | 2 |
[..]
| 107 | 89 | 9 | 8 | 9 | 8 |
| 108 | 90 | 9 | 9 | 9 | 9 |
| 109 | 91 | 9 | 10 | 9 | 10 |
Example 2
from pers import PersistentResults
import pandas as pd
results = PersistentResults(
'test2.pickle',
interval=1,
result_prefix='' # we do not want a prefix _result_
)
def fun(x, y, a, b):
print(f'x: {x}, y:{y}')
return { # yes, we can return dictionary
'out': x**2 + y,
'x': x, # yes, we can return input in dict
'a': a,
}
try:
for x in range(10):
for y in range(11):
results.append(fun, x=x, y=y, a=x, b=y)
if x == 5 and y ==5: # simulate reboot
raise Exception('Unexpected reboot...')
except:
pass
# RERUN the tests
# will skip already processed elements
for x in range(10):
for y in range(11):
results.append(fun, x=x, y=y, a=x, b=y)
results.save()
print(pd.DataFrame(results.data).to_markdown())
x: 0, y:0
x: 0, y:1
[..]
x: 5, y:4
x: 5, y:5
Unexpected reboot...
x: 5, y:6
x: 5, y:7
[..]
x: 9, y:9
x: 9, y:10
| | out | x | a | y | b |
|----:|------:|----:|----:|----:|----:|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 | 1 | 1 |
| 2 | 2 | 0 | 0 | 2 | 2 |
[..]
| 107 | 89 | 9 | 9 | 8 | 8 |
| 108 | 90 | 9 | 9 | 9 | 9 |
| 109 | 91 | 9 | 9 | 10 | 10 |
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pers-0.2.2.tar.gz
(17.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
pers-0.2.2-py2.py3-none-any.whl
(17.0 kB
view details)
File details
Details for the file pers-0.2.2.tar.gz.
File metadata
- Download URL: pers-0.2.2.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.2 CPython/3.10.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2d295e859524227e25dee94bd199bd128a352c0d2d3fa27b2582bbcb272f367
|
|
| MD5 |
23ef433403f10887f5764684bf61e569
|
|
| BLAKE2b-256 |
9cb2706f57d751da68f4928e85d843962649577300fa10fa52cbb687b6e5a630
|
File details
Details for the file pers-0.2.2-py2.py3-none-any.whl.
File metadata
- Download URL: pers-0.2.2-py2.py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.2 CPython/3.10.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e255bb1c6143ce1586d15e96ebee36d4239d95436642582053cdf164697d4372
|
|
| MD5 |
26b99266f02ac9a89177f6312f95e84e
|
|
| BLAKE2b-256 |
40bd73fa3a4cdd4d18ec1809db2f561f5449ab1531f474c644b802a226b99966
|