From 783b4399e537858924c3d66bd25eab92061ea91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Berland?= Date: Fri, 23 Dec 2022 14:00:42 +0100 Subject: [PATCH] Remove py27 relics, black everything --- .github/workflows/style.yml | 4 +- cmake/create_cmakelists.py | 14 +- install/install.py | 202 -------------------- python/docs/examples/avg_pressure.py | 44 ++--- python/docs/examples/cmp_nnc.py | 27 ++- python/docs/examples/grid_info.py | 15 +- python/ecl/ecl_type.py | 2 - python/ecl/eclfile/ecl_3dkw.py | 2 - python/ecl/eclfile/ecl_kw.py | 2 - python/ecl/grid/faults/fault_line.py | 1 - python/ecl/rft/ecl_rft.py | 2 - python/ecl/util/geometry/geometry_tools.py | 8 +- python/ecl/util/util/stringlist.py | 16 +- python/tests/ecl_tests/test_fault_blocks.py | 4 +- python/tests/util_tests/test_string_list.py | 7 +- requirements.txt | 2 - setup.cfg | 3 + setup.py | 9 +- 18 files changed, 67 insertions(+), 297 deletions(-) delete mode 100644 install/install.py diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index 93df0a8fa5..da8fa10194 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -13,7 +13,7 @@ jobs: - name: Install dependencies run: | - sudo pip install cmake-format black==22.1.0 + sudo pip install cmake-format black - name: Clang Format run: ./script/clang-format --check @@ -25,4 +25,4 @@ jobs: - name: Black run: | - black --check python/ --exclude=docs + black --check . diff --git a/cmake/create_cmakelists.py b/cmake/create_cmakelists.py index 2f877df089..173331e6ba 100755 --- a/cmake/create_cmakelists.py +++ b/cmake/create_cmakelists.py @@ -5,7 +5,6 @@ import sys - def findFilesAndDirectories(directory): all_files = listdir(directory) files = [] @@ -24,7 +23,8 @@ def findRelativeModulePath(directory): """@type directory: str""" index = directory.rfind("python/") index += len("python/") - return directory[index:len(directory)] + return directory[index : len(directory)] + def createPythonSources(files, test_sources=False): result = "" @@ -42,6 +42,7 @@ def createPythonSources(files, test_sources=False): return result + def addSubDirectories(directories): result = "" @@ -50,20 +51,23 @@ def addSubDirectories(directories): return result + def addPythonPackage(relative_module_path, test_sources=False): module_name = ".".join(relative_module_path.split("/")) source_type = "PYTHON_SOURCES" if not test_sources else "TEST_SOURCES" - template = "add_python_package(\"python.%s\" ${PYTHON_INSTALL_PREFIX}/%s \"${%s}\" %s)" + template = 'add_python_package("python.%s" ${PYTHON_INSTALL_PREFIX}/%s "${%s}" %s)' install = "False" if test_sources else "True" return template % (module_name, relative_module_path, source_type, install) + def addInclude(filename): with open(filename, "r") as include_file: content = include_file.read() return content + files, directories = findFilesAndDirectories(sys.argv[1]) module_path = findRelativeModulePath(sys.argv[1]) @@ -79,7 +83,3 @@ def addInclude(filename): if "local.cmake" in files: text_file.write("\n\n") text_file.write(addInclude(join(sys.argv[1], "local.cmake"))) - - - - diff --git a/install/install.py b/install/install.py deleted file mode 100644 index 1a60337933..0000000000 --- a/install/install.py +++ /dev/null @@ -1,202 +0,0 @@ -# Copyright (C) 2011 Equinor ASA, Norway. -# -# The file 'ecl.py' is part of ERT - Ensemble based Reservoir Tool. -# -# ERT is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# ERT is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. -# -# See the GNU General Public License at -# for more details. -from __future__ import print_function -import pwd -import grp -import os -import os.path -import shutil -import re -import stat -import py_compile - -DEFAULT_DIR_MODE = 0755 -DEFAULT_EXE_MODE = 0755 -DEFAULT_REG_MODE = 0644 - -os.umask( 0 ) - - -def msg( verbose , text , arg): - text_width = 20 - if verbose: - pad_text = text + (text_width - len( text)) * "." + ": " - print(pad_text + arg) - - -def update_mode( path , mode , user , group): - try: - os.chmod( path , mode ) - - if user: # Only applicable for root: - user_info = pwd.getpwnam( user ) - uid = user_info[ 2 ] - os.chown( path , uid , -1 ) - - if group: - group_info = grp.getgrnam( group ) - gid = group_info[ 2 ] - os.chown( path , -1 , gid ) - except: - pass # Probably some missing permissions - - -def include_by_ext( full_path , ext_list): - if not os.path.exists( full_path ): - return False - - mode = os.stat( full_path )[stat.ST_MODE] - (base , ext) = os.path.splitext( full_path ) - if stat.S_ISDIR( mode ): - if ext == ".svn": - return False - else: - return True - else: - if ext in ext_list: - return True - else: - return False - - - -def include_python( full_path ): - return include_by_ext( full_path , [".py"]) - - -def include_html( full_path ): - return include_by_ext( full_path , [".html"]) - -def include_image( full_path ): - return include_by_ext( full_path , [".png" , ".jpg"]) - - -class File: - - def __init__( self , src , create_path = None , target_name = None ): - self.src = src - self.create_path = create_path - self.name = os.path.basename( self.src ) - if target_name: - self.target_name = target_name - else: - self.target_name = self.name - - - # Modes should be a tuple of three elements: (DIR_MODE , REG_MODE , EXE_MODE); - # all modes assume umask( 0 )! - def install( self , src_root , target_root , modes , verbose , user , group): - if self.create_path: - target_path = target_root - for path in self.create_path.split("/"): - target_path += "/%s" % path - if not os.path.exists( target_path ): - os.makedirs( target_path , modes[0] ) - msg(verbose , "Creating directory" , target_path) - update_mode( target_path , modes[0] , user, group) - target_file = "%s/%s/%s" % (target_root , self.create_path , self.target_name) - else: - target_file = "%s/%s" % (target_root , self.target_name) - - if os.path.isabs(self.src): - src_file = self.src - else: - src_file = "%s/%s" % (src_root , self.src) - - msg( verbose , "Copying file" , "%s -> %s" % (src_file , target_file)) - shutil.copyfile( src_file , target_file ) - if os.access( src_file , os.X_OK): - update_mode( target_file , modes[2] , user , group) - else: - update_mode( target_file , modes[1] , user , group ) - (target_base , ext) = os.path.splitext( target_file ) - if ext == ".py": - msg( verbose , "Byte compiling" , target_file) - try: - py_compile.compile( target_file ) - pyc_file = target_base + ".pyc" - update_mode( pyc_file , modes[1] , user , group) - except: - pass # Some permissions missing ... - - - - -class Install: - def __init__( self , src_root , target_root): - self.src_root = src_root - self.target_root = target_root - self.file_list = [] - self.link_list = [] - - - def install(self, modes = (DEFAULT_DIR_MODE , DEFAULT_REG_MODE, DEFAULT_EXE_MODE) , user = None , group = None , verbose = False): - for file in self.file_list: - file.install( self.src_root , self.target_root , modes , verbose , user , group) - - for (src,link) in self.link_list: - full_link = "%s/%s" % ( self.target_root , link ) - if os.path.exists( full_link ): - os.remove( full_link ) - - msg( verbose , "Linking" , "%s/%s -> %s/%s" % (self.target_root , link , self.target_root , src)) - os.symlink( "%s/%s" % (self.target_root , src), "%s/%s" % (self.target_root , link )) - - - def add_link( self , src , link , verbose = False): - self.link_list.append( ( src , link )) - - - - - def add_path( self , path , include_callback = None , create_path = None , recursive = False): - path_list = [] - for entry in os.listdir( "%s/%s" % (self.src_root , path )): - if entry == ".svn": - continue - - full_path = "%s/%s/%s" % ( self.src_root , path , entry) - - include = True - if include_callback: - include = include_callback( full_path ) - - if not include: - continue - - mode = os.stat( full_path )[stat.ST_MODE] - if stat.S_ISREG( mode ): - file = File("%s/%s" % (path , entry), create_path = create_path) - self.add_file( file ) - elif stat.S_ISDIR( mode ): - if recursive: - path_list.append( entry ) - - for dir in path_list: - if create_path: - new_create_path = "%s/%s" % (create_path , dir) - else: - new_create_path = dir - self.add_path( "%s/%s" % (path , dir) , include_callback = include_callback , create_path = new_create_path , recursive = True ) - - - def add_file( self , file ): - self.file_list.append( file ) - - - - - diff --git a/python/docs/examples/avg_pressure.py b/python/docs/examples/avg_pressure.py index aa502b8c77..1e45c1248e 100755 --- a/python/docs/examples/avg_pressure.py +++ b/python/docs/examples/avg_pressure.py @@ -11,22 +11,22 @@ # three different methods. def avg_pressure(p, sw, pv, region, region_id, result): if region: - p_pv = p*pv - p_hc_pv = p*pv*(1 - sw) - hc_pv = pv*(1 - sw) + p_pv = p * pv + p_hc_pv = p * pv * (1 - sw) + hc_pv = pv * (1 - sw) - total_pv = pv.sum( mask = region ) - total_hc_pv = hc_pv.sum( mask = region ) + total_pv = pv.sum(mask=region) + total_hc_pv = hc_pv.sum(mask=region) - p1 = p.sum( mask = region) / region.active_size( ) + p1 = p.sum(mask=region) / region.active_size() if total_pv > 0: - p2 = p_pv.sum( mask = region ) / total_pv + p2 = p_pv.sum(mask=region) / total_pv else: p2 = None if total_hc_pv > 0: - p3 = p_hc_pv.sum( mask = region ) / total_hc_pv + p3 = p_hc_pv.sum(mask=region) / total_hc_pv else: p3 = None else: @@ -35,15 +35,14 @@ def avg_pressure(p, sw, pv, region, region_id, result): p3 = None if not region_id in result: - result[region_id] = [[],[],[]] - + result[region_id] = [[], [], []] result[region_id][0].append(p1) result[region_id][1].append(p2) result[region_id][2].append(p3) -#----------------------------------------------------------------- +# ----------------------------------------------------------------- if __name__ == "__main__": case = sys.argv[1] @@ -52,33 +51,30 @@ def avg_pressure(p, sw, pv, region, region_id, result): init_file = EclFile("%s.INIT" % case) # Create PORV keyword where all the inactive cells have been removed. - pv = grid.compressed_kw_copy( init_file["PORV"][0] ) + pv = grid.compressed_kw_copy(init_file["PORV"][0]) # Extract an integer region keyword from the init file region_kw = init_file["EQLNUM"][0] - sim_days = [] result = {} for header in rst_file.headers(): line = {} - rst_block = rst_file.restart_view( report_step = header.get_report_step( ) ) + rst_block = rst_file.restart_view(report_step=header.get_report_step()) p = rst_block["PRESSURE"][0] sw = rst_block["SWAT"][0] + for region_id in range(region_kw.get_max() + 1): + region = EclRegion(grid, False) + region.select_equal(region_kw, region_id) + avg_pressure(p, sw, pv, region, region_id, result) - for region_id in range(region_kw.get_max( ) + 1): - region = EclRegion( grid, False ) - region.select_equal( region_kw, region_id ) - avg_pressure( p, sw, pv , region, region_id, result) - - avg_pressure( p, sw, pv , EclRegion( grid, True ), "field", result) - sim_days.append( header.get_sim_days( ) ) - + avg_pressure(p, sw, pv, EclRegion(grid, True), "field", result) + sim_days.append(header.get_sim_days()) for key in result.keys(): plt.figure(1) - for index,p in enumerate(result[key]): - plt.plot( sim_days, p , label="Region:%s P%d" % (key, index + 1)) + for index, p in enumerate(result[key]): + plt.plot(sim_days, p, label="Region:%s P%d" % (key, index + 1)) plt.legend() plt.show() diff --git a/python/docs/examples/cmp_nnc.py b/python/docs/examples/cmp_nnc.py index 248d3000f2..293c362635 100755 --- a/python/docs/examples/cmp_nnc.py +++ b/python/docs/examples/cmp_nnc.py @@ -5,33 +5,30 @@ from ecl.grid import EclGrid - - if __name__ == "__main__": case = sys.argv[1] - grid_file = EclFile("%s.EGRID" % case) - init_file = EclFile("%s.INIT" % case) - grid = EclGrid("%s.EGRID" % case) + grid_file = EclFile(f"{case}.EGRID") + init_file = EclFile(f"{case}.INIT") + grid = EclGrid(f"{case}.EGRID") nnc1 = grid_file["NNC1"][0] nnc2 = grid_file["NNC2"][0] tran = init_file["TRANNNC"][0] nnc_list = [] - for g1,g2,t in zip(nnc1,nnc2,tran): - nnc_list.append((g1,g2,t)) + for g1, g2, t in zip(nnc1, nnc2, tran): + nnc_list.append((g1, g2, t)) - nnc_list = sorted(nnc_list, key = itemgetter(0)) - for (g1,g2,T) in nnc_list: + nnc_list = sorted(nnc_list, key=itemgetter(0)) + for (g1, g2, T) in nnc_list: # grid_ijk assumes 0-based indexing, g1/g2 are 1-based (FORTRAN) # Convert them to zero based ones. g1 = g1 - 1 g2 = g2 - 1 - i1,j1,k1 = grid.get_ijk( global_index = g1 ) - i2,j2,k2 = grid.get_ijk( global_index = g2 ) + i1, j1, k1 = grid.get_ijk(global_index=g1) + i2, j2, k2 = grid.get_ijk(global_index=g2) # print 1-based indices just like in eclipse PRT files - print "(%02d,%02d,%02d) -> (%02d,%02d,%02d) T:%g" % (i1+1,j1+1,k1+1,i2+1,j2+1,k2+1,T) - - - + print( + f"({i1+1:02d},{j1+1:02d},{k1+1:02d}) -> ({i2+1:02d},{j2+1:02d},{k2+1:02d}) T:{T}" + ) diff --git a/python/docs/examples/grid_info.py b/python/docs/examples/grid_info.py index 20ffdb0cab..b569b18255 100755 --- a/python/docs/examples/grid_info.py +++ b/python/docs/examples/grid_info.py @@ -1,31 +1,32 @@ #!/usr/bin/env python import sys -from ecl.grid import EclGrid, EclRegion +from ecl.grid import EclRegion, EclGrid def volume_min_max(grid): vols = [c.volume for c in grid if c.active] return min(vols), max(vols) + def main(grid): - vmin,vmax = volume_min_max(grid) + vmin, vmax = volume_min_max(grid) dz_limit = 0.3 region = EclRegion(grid, False) region.select_thin(dz_limit) - print "Smallest cell : %g" % vmin - print "Largest cell : %g" % vmax - print "Thin active cells : %d" % region.active_size() + print(f"Smallest cell : {vmin}") + print(f"Largest cell : {vmax}") + print(f"Thin active cells : {region.active_size()}") for ai in region.get_active_list(): c = grid.cell(active_index=ai) - print('dz(%2d, %2d, %2d) = %.3f' % (c.i, c.j, c.k, c.dz)) + print(f"dz({c.i:2d}, {c.j:2d}, {c.k:2d}) = {c.dz:.3f}") if __name__ == "__main__": if len(sys.argv) < 2: - exit('usage: grid_info.py path/to/file.EGRID') + exit("usage: grid_info.py path/to/file.EGRID") case = sys.argv[1] grid = EclGrid(case) main(grid) diff --git a/python/ecl/ecl_type.py b/python/ecl/ecl_type.py index 51dc8a2b9a..4ef18d3ef0 100644 --- a/python/ecl/ecl_type.py +++ b/python/ecl/ecl_type.py @@ -13,8 +13,6 @@ # # See the GNU General Public License at # for more details. -from __future__ import absolute_import - from cwrap import BaseCClass, BaseCEnum from ecl import EclPrototype diff --git a/python/ecl/eclfile/ecl_3dkw.py b/python/ecl/eclfile/ecl_3dkw.py index eb05e470dc..8274732d54 100644 --- a/python/ecl/eclfile/ecl_3dkw.py +++ b/python/ecl/eclfile/ecl_3dkw.py @@ -14,8 +14,6 @@ # See the GNU General Public License at # for more details. -from __future__ import absolute_import, division, print_function, unicode_literals - from ecl.util.util import monkey_the_camel from .ecl_kw import EclKW diff --git a/python/ecl/eclfile/ecl_kw.py b/python/ecl/eclfile/ecl_kw.py index 07ea8581cf..a499424d81 100644 --- a/python/ecl/eclfile/ecl_kw.py +++ b/python/ecl/eclfile/ecl_kw.py @@ -38,8 +38,6 @@ the libecl library. """ -from __future__ import absolute_import, division, print_function, unicode_literals - import ctypes import warnings import numpy diff --git a/python/ecl/grid/faults/fault_line.py b/python/ecl/grid/faults/fault_line.py index caa0e2f273..1bbb85eadb 100644 --- a/python/ecl/grid/faults/fault_line.py +++ b/python/ecl/grid/faults/fault_line.py @@ -14,7 +14,6 @@ # See the GNU General Public License at # for more details. -from __future__ import print_function import sys from ecl.util.util import monkey_the_camel diff --git a/python/ecl/rft/ecl_rft.py b/python/ecl/rft/ecl_rft.py index 1e963f3da5..46301857cf 100644 --- a/python/ecl/rft/ecl_rft.py +++ b/python/ecl/rft/ecl_rft.py @@ -17,8 +17,6 @@ Module for loading ECLIPSE RFT files. """ -from __future__ import absolute_import, division, print_function, unicode_literals - from cwrap import BaseCClass from ecl import EclPrototype diff --git a/python/ecl/util/geometry/geometry_tools.py b/python/ecl/util/geometry/geometry_tools.py index 20329fd84a..1b7525cd92 100644 --- a/python/ecl/util/geometry/geometry_tools.py +++ b/python/ecl/util/geometry/geometry_tools.py @@ -1,6 +1,6 @@ from math import sqrt +import functools import sys -import six class GeometryTools(object): @@ -111,9 +111,9 @@ def keepLeft(hull, r): return hull - l = six.functools.reduce(keepLeft, points, []) - u = six.functools.reduce(keepLeft, reversed(points), []) - l.extend([u[i] for i in six.moves.xrange(1, len(u) - 1)]) + l = functools.reduce(keepLeft, points, []) + u = functools.reduce(keepLeft, reversed(points), []) + l.extend([u[i] for i in range(1, len(u) - 1)]) return l diff --git a/python/ecl/util/util/stringlist.py b/python/ecl/util/util/stringlist.py index 614cef2bb5..9546b423bf 100644 --- a/python/ecl/util/util/stringlist.py +++ b/python/ecl/util/util/stringlist.py @@ -30,8 +30,6 @@ return a normal python list of string objects, used in this way you hardly need to notice that the StringList class is at play. """ -from __future__ import absolute_import, division, print_function, unicode_literals -from six import string_types from ecl import EclPrototype from cwrap import BaseCClass @@ -79,7 +77,7 @@ def __init__(self, initial=None): if not initial: return - if isinstance(initial, string_types): + if isinstance(initial, str): raise TypeError( ( 'Cannot initialize a StringList from "{initial}".\n' @@ -89,7 +87,7 @@ def __init__(self, initial=None): for s in initial: if isinstance(s, bytes): s.decode("ascii") - if isinstance(s, string_types): + if isinstance(s, str): self.append(s) else: raise TypeError('Item is not a string: "%s".' % s) @@ -121,7 +119,7 @@ def __setitem__(self, index, value): ) if isinstance(value, bytes): value = value.decode("ascii") - if isinstance(value, string_types): + if isinstance(value, str): self._iset(index, value) else: raise TypeError("Item: %s not string type" % value) @@ -157,7 +155,7 @@ def __contains__(self, s): def __iadd__(self, other): if isinstance(other, bytes): other.decode("ascii") - if isinstance(other, string_types): + if isinstance(other, str): raise TypeError("Can not add strings with + - use append()") for s in other: self.append(s) @@ -171,7 +169,7 @@ def __add__(self, other): def __ior__(self, other): if isinstance(other, bytes): other.decode("ascii") - if isinstance(other, string_types): + if isinstance(other, str): raise TypeError("Can not | with string.") for s in other: if not s in self: @@ -237,7 +235,7 @@ def append(self, s): """ if isinstance(s, bytes): s.decode("ascii") - if isinstance(s, string_types): + if isinstance(s, str): self._append(s) else: self._append(str(s)) @@ -283,7 +281,7 @@ def index(self, value): """@rtype: int""" if isinstance(value, bytes): value.decode("ascii") - if isinstance(value, string_types): + if isinstance(value, str): return self._find_first(value) raise KeyError( 'Cannot index by "%s", lst.index() needs a string.' % str(type(value)) diff --git a/python/tests/ecl_tests/test_fault_blocks.py b/python/tests/ecl_tests/test_fault_blocks.py index cb3ecd5304..11fa3ceb3f 100644 --- a/python/tests/ecl_tests/test_fault_blocks.py +++ b/python/tests/ecl_tests/test_fault_blocks.py @@ -14,15 +14,13 @@ # # See the GNU General Public License at # for more details. -from __future__ import print_function from unittest import skipIf -import warnings import cwrap from ecl import EclDataType from ecl.eclfile import EclKW from ecl.grid import EclGrid, EclRegion -from ecl.grid.faults import FaultBlock, FaultBlockLayer, FaultBlockCell, FaultCollection +from ecl.grid.faults import FaultBlock, FaultBlockLayer, FaultCollection from ecl.util.geometry import Polyline, CPolylineCollection from ecl.util.test import TestAreaContext from tests import EclTest diff --git a/python/tests/util_tests/test_string_list.py b/python/tests/util_tests/test_string_list.py index 0eabbeb43c..5c4c13c1de 100644 --- a/python/tests/util_tests/test_string_list.py +++ b/python/tests/util_tests/test_string_list.py @@ -1,9 +1,4 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - -try: - from unittest2 import TestCase -except ImportError: - from unittest import TestCase +from unittest import TestCase from ecl.util.util import StringList diff --git a/requirements.txt b/requirements.txt index 7886c3a4cc..e7d4457267 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,9 @@ cwrap -future ninja numpy pandas scikit-build setuptools setuptools_scm -six sphinx!=3.4.0 wheel diff --git a/setup.cfg b/setup.cfg index 31ad82b6a0..c62373ce9c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,5 @@ [aliases] test = pytest + +[flake8] +max-line-length = 88 diff --git a/setup.py b/setup.py index 32a08ecfe7..ce048a0df0 100644 --- a/setup.py +++ b/setup.py @@ -72,13 +72,8 @@ def utility_wrappers(): platforms="any", install_requires=[ "cwrap", - "functools32;python_version=='2.7'", - "future", - "numpy;python_version>='3.0'", - "numpy<=1.16.6;python_version=='2.7'", - "pandas;python_version>='3.0'", - "pandas<=0.25.3;python_version=='2.7'", - "six", + "numpy", + "pandas", ], entry_points={"console_scripts": utility_wrappers()}, cmake_args=[