Skip to content

Instantly share code, notes, and snippets.

@doctaphred
Created February 5, 2020 03:30
Show Gist options
  • Save doctaphred/8ea3e63beb0473c8393d811d9b1946f7 to your computer and use it in GitHub Desktop.
Save doctaphred/8ea3e63beb0473c8393d811d9b1946f7 to your computer and use it in GitHub Desktop.
Content Addressable Python! (work in progress)
import dill
x = 1
def print_globals(max_value_len=79):
"""
>>> print_globals(40) # doctest: +ELLIPSIS
globals: {
...
'__doc__': ...
'__package__': ...
'__loader__': <_frozen_importlib_e…ject at 0x...>
'__spec__': ModuleSpec(name='...')
'__file__': '...'
'__cached__': '...'
'__builtins__': {'__name__': 'builti… object., '_': None}
...
'print_globals': <function print_globals at 0x...>
...
}
"""
print('globals: {')
for k, v in globals().items():
r = repr(v)
if len(r) > max_value_len:
half = int(max_value_len / 2)
r = r[:half] + '…' + r[-half:]
print(f" {k!r}: {r}")
print('}')
def stuff():
global x
x += 1
print(f"x is now {x}")
if __name__ == '__main__':
import os
from hashlib import sha256
# with open(__file__) as f:
# code = f.read()
# bytecode = compile(code, '__main__', 'exec')
# bytecode_hash = sha256(bytecode.co_code).hexdigest()
with open(__file__, 'rb') as f:
code = f.read()
code_hash = sha256(code).hexdigest()
import sys
_, *args = sys.argv
try:
start_from, = args
except ValueError:
start_from = None
if start_from is None:
print(f"Starting fresh session for {code_hash}")
try:
os.makedirs(f'sessions/{code_hash}')
except FileExistsError:
pass
else:
with open(f'sessions/{code_hash}/code.py', 'wb') as f:
f.write(code)
else:
print(f"Loading session {start_from!r}")
dill.load_session(f'sessions/{code_hash}/{start_from}.pkl')
print_globals()
print(f"Doing stuff")
stuff()
print(f"Done doing stuff")
print_globals()
temp_path = f'sessions/{code_hash}/tmp.pkl'
dill.dump_session(temp_path)
with open(temp_path, 'rb') as f:
state_hash = sha256(f.read()).hexdigest()
new_path = f'sessions/{code_hash}/{state_hash}.pkl'
os.rename(temp_path, new_path)
prev_path = f'sessions/{code_hash}/prev.pkl'
try:
os.remove(prev_path)
except FileNotFoundError:
pass
os.link(new_path, prev_path)
print(f"Saved state {state_hash}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment