10
0
mirror of https://gitlab.com/scemama/irpf90.git synced 2024-11-19 12:32:22 +01:00
irpf90/src/util.py

102 lines
2.2 KiB
Python
Raw Normal View History

2009-09-02 20:45:53 +02:00
#!/usr/bin/python
2009-09-04 15:11:42 +02:00
def strip(x):
return x.strip()
2009-09-05 16:05:00 +02:00
def lower(x):
return x.lower()
2009-09-02 20:45:53 +02:00
def same_file(filename,txt):
assert isinstance(filename,str)
assert isinstance(txt,list)
try:
file = open(filename,"r")
except IOError:
return False
lines = file.readlines()
file.close()
if len(lines) != len(txt):
return False
for a,b in zip(lines,txt):
if a != b:
return False
return True
2009-09-04 15:11:42 +02:00
def build_dim(dim):
if len(dim) == 0:
return ""
else:
return "(%s)"%( ",".join(dim) )
2009-09-04 18:37:10 +02:00
def find_subname(line):
buffer = line.text.split('(')
if len(buffer) > 1:
buffer = " ".join(buffer[:-1])
else:
buffer = buffer[0]
buffer = buffer.lower().split()
if len(buffer) < 2:
error.fail(line,"Syntax Error")
return buffer[-1]
2009-09-04 23:48:49 +02:00
def make_single(l):
d = {}
for x in l:
d[x] = True
return d.keys()
2009-09-05 15:37:23 +02:00
def flatten(l):
if isinstance(l,list):
result = []
for i in range(len(l)):
elem = l[i]
result += flatten(elem)
return result
else:
return [l]
2009-09-04 18:37:10 +02:00
2009-09-08 16:00:46 +02:00
def dimsize(x):
assert isinstance(x,str)
buffer = x.split(':')
if len(buffer) == 1:
return x
buffer = map(strip,buffer)
else:
assert len(buffer) == 2
size = ""
b0, b1 = buffer
if b0.replace('-','').isdigit() and b1.replace('-','').isdigit():
size = str( int(b1) - int(b0) + 1 )
else:
if b0.replace('-','').isdigit():
size = "%s - (%d)"%(b1,int(b0)-1)
elif b1.replace('-','').isdigit():
size = "%d - %s"%(int(b1)+1,b0)
else:
size = "%s - %s + 1"%(b1,b0)
return size
2009-09-09 00:46:42 +02:00
def put_info(text,filename):
assert isinstance(text,list)
if len(text) > 0:
assert isinstance(text[0],tuple)
from irpf90_t import Line
assert isinstance(text[0][0],list)
assert isinstance(text[0][1],Line)
lenmax = 80 - len(filename)
format = "%"+str(lenmax)+"s ! %s:%4s"
for vars,line in text:
line.text = format%(line.text.ljust(lenmax),line.filename,str(line.i))
return text
2009-09-02 20:45:53 +02:00
if __name__ == '__main__':
2009-09-08 16:00:46 +02:00
print "10",dimsize("10") #-> "10"
print "0:10",dimsize("0:10") # -> "11"
print "0:x",dimsize("0:x") # -> "x+1"
print "-3:x",dimsize("-3:x") # -> "x+1"
print "x:y",dimsize("x:y") # -> "y-x+1"
print "x:5",dimsize("x:5") # -> "y-x+1"