-
Notifications
You must be signed in to change notification settings - Fork 6
/
cachefy.py
executable file
·66 lines (58 loc) · 1.73 KB
/
cachefy.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
## cachefy.py
##
## Add or remove numba "cache" decorators
##
## Copyright (C) 2018, Huan Zhang <huan@huan-zhang.com> and contributors
##
## This program is licenced under the BSD 2-Clause License,
## contained in the LICENCE file in this directory.
## See CREDITS for a list of contributors.
##
import sys
import glob
filelist = glob.glob('*.py')
def add_cache(l):
if l.find("cache=True") >= 0: # already patched
return l
if l[-1] == "t": #njit or jit
l += "(cache=True)"
elif l[-1] == ")": # some other parameters exist
if "cache" not in l:
l = l[:-1] + ",cache=True)"
return l
def remove_cache(l):
if l[-1] == ")": # some parameters exist
l = l.replace("cache=True", "")
# remove the extra ,
s = l.strip()[:-1].strip()
if s[-1] == ",":
l = s[:-1] + ")"
# remove empty ()
if l[-2:] == "()":
l = l[:-2]
return l
func = add_cache
if len(sys.argv) > 1:
if sys.argv[1] == "-u":
func = remove_cache
for pyfile in filelist:
with open(pyfile) as f:
lines = f.readlines()
print("processing", pyfile)
write = False
for i in range(len(lines)):
l = lines[i]
l = l.strip()
if l.startswith("@jit") or l.startswith("@njit"):
new_l = func(l)
if new_l != l:
print('line {:5d}: "{}" -> "{}"'.format(i+1, l, new_l))
lines[i] = new_l + "\n"
write = True
else:
print('line {:5d}: "{}" (patched)'.format(i+1, l))
if write:
print("updating", pyfile)
with open(pyfile, "w") as f:
f.writelines(lines)