-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdec.py
More file actions
executable file
·44 lines (35 loc) · 1.25 KB
/
testdec.py
File metadata and controls
executable file
·44 lines (35 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/env python
# decorator for saving arguments passed to a function
# based on the presence of an environment variable
# ZP_DUMP. includes regular and keyword args.
# saves args to a pickle file.
def save(f):
def dumper(self, *args, **kwargs):
import os
if os.environ.get('ZP_DUMP', None):
import pickle
import time
filetime = time.strftime('%H%M%S', time.localtime())
fname = '_'.join((self.__class__.__name__,f.func_name,filetime))
pkl_file = open(os.path.join('/tmp', fname + '.pickle') , 'w')
arguments = []
for count, thing in enumerate(args):
arguments.append(thing)
for name, value in kwargs.items():
arguments.append('{}={}'.format(name,value))
print 'writing {}'.format(fname)
pickle.dump(arguments, pkl_file)
pkl_file.close()
return f(self, *args, **kwargs)
return dumper
class foo(object):
#@print_everything('foo', 'bar')
@save
def bar(self, x, y, z='bar'):
print 'this is a test'
def xyz(self):
pass
x = foo()
x.bar(1, '2')
x.bar('abc', 'def', z='xyz')
x.bar([1, 2, 3], 'a')