diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index cdb9123d..287c6a6b 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -35,10 +35,13 @@ jobs: - run: bundle install - run: bundle exec danger || true - run: BML_OPENMP=no EMACS=emacs27 ./build.sh check_indent + - run: sudo apt-get install --yes pycodestyle + - run: pycodestyle $(git ls-files '*.py') build: name: Build and test (${{ matrix.JOBNAME }}) runs-on: ubuntu-20.04 + needs: lint strategy: fail-fast: false matrix: diff --git a/examples/gpmd/get_energy.py b/examples/gpmd/get_energy.py index 33714e05..f24da058 100644 --- a/examples/gpmd/get_energy.py +++ b/examples/gpmd/get_energy.py @@ -1,26 +1,23 @@ #!/usr/bin/env python -import os, sys +import sys import numpy -MyFileName=str(sys.argv[1]) -#print 'File name:', MyFileName +MyFileName = str(sys.argv[1]) -count=-1 -MyFile = open(MyFileName,"r") +count = -1 +MyFile = open(MyFileName, "r", encoding="utf-8") for lines in MyFile: - count = count + 1 -Dim=count -#print 'Dim', Dim + count = count + 1 +Dim = count datos = numpy.zeros(Dim+1) -count=-1 -MyFile = open(MyFileName,"r") +count = -1 +MyFile = open(MyFileName, "r", encoding="utf-8") for lines in MyFile: - lines_split = lines.split() - count = count + 1 - if(len(lines_split) > 2): - if(lines_split[0] == "Energy"): - if(lines_split[1] == "Total"): - print " ", lines_split[4] - + lines_split = lines.split() + count = count + 1 + if len(lines_split) > 2: + if lines_split[0] == "Energy": + if lines_split[1] == "Total": + print(" ", lines_split[4]) diff --git a/examples/gpmd/test-energy.py b/examples/gpmd/test-energy.py index d7be33fd..d661beac 100755 --- a/examples/gpmd/test-energy.py +++ b/examples/gpmd/test-energy.py @@ -1,4 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +import argparse + def compare_MD(reference, current, reltol): """Compare MD energies. @@ -8,29 +11,24 @@ def compare_MD(reference, current, reltol): """ - import sys - #energy = re.compile("MD_data\s+([0-9eE.+-]+)\s+([0-9eE.+-]+)") - - fd = open(reference) - reference_energies = [] - for line in fd: - result = line.split() - reference_energies.append(float(result[0])) - fd.close() - - fd = open(current) - current_energies = [] - for line in fd: - result = line.split() - current_energies.append(float(result[0])) - fd.close() - - + with open(reference, encoding="utf-8") as fd: + reference_energies = [] + for line in fd: + result = line.split() + reference_energies.append(float(result[0])) + + with open(current, encoding="utf-8") as fd: + current_energies = [] + for line in fd: + result = line.split() + current_energies.append(float(result[0])) + if len(reference_energies) != len(current_energies): - raise Exception("[error] different number of MD steps\n" - + (" reference ran for %4d steps\n" % (len(reference_energies))) - + (" current ran for %4d steps\n" % (len(current_energies))) - + " can not compare") + raise Exception( + "[error] different number of MD steps\n" + + (" reference ran for %4d steps\n" % (len(reference_energies))) + + (" current ran for %4d steps\n" % (len(current_energies))) + + " can not compare") result = True for i in range(len(reference_energies)): @@ -41,29 +39,34 @@ def compare_MD(reference, current, reltol): print("failure in MD step %d" % (i+1)) result = False if not result: - raise Exception(("[error] when comparing '%s' with '%s'" % (reference, current)) - + "energies do not agree") + raise Exception( + ("[error] when comparing '%s' with '%s'" % (reference, current)) + + "energies do not agree") + + print("Energy test passed without failure ...") - print("Energy test passed without failure ...") def main(): """The main function. """ - import argparse, os, sys - - parser = argparse.ArgumentParser(description="""Script to compare MD results by using the total energy""") - parser.add_argument("--reference", - help="The reference output") - parser.add_argument("--current", - help="The current output") - parser.add_argument("--reltol", - help="Relative tolerance when comparing, default is %(default)s", - type=float, - default=1e-10) + parser = argparse.ArgumentParser(description="""Script to compare + MD results by using the total energy""") + parser.add_argument( + "--reference", + help="The reference output") + parser.add_argument( + "--current", + help="The current output") + parser.add_argument( + "--reltol", + help="Relative tolerance when comparing, default is %(default)s", + type=float, + default=1e-10) options = parser.parse_args() compare_MD(options.reference, options.current, options.reltol) + if __name__ == "__main__": main() diff --git a/examples/gpmdcov/get_energy.py b/examples/gpmdcov/get_energy.py index 8212e8bf..c18f3cb1 100755 --- a/examples/gpmdcov/get_energy.py +++ b/examples/gpmdcov/get_energy.py @@ -3,14 +3,13 @@ import argparse import re -energy_re = re.compile("Energy Total.*=\s+([0-9-.]+)") +energy_re = re.compile(r"Energy Total.*=\s+([0-9-.]+)") parser = argparse.ArgumentParser() -parser.add_argument("OUT", - help="The output") +parser.add_argument("OUT", help="The output") options = parser.parse_args() -with open(options.OUT) as fd: +with open(options.OUT, encoding="utf-8") as fd: for line in fd: result = energy_re.search(line) if result: diff --git a/examples/gpmdcov/test-energy.py b/examples/gpmdcov/test-energy.py index d7be33fd..072a6e5a 100755 --- a/examples/gpmdcov/test-energy.py +++ b/examples/gpmdcov/test-energy.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +import argparse + + def compare_MD(reference, current, reltol): """Compare MD energies. @@ -8,29 +11,24 @@ def compare_MD(reference, current, reltol): """ - import sys - #energy = re.compile("MD_data\s+([0-9eE.+-]+)\s+([0-9eE.+-]+)") - - fd = open(reference) - reference_energies = [] - for line in fd: - result = line.split() - reference_energies.append(float(result[0])) - fd.close() - - fd = open(current) - current_energies = [] - for line in fd: - result = line.split() - current_energies.append(float(result[0])) - fd.close() - - + with open(reference, encoding="utf-8") as fd: + reference_energies = [] + for line in fd: + result = line.split() + reference_energies.append(float(result[0])) + + with open(current, encoding="utf-8") as fd: + current_energies = [] + for line in fd: + result = line.split() + current_energies.append(float(result[0])) + if len(reference_energies) != len(current_energies): - raise Exception("[error] different number of MD steps\n" - + (" reference ran for %4d steps\n" % (len(reference_energies))) - + (" current ran for %4d steps\n" % (len(current_energies))) - + " can not compare") + raise Exception( + "[error] different number of MD steps\n" + + (" reference ran for %4d steps\n" % (len(reference_energies))) + + (" current ran for %4d steps\n" % (len(current_energies))) + + " can not compare") result = True for i in range(len(reference_energies)): @@ -41,29 +39,34 @@ def compare_MD(reference, current, reltol): print("failure in MD step %d" % (i+1)) result = False if not result: - raise Exception(("[error] when comparing '%s' with '%s'" % (reference, current)) - + "energies do not agree") + raise Exception( + ("[error] when comparing '%s' with '%s'" % (reference, current)) + + "energies do not agree") + + print("Energy test passed without failure ...") - print("Energy test passed without failure ...") def main(): """The main function. """ - import argparse, os, sys - - parser = argparse.ArgumentParser(description="""Script to compare MD results by using the total energy""") - parser.add_argument("--reference", - help="The reference output") - parser.add_argument("--current", - help="The current output") - parser.add_argument("--reltol", - help="Relative tolerance when comparing, default is %(default)s", - type=float, - default=1e-10) + parser = argparse.ArgumentParser(description="""Script to compare MD + results by using the total energy""") + parser.add_argument( + "--reference", + help="The reference output") + parser.add_argument( + "--current", + help="The current output") + parser.add_argument( + "--reltol", + help="Relative tolerance when comparing, default is %(default)s", + type=float, + default=1e-10) options = parser.parse_args() compare_MD(options.reference, options.current, options.reltol) + if __name__ == "__main__": main()