From 7e6be9ab45937c9a6ccb55a675b3446c3b58ff3e Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Mon, 27 Jan 2020 11:32:17 +0100 Subject: [PATCH] Python3. Version:2.0.0 Version:2.0.1 --- Bash/ezfio.sh | 2 +- README.rst | 18 +++++++++--------- bin/archive_ezfio.py | 16 +++++++--------- bin/unarchive_ezfio.py | 14 +++++++------- git-tools/commit-msg | 4 ++-- git-tools/post-commit | 4 ++-- make.config | 2 +- src/ezfio-head.py | 27 ++++++++++++++------------- src/ezfio-tail.py | 6 +++--- src/groups.py | 32 ++++++++++++++++---------------- src/groups_templates.py | 4 ++-- src/libezfio_file.irp.f | 2 +- src/libezfio_util.irp.f | 18 +++++++++--------- src/read_config.py | 16 ++++++++-------- version | 2 +- 15 files changed, 83 insertions(+), 84 deletions(-) diff --git a/Bash/ezfio.sh b/Bash/ezfio.sh index bd756dc..6814cb9 100644 --- a/Bash/ezfio.sh +++ b/Bash/ezfio.sh @@ -7,7 +7,7 @@ EZFIO_ROOT=$( cd $(dirname "${BASH_SOURCE}")/.. ; pwd -P ) function _ezfio_py() { - python2 ${EZFIO_ROOT}/Python/ezfio.py "$@" + python3 ${EZFIO_ROOT}/Python/ezfio.py "$@" } diff --git a/README.rst b/README.rst index ef35abe..1f2b050 100644 --- a/README.rst +++ b/README.rst @@ -180,7 +180,7 @@ Create a file named ``create_input.py`` with: .. code-block:: python - #!/usr/bin/python2 + #!/usr/bin/python3 import sys EZFIO = "./EZFIO" # Put here the absolute path to the EZFIO directory @@ -195,17 +195,17 @@ Create a file named ``create_input.py`` with: Molecule = [] for line in input.splitlines(): - new_list = map(eval,line.split()) + new_list = list(map(eval,line.split())) Molecule.append(new_list) # Create the mass array - mass = map( lambda x: x[0], Molecule ) - # print mass + mass = [ x[0] for x in Molecule ] + # print(mass) # [16.0, 1.0, 1.0] # Create the coord array - coord = map( lambda x: (x[1], x[2], x[3]), Molecule ) - # print coord + coord = [ (x[1], x[2], x[3]) for x in Molecule ] + # print(coord) # [(0.0, 0.222396, 0.0), (1.436494, -0.88966, 0.0), (-1.436494, -0.88966, 0.0)] # Select the EZFIO file @@ -217,13 +217,13 @@ Create a file named ``create_input.py`` with: ezfio.molecule_coord = coord # Check that the total mass is correct: - print ezfio.properties_mass # Should give 18. + print(ezfio.properties_mass) # Should give 18. Execute the script: .. code-block:: bash - $ python2 create_input.py + $ python3 create_input.py 18.0 The printed mass is correct, and a new directory (``Water``) was created @@ -364,7 +364,7 @@ Here is the same script as the Python script, but using Bash #!/bin/bash - source EZFIO/Bash/ezfio.sh + source EZFIO/Bash/ezfio.sh # Put correct path here # Select the EZFIO file ezfio set_file Water diff --git a/bin/archive_ezfio.py b/bin/archive_ezfio.py index 5b8f1e3..7de66dd 100755 --- a/bin/archive_ezfio.py +++ b/bin/archive_ezfio.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import sys import os sys.path = [ os.path.dirname(__file__)+"/../Python" ]+sys.path -import cPickle as pickle +import pickle as pickle import zlib from ezfio import ezfio_obj, ezfio @@ -20,7 +20,7 @@ def main(): sys.argv.remove("-v") if len(sys.argv) == 1: - print "syntax: %s "%(sys.argv[0]) + print(("syntax: %s "%(sys.argv[0]))) sys.exit(1) ezfio_filename = sys.argv[1] while ezfio_filename[-1] == "/": @@ -28,21 +28,19 @@ def main(): ezfio.set_filename(ezfio_filename) - get_functions = filter( - lambda x: x.startswith("has_"), - ezfio_obj.__dict__.keys() ) + get_functions = [x for x in list(ezfio_obj.__dict__.keys()) if x.startswith("has_")] d = {} for f in get_functions: f_name = f[4:] try: - exec """d['%s'] = ezfio.%s"""%(f_name,f_name) + exec("""d['%s'] = ezfio.%s"""%(f_name,f_name)) except: if do_verbose: - print "%-40s [%5s]"%(f_name, "Empty") + print(("%-40s [%5s]"%(f_name, "Empty"))) else: if do_verbose: - print "%-40s [%5s]"%(f_name, " OK ") + print(("%-40s [%5s]"%(f_name, " OK "))) dump = zlib.compress(pickle.dumps(d)) file = open(ezfio_filename+".ezar","w") diff --git a/bin/unarchive_ezfio.py b/bin/unarchive_ezfio.py index 96b9077..c611f63 100755 --- a/bin/unarchive_ezfio.py +++ b/bin/unarchive_ezfio.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import sys import os sys.path = [ os.path.dirname(__file__)+"/../Python" ]+sys.path -import cPickle as pickle +import pickle as pickle import zlib from ezfio import ezfio_obj, ezfio @@ -15,7 +15,7 @@ ezfio.error = f def main(): if len(sys.argv) == 1: - print "syntax: %s "%(sys.argv[0]) + print(("syntax: %s "%(sys.argv[0]))) sys.exit(1) ezfio_filename = sys.argv[1].split(".ezar")[0] @@ -27,7 +27,7 @@ def main(): d = pickle.loads(zlib.decompress(dump)) - set_functions = d.keys() + set_functions = list(d.keys()) nerrors_old = len(d)+1 nerrors = nerrors_old+1 @@ -37,15 +37,15 @@ def main(): failed = [] for f_name in set_functions: try: - exec """ezfio.%s = d['%s']"""%(f_name,f_name) + exec("""ezfio.%s = d['%s']"""%(f_name,f_name)) except: nerrors += 1 failed.append(f_name) if nerrors != 0: - print "Unarchive failed:" + print("Unarchive failed:") for i in failed: - print i + print(i) sys.exit(1) if __name__ == "__main__": diff --git a/git-tools/commit-msg b/git-tools/commit-msg index 9aed76b..b004f8f 100755 --- a/git-tools/commit-msg +++ b/git-tools/commit-msg @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 import os,sys ROOT = os.path.dirname(__file__)+'/../../' @@ -6,7 +6,7 @@ file = open(ROOT+'version','r') lines = file.readlines() file.close() file = open(sys.argv[1],'a') -print >>file, 'Version:'+lines[0].split('=')[1] +print('Version:'+lines[0].split('=')[1], file=file) file.close() diff --git a/git-tools/post-commit b/git-tools/post-commit index 97cb4e5..beab67c 100755 --- a/git-tools/post-commit +++ b/git-tools/post-commit @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 import os ROOT = os.path.dirname(__file__)+'/../../' @@ -6,7 +6,7 @@ ROOT = os.path.dirname(__file__)+'/../../' file = open(ROOT+'version','r') ; lines = file.readlines() ; file.close() ; -v = map(int,lines[0].split('=')[1].split('.')); +v = list(map(int,lines[0].split('=')[1].split('.'))); v[2] += 1 lines[0] = "VERSION=%d.%d.%d\n"%tuple(v) diff --git a/make.config b/make.config index e37d250..b0a4736 100644 --- a/make.config +++ b/make.config @@ -6,4 +6,4 @@ FC ?= gfortran -g -ffree-line-length-none -fPIC # -fopenmp FCFLAGS ?= -fPIC AR ?= ar RANLIB ?= ranlib -PYTHON ?= python2 +PYTHON ?= python3 diff --git a/src/ezfio-head.py b/src/ezfio-head.py index 487aa5d..6508e8d 100644 --- a/src/ezfio-head.py +++ b/src/ezfio-head.py @@ -25,10 +25,11 @@ import os, sys import time -import cStringIO as StringIO +import io as StringIO from gzip import GzipFile import tempfile import threading +from functools import reduce def version(x): b = [int(i) for i in x.split('.')] @@ -40,7 +41,7 @@ def size(x): def flatten(l): res = [] for i in l: - if hasattr(i, "__iter__") and not isinstance(i, basestring): + if hasattr(i, "__iter__") and not isinstance(i, str): res.extend(flatten(i)) else: res.append(i) @@ -149,18 +150,18 @@ class ezfio_obj(object): except OSError: pass file = open(path.strip()+'/.version','w') - print >>file,self.version + print(self.version, file=file) file.close() def error(self,where,txt): - print '------------------------------------------------------------' - print 'EZFIO File : '+self.filename - print 'EZFIO Error in : '+where.strip() - print '------------------------------------------------------------' - print '' - print txt.strip() - print '' - print '------------------------------------------------------------' + print('------------------------------------------------------------') + print('EZFIO File : '+self.filename) + print('EZFIO Error in : '+where.strip()) + print('------------------------------------------------------------') + print('') + print(txt.strip()) + print('') + print('------------------------------------------------------------') raise IOError def get_filename(self): @@ -233,7 +234,7 @@ echo %s > %s/ezfio/library"""%(filename,filename,self.LIBRARY,filename)) indices = [] values = [] - for i in xrange(isize): + for i in range(isize): try: line = self.file.readline().split() except: @@ -250,7 +251,7 @@ echo %s > %s/ezfio/library"""%(filename,filename,self.LIBRARY,filename)) if self.buffer_rank == -1: self.error('write_buffer','No buffered file is open.') - for i in xrange(isize): + for i in range(isize): for j in indices[i]: self.file.write("%4d "%(j,)) self.file.write("%24.15e\n"%(values[i],)) diff --git a/src/ezfio-tail.py b/src/ezfio-tail.py index 7275602..b2bf486 100644 --- a/src/ezfio-tail.py +++ b/src/ezfio-tail.py @@ -32,7 +32,7 @@ def main(): try: EZFIO_FILE = os.environ["EZFIO_FILE"] except KeyError: - print "EZFIO_FILE not defined" + print("EZFIO_FILE not defined") return 1 ezfio.set_file(EZFIO_FILE) @@ -42,7 +42,7 @@ def main(): try: f = getattr(ezfio,command) except AttributeError: - print "{0} not found".format(command) + print("{0} not found".format(command)) return 1 if command.startswith('has'): @@ -67,7 +67,7 @@ def main(): except NameError: data = text except: - print "Syntax Error" + print("Syntax Error") return 1 if data is None: data = "None" diff --git a/src/groups.py b/src/groups.py index 7ecca0a..30e4564 100644 --- a/src/groups.py +++ b/src/groups.py @@ -34,26 +34,26 @@ file = open("../version","r") v = file.readline() file.close() v = v.split('=')[1].strip() -print >>file_py, """ +print(""" def get_version(self): return '%s' version = property(fset=None,fget=get_version) -"""%(v) +"""%(v), file=file_py) import sys -for group in groups.keys(): - print path%{ 'group' : group } - print >>file_py, path_py%{ 'group' : group } +for group in list(groups.keys()): + print(path%{ 'group' : group }) + print(path_py%{ 'group' : group }, file=file_py) for var,type,dims,command in groups[group]: command_py = command dims_py = str(dims) - for g in groups.keys(): + for g in list(groups.keys()): command_py = command_py.replace(g,'self.'+g) dims_py = dims_py.replace(g,'self.'+g) var = var.lower() group = group.lower() - strdims = tuple(map(lambda x: '('+str(x)+')',dims)) + strdims = tuple(['('+str(x)+')' for x in dims]) strdims = str(strdims).replace("'","") if len(dims) == 1: strdims = strdims[:-2]+")" @@ -76,8 +76,8 @@ for group in groups.keys(): 'var': var, 'group': group, 'dims': strdims} - print attributes%d - print >>file_py, attributes_py%d + print(attributes%d) + print(attributes_py%d, file=file_py) else: d = { 'type': type, 'var': var, @@ -88,15 +88,15 @@ for group in groups.keys(): 'expr_py': command_py} buffer = calculated%d buffer = re.sub(r"at\((.*),(.*)\)",r'\1(\2)',buffer) - print buffer - print >>file_py, calculated_py%d + print(buffer) + print(calculated_py%d, file=file_py) elif type == "buffered": d = { 'group': group, 'var': var, 'dims_py': dims_py, 'dims': dims } - print buffered%d - print >>file_py, buffered_py%d + print(buffered%d) + print(buffered_py%d, file=file_py) else: dims_loop = '' copy_loop = '' @@ -119,7 +119,7 @@ for group in groups.keys(): copy_loop = copy_loop[:-1]%{'var': var,'group': group} + ")\n" for k,d in enumerate(dims): copy_loop += (len(dims)-k-1)*" "+" enddo\n" - for g in groups.keys(): + for g in list(groups.keys()): dims_loop_py = dims_loop_py.replace(" "+g,' self.'+g) dims_loop_py = dims_loop_py.replace("*"+g,'*self.'+g) dims_loop_py = dims_loop_py.replace("/"+g,'/self.'+g) @@ -140,7 +140,7 @@ for group in groups.keys(): 'dims_loop_py': dims_loop_py, 'copy_loop': copy_loop, 'declar_loop' : declar_loop} - print attributes_arr%d - print >>file_py, attributes_arr_py%d + print(attributes_arr%d) + print(attributes_arr_py%d, file=file_py) file_py.close() diff --git a/src/groups_templates.py b/src/groups_templates.py index 034cdc3..b2fae1c 100644 --- a/src/groups_templates.py +++ b/src/groups_templates.py @@ -345,7 +345,7 @@ attributes_py = """ attributes_arr_py = """ def get_%(group)s_%(var)s(self): rank = %(rank)s - dims = range(rank) + dims = list(range(rank)) %(dims_loop_py)s dim_max = 1 for d in dims: @@ -359,7 +359,7 @@ attributes_arr_py = """ def set_%(group)s_%(var)s(self,%(var)s): rank = %(rank)s - dims = range(rank) + dims = list(range(rank)) %(dims_loop_py)s dim_max = 1 for d in dims: diff --git a/src/libezfio_file.irp.f b/src/libezfio_file.irp.f index 9dbdf10..d2e3be3 100644 --- a/src/libezfio_file.irp.f +++ b/src/libezfio_file.irp.f @@ -184,6 +184,6 @@ subroutine ezfio_finish() BEGIN_SHELL [ /usr/bin/env python3 ] import os from zlib import crc32 -print ' call irp_finalize_%s'%(str(abs(crc32(os.getcwd())))) +print(' call irp_finalize_%s'%(str(abs(crc32(os.getcwd().encode()))))) END_SHELL end diff --git a/src/libezfio_util.irp.f b/src/libezfio_util.irp.f index 0b3cdc8..35e5e50 100644 --- a/src/libezfio_util.irp.f +++ b/src/libezfio_util.irp.f @@ -300,11 +300,11 @@ end function ! Build Python functions """ for t in format.keys(): - print template_nowrite%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][0] } + print(template_nowrite%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][0] }) if not t.startswith("character"): - print template_write%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][0] } + print(template_write%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][0] }) if t != "logical": - print template_no_logical%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][0] } + print(template_no_logical%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][0] }) template_py = """ def read_%(type_short)s(self,dir,fil): @@ -331,7 +331,7 @@ template_py = """ l_filename += [ dir.strip()+'/'+fil ] dat = conv(dat) file = open(l_filename[0],'w') - print >>file,'%(fmt)s'%%(dat,) + print('%(fmt)s'%%(dat,), file=file) file.close() os.rename(l_filename[0],l_filename[1]) @@ -371,13 +371,13 @@ template_py = """ file.write("\\n") dat = flatten(dat) - for i in xrange(dim_max): + for i in range(dim_max): file.write("%(fmt)s\\n"%%(dat[i],)) file.flush() buffer = file.getvalue() file.close() file = GzipFile(filename=l_filename[0],mode='wb') - file.write(buffer) + file.write(buffer.encode()) file.close() os.rename(l_filename[0],l_filename[1]) except: @@ -387,16 +387,16 @@ template_py = """ file_py = open("libezfio_util-gen.py","w") for t in format.keys(): - print >>file_py, template_py%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][1] } + print (template_py%{ 'type_short' : t_short(t), 'type' : t, 'fmt':format[t][1] }, file=file_py) import os command = "'echo %s > '//libezfio_filename//'/ezfio/last_library'" cwd = os.getcwd() cwd = cwd.split('src')[:-1] cwd = '/'.join(cwd) -print >>file_py, """ +print(""" LIBRARY = "%s" -"""%cwd +"""%cwd,file=file_py) file_py.close() diff --git a/src/read_config.py b/src/read_config.py index 62d314e..3ff6e6d 100644 --- a/src/read_config.py +++ b/src/read_config.py @@ -31,7 +31,7 @@ import os, sys lines = [] for filename in [ '../config/'+i for i in os.listdir('../config')]: file = open(filename,'r') - lines += map(lambda x: (x,filename), file.readlines()) + lines += [(x,filename) for x in file.readlines()] try: if lines[-1] != '': lines.append( ('', filename) ) @@ -51,7 +51,7 @@ for line, filename in lines: groups[group] = my_list elif line[0] != ' ': # New group group = line.strip() - if group in groups.keys(): + if group in list(groups.keys()): my_list = groups[group] else: my_list = [] @@ -76,12 +76,12 @@ for line, filename in lines: my_list.append(tuple(buffer)) except: import sys, time - print >>sys.stderr, '' - print >>sys.stderr, '*'*80 - print >>sys.stderr, 'Error in EZFIO config file '+filename+' :' - print >>sys.stderr, line - print >>sys.stderr, '*'*80 - print >>sys.stderr, '' + print('', file=sys.stderr) + print('*'*80, file=sys.stderr) + print('Error in EZFIO config file '+filename+' :', file=sys.stderr) + print(line, file=sys.stderr) + print('*'*80, file=sys.stderr) + print('', file=sys.stderr) time.sleep(3) sys.exit(1) diff --git a/version b/version index 68b0096..f9a2e77 100644 --- a/version +++ b/version @@ -1 +1 @@ -VERSION=2.0 +VERSION=2.0.1