more python 2/3 porting for using compile()/eval()/exec()

This commit is contained in:
Axel Kohlmeyer
2022-08-20 11:06:02 -04:00
parent bd5dea0d07
commit d3101898f1

View File

@ -724,7 +724,7 @@ class dump:
column = self.names[name] column = self.names[name]
insert = "snap.atoms[i][%d]" % (column) insert = "snap.atoms[i][%d]" % (column)
eq = eq.replace(item,insert) eq = eq.replace(item,insert)
ceq = compile(eq,'','single') ceq = compile(eq,'<string>','single')
for snap in self.snaps: for snap in self.snaps:
if not snap.tselect: continue if not snap.tselect: continue
@ -1157,11 +1157,11 @@ class tselect:
def test(self,teststr): def test(self,teststr):
data = self.data data = self.data
snaps = data.snaps snaps = data.snaps
cmd = "flag = " + teststr.replace("$t","snaps[i].time") cmd = teststr.replace("$t","snaps[i].time")
ccmd = compile(cmd,'','single') ccmd = compile(cmd,'<string>','eval')
for i in range(data.nsnaps): for i in range(data.nsnaps):
if not snaps[i].tselect: continue if not snaps[i].tselect: continue
exec(ccmd) flag = eval(ccmd)
if not flag: if not flag:
snaps[i].tselect = 0 snaps[i].tselect = 0
data.nselect -= 1 data.nselect -= 1
@ -1199,21 +1199,20 @@ class aselect:
# replace all $var with snap.atoms references and compile test string # replace all $var with snap.atoms references and compile test string
pattern = "\$\w*" pattern = "\$\w*"
list = re.findall(pattern,teststr) matches = re.findall(pattern,teststr)
for item in list: for item in matches:
name = item[1:] name = item[1:]
column = data.names[name] column = data.names[name]
insert = "snap.atoms[i][%d]" % column insert = "snap.atoms[i][%d]" % column
teststr = teststr.replace(item,insert) teststr = teststr.replace(item,insert)
cmd = "flag = " + teststr ccmd = compile(teststr,'<string>','eval')
ccmd = compile(cmd,'','single')
if len(args) == 0: # all selected timesteps if len(args) == 0: # all selected timesteps
for snap in data.snaps: for snap in data.snaps:
if not snap.tselect: continue if not snap.tselect: continue
for i in range(snap.natoms): for i in range(snap.natoms):
if not snap.aselect[i]: continue if not snap.aselect[i]: continue
exec(ccmd) flag = eval(ccmd)
if not flag: if not flag:
snap.aselect[i] = 0 snap.aselect[i] = 0
snap.nselect -= 1 snap.nselect -= 1