Last active
October 16, 2017 08:30
Star
You must be signed in to star a gist
Proof of concept of a decorator for logged database objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from functools import wraps | |
import json | |
# Pretend this was logging to disk | |
log = [] | |
def mutator(fn): | |
@wraps(fn) | |
def logger(self, *args, **kwargs): | |
self.log(fn.__name__, args, kwargs) | |
logger.replay = lambda s, a, k: fn(s, *a, **k) | |
return logger | |
class DB: | |
def log(self, name, args, kwargs): | |
entry = json.dumps({'name': name, 'args': args, 'kwargs': kwargs}) | |
log.append(entry) | |
def replay(self, x): | |
entry = json.loads(x) | |
getattr(self.__class__, entry['name']).replay(self, entry['args'], entry['kwargs']) | |
class FooDB (DB): | |
def __init__(self): | |
self.state = dict() | |
@mutator | |
def blah(self, a, b): | |
"Blah blah blah" | |
self.state[a] = b | |
if __name__ == '__main__': | |
f = FooDB() | |
f.blah(10, 20); | |
f.blah(30, 40) | |
f.blah('abc', 'def') | |
print('-- LOG --') | |
for e in log: print(e) | |
print('---------') | |
for e in log: f.replay(e) | |
print(f.state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment