python 3 compatibility for src/*.py files

move imports to top of files
add from __future__ import
commands module -> subprocesses module
print statement -> print() function
exec statement -> exec() function
xrange -> range
map() -> list(map())
StandardError -> Exception
integer division: / -> //
x.has_key(y) -> y in x
sort(): use functools.cmp_to_key
type(x) is y -> isinstance(x, y)
raw_input -> input
change variable names 'list' to avoid clashes with list class
This commit is contained in:
danielque
2023-08-10 13:19:02 +02:00
parent d560b34214
commit 597e606bde
34 changed files with 1463 additions and 1352 deletions

View File

@ -8,6 +8,13 @@
# ldump tool
# Imports and external programs
from __future__ import absolute_import
import sys, subprocess, re, glob, types
import functools
from os import popen
oneline = "Read dump files with line segment info"
docstr = """
@ -67,11 +74,6 @@ l.owrap(...) wrap lines to same image as their atoms
# xlo,xhi,ylo,yhi,zlo,zhi = box bounds (float)
# atoms[i][j] = 2d array of floats, i = 0 to natoms-1, j = 0 to ncols-1
# Imports and external programs
import sys, commands, re, glob, types
from os import popen
try:
import numpy as np
oldnumeric = False
@ -99,7 +101,7 @@ class ldump:
self.flist = []
for word in words: self.flist += glob.glob(word)
if len(self.flist) == 0 and len(list) == 1:
raise StandardError,"no ldump file specified"
raise Exception("no ldump file specified")
if len(list) == 1:
self.increment = 0
@ -124,26 +126,26 @@ class ldump:
snap = self.read_snapshot(f)
while snap:
self.snaps.append(snap)
print snap.time,
print(snap.time, end=' ')
sys.stdout.flush()
snap = self.read_snapshot(f)
f.close()
print
print()
# sort entries by timestep, cull duplicates
self.snaps.sort(self.compare_time)
self.snaps.sort(key = functools.cmp_to_key(self.compare_time))
self.cull()
self.nsnaps = len(self.snaps)
print "read %d snapshots" % self.nsnaps
print("read %d snapshots" % self.nsnaps)
# --------------------------------------------------------------------
# read next snapshot from list of files
def next(self):
if not self.increment: raise StandardError,"cannot read incrementally"
if not self.increment: raise Exception("cannot read incrementally")
# read next snapshot in current file using eof as pointer
# if fail, try next file
@ -197,14 +199,14 @@ class ldump:
if snap.natoms:
words = f.readline().split()
ncol = len(words)
for i in xrange(1,snap.natoms):
for i in range(1,snap.natoms):
words += f.readline().split()
floats = map(float,words)
floats = list(map(float,words))
if oldnumeric: atoms = np.zeros((snap.natoms,ncol),np.Float)
else: atoms = np.zeros((snap.natoms,ncol),np.float)
start = 0
stop = ncol
for i in xrange(snap.natoms):
for i in range(snap.natoms):
atoms[i] = floats[start:stop]
start = stop
stop += ncol
@ -219,7 +221,7 @@ class ldump:
def map(self,*pairs):
if len(pairs) % 2 != 0:
raise StandardError, "ldump map() requires pairs of mappings"
raise Exception("ldump map() requires pairs of mappings")
for i in range(0,len(pairs),2):
j = i + 1
self.names[pairs[j]] = pairs[i]-1
@ -249,9 +251,9 @@ class ldump:
# --------------------------------------------------------------------
def findtime(self,n):
for i in xrange(self.nsnaps):
for i in range(self.nsnaps):
if self.snaps[i].time == n: return i
raise StandardError, "no step %d exists" % n
raise Exception("no step %d exists" % n)
# --------------------------------------------------------------------
# delete successive snapshots with duplicate time stamp
@ -293,7 +295,7 @@ class ldump:
# don't add line if all 4 values are 0 since not a line
lines = []
for i in xrange(snap.natoms):
for i in range(snap.natoms):
atom = snap.atoms[i]
e1x = atom[end1x]
e1y = atom[end1y]
@ -323,7 +325,7 @@ class ldump:
# jdump = atom J in dump's atoms that atom I was owrapped on
# delx,dely = offset applied to atom I and thus to line I
for i in xrange(snap.natoms):
for i in range(snap.natoms):
tag = atoms[i][id]
idump = idsdump[tag]
jdump = idsdump[atomsdump[idump][iother]]