-
Notifications
You must be signed in to change notification settings - Fork 5
/
txtxFunctions.py
85 lines (67 loc) · 1.84 KB
/
txtxFunctions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
def executeFormula(fIn, fOu, nrow, n, s):
# v=0 #init. not required; it can interfere with the try/except structure
pos = s.find("v")
if pos == -1:
print("missing 'v' in formula, row", nrow,
"\nexecution stopped in error")
fIn.close()
fOu.close()
os.sys.exit(1)
pos = s.find("=")
if pos == -1:
print("missing '=' in formula, row", nrow,
"\nexecution stopped in error")
fIn.close()
fOu.close()
os.sys.exit(1)
try:
while s[0] == ' ':
if s[0] == ' ':
s = s[1:]
pos = s.find('\n') # eliminating spaces after \n (formerly #) if any
if pos != -1:
while s[pos + 1] == ' ':
s = s[:pos + 1] + s[pos + 2:]
# print "[",n, s,"]",
d = dict([('n', n), ('v', 0)])
exec(s, d)
v = d['v']
return str(v)
except BaseException:
print("error in formula, row", nrow, "\nexecution stopped in error")
fIn.close()
fOu.close()
os.sys.exit(1)
def fill(s):
s = list(s)
if s == "":
return s
change = False
s = list(s)
for i in range(len(s)):
if s[i] == '&':
if not change:
change = True
else:
change = False
if s[i] == ' ' and change:
s[i] = '&'
return "".join(s)
def splitUnfill(s):
if s == "":
return s
# print s
s = s.split()
# print s
for i in range(len(s)):
s_tmp = list(s[i])
# print s_tmp, len(s_tmp)
for j in range(len(s_tmp)):
if s_tmp[j] == "&":
s_tmp[j] = ' '
if s_tmp[j] == "#":
s_tmp[j] = '\n' # inserting \n sign
# print s_tmp
s[i] = "".join(s_tmp)
return s