From 0bcc5b260fb97f0c72be98c9cf790bc3faaaa7b3 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 14 Jan 2022 20:25:32 -0800 Subject: [PATCH] pre-commit: sort Python includes (PEP8) (#1175) * pre-commit: sort Python includes (PEP8) Sort Python includes according to PEP8 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .editorconfig | 5 ++++ .pre-commit-config.yaml | 26 +++++++++++++++++++ docs/source/conf.py | 2 ++ examples/10_streaming_read.py | 3 ++- examples/10_streaming_write.py | 5 ++-- examples/11_particle_dataframe.py | 10 ++++--- examples/12_span_write.py | 2 +- examples/13_write_dynamic_configuration.py | 4 +-- examples/2_read_serial.py | 1 - examples/2a_read_thetaMode_serial.py | 1 - examples/3_write_serial.py | 3 +-- examples/3a_write_thetaMode_serial.py | 3 +-- examples/3b_write_resizable_particles.py | 3 +-- examples/4_read_parallel.py | 2 -- examples/5_write_parallel.py | 4 +-- examples/7_extended_write_serial.py | 6 ++--- examples/9_particle_write_serial.py | 5 ++-- setup.py | 8 +++--- src/binding/python/openpmd_api/DaskArray.py | 2 ++ .../python/openpmd_api/DaskDataFrame.py | 1 + src/binding/python/openpmd_api/DataFrame.py | 2 ++ src/binding/python/openpmd_api/__init__.py | 7 +++-- src/binding/python/openpmd_api/ls/__main__.py | 1 + .../python/openpmd_api/pipe/__main__.py | 3 ++- src/cli/pipe.py | 3 ++- test/python/unittest/API/APITest.py | 7 ++--- test/python/unittest/Test.py | 2 +- 27 files changed, 78 insertions(+), 43 deletions(-) diff --git a/.editorconfig b/.editorconfig index b63a89f6c6..adc5a1c59c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -30,3 +30,8 @@ trim_trailing_whitespace = false # TABs are part of its syntax indent_style = tab indent_size = unset + + +[*.py] +# isort config +force_sort_within_sections = true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3774065f12..31fb1884b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -55,6 +55,32 @@ repos: # C++ formatting # clang-format +# Autoremoves unused Python imports +- repo: https://github.com/hadialqattan/pycln + rev: v1.1.0 + hooks: + - id: pycln + name: pycln (python) + +# Sorts Python imports according to PEP8 +# https://www.python.org/dev/peps/pep-0008/#imports +- repo: https://github.com/pycqa/isort + rev: 5.10.1 + hooks: + - id: isort + name: isort (python) + +# Python: Flake8 (checks only, does this support auto-fixes?) +#- repo: https://github.com/PyCQA/flake8 +# rev: 4.0.1 +# hooks: +# - id: flake8 +# additional_dependencies: &flake8_dependencies +# - flake8-bugbear +# - pep8-naming +# exclude: ^(docs/.*|tools/.*)$ +# Alternatively: use autopep8? + # Python Formatting #- repo: https://github.com/psf/black # rev: 21.10b0 # Keep in sync with blacken-docs diff --git a/docs/source/conf.py b/docs/source/conf.py index 7a55e977e8..ca3f41e65a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -15,7 +15,9 @@ # import os import subprocess + from recommonmark.parser import CommonMarkParser + # import sys # sys.path.insert(0, os.path.abspath('.')) diff --git a/examples/10_streaming_read.py b/examples/10_streaming_read.py index 4169f541f5..d6f7a6e67b 100755 --- a/examples/10_streaming_read.py +++ b/examples/10_streaming_read.py @@ -1,8 +1,9 @@ #!/usr/bin/env python import json -import openpmd_api as io import sys +import openpmd_api as io + # pass-through for ADIOS2 engine parameters # https://adios2.readthedocs.io/en/latest/engines/engines.html config = {'adios2': {'engine': {}, 'dataset': {}}} diff --git a/examples/10_streaming_write.py b/examples/10_streaming_write.py index c6ead08c47..514b815202 100755 --- a/examples/10_streaming_write.py +++ b/examples/10_streaming_write.py @@ -1,9 +1,10 @@ #!/usr/bin/env python import json -import openpmd_api as io -import numpy as np import sys +import numpy as np +import openpmd_api as io + # pass-through for ADIOS2 engine parameters # https://adios2.readthedocs.io/en/latest/engines/engines.html config = {'adios2': {'engine': {}, 'dataset': {}}} diff --git a/examples/11_particle_dataframe.py b/examples/11_particle_dataframe.py index d3ae34a18d..9b5e626705 100755 --- a/examples/11_particle_dataframe.py +++ b/examples/11_particle_dataframe.py @@ -6,9 +6,11 @@ Authors: Axel Huebl, Dmitry Ganyushin License: LGPLv3+ """ -import openpmd_api as io -import numpy as np import sys + +import numpy as np +import openpmd_api as io + try: import pandas as pd except ImportError: @@ -16,9 +18,9 @@ sys.exit() found_dask = False try: - from dask.delayed import delayed - import dask.array as da import dask + import dask.array as da + from dask.delayed import delayed found_dask = True except ImportError: print("dask NOT found. Install dask to run the 2nd example.") diff --git a/examples/12_span_write.py b/examples/12_span_write.py index f6c40b6324..c776bd04a7 100644 --- a/examples/12_span_write.py +++ b/examples/12_span_write.py @@ -1,5 +1,5 @@ -import openpmd_api as io import numpy as np +import openpmd_api as io def span_write(filename): diff --git a/examples/13_write_dynamic_configuration.py b/examples/13_write_dynamic_configuration.py index d3af270075..ce96456f03 100644 --- a/examples/13_write_dynamic_configuration.py +++ b/examples/13_write_dynamic_configuration.py @@ -1,8 +1,8 @@ #!/usr/bin/env python import json -import openpmd_api as io -import numpy as np +import numpy as np +import openpmd_api as io # This example demonstrates how to use JSON/TOML-based dynamic # configuration for openPMD. diff --git a/examples/2_read_serial.py b/examples/2_read_serial.py index 4c1bf84402..9b68db4809 100755 --- a/examples/2_read_serial.py +++ b/examples/2_read_serial.py @@ -8,7 +8,6 @@ """ import openpmd_api as io - if __name__ == "__main__": series = io.Series("../samples/git-sample/data%T.h5", io.Access.read_only) diff --git a/examples/2a_read_thetaMode_serial.py b/examples/2a_read_thetaMode_serial.py index 1a88ac54c0..b2ec25bc20 100755 --- a/examples/2a_read_thetaMode_serial.py +++ b/examples/2a_read_thetaMode_serial.py @@ -8,7 +8,6 @@ """ import openpmd_api as io - if __name__ == "__main__": series = io.Series("../samples/git-sample/thetaMode/data%T.h5", io.Access.read_only) diff --git a/examples/3_write_serial.py b/examples/3_write_serial.py index 46dc536fb2..5aabd2998a 100755 --- a/examples/3_write_serial.py +++ b/examples/3_write_serial.py @@ -6,9 +6,8 @@ Authors: Axel Huebl License: LGPLv3+ """ -import openpmd_api as io import numpy as np - +import openpmd_api as io if __name__ == "__main__": # user input: size of matrix to write, default 3x3 diff --git a/examples/3a_write_thetaMode_serial.py b/examples/3a_write_thetaMode_serial.py index 7d3f581adf..8570383c42 100755 --- a/examples/3a_write_thetaMode_serial.py +++ b/examples/3a_write_thetaMode_serial.py @@ -6,9 +6,8 @@ Authors: Axel Huebl License: LGPLv3+ """ -import openpmd_api as io import numpy as np - +import openpmd_api as io if __name__ == "__main__": # open file for writing diff --git a/examples/3b_write_resizable_particles.py b/examples/3b_write_resizable_particles.py index 03dd780b29..eb604deb71 100755 --- a/examples/3b_write_resizable_particles.py +++ b/examples/3b_write_resizable_particles.py @@ -6,9 +6,8 @@ Authors: Axel Huebl License: LGPLv3+ """ -import openpmd_api as io import numpy as np - +import openpmd_api as io if __name__ == "__main__": # open file for writing diff --git a/examples/4_read_parallel.py b/examples/4_read_parallel.py index 4956ca78c9..35efc9a4f9 100755 --- a/examples/4_read_parallel.py +++ b/examples/4_read_parallel.py @@ -11,10 +11,8 @@ # on import: calls MPI_Init_thread() # exit hook: calls MPI_Finalize() from mpi4py import MPI - import openpmd_api as io - if __name__ == "__main__": # also works with any other MPI communicator comm = MPI.COMM_WORLD diff --git a/examples/5_write_parallel.py b/examples/5_write_parallel.py index 662cb95353..8000f6867f 100755 --- a/examples/5_write_parallel.py +++ b/examples/5_write_parallel.py @@ -11,10 +11,8 @@ # on import: calls MPI_Init_thread() # exit hook: calls MPI_Finalize() from mpi4py import MPI - -import openpmd_api as io import numpy as np - +import openpmd_api as io if __name__ == "__main__": # also works with any other MPI communicator diff --git a/examples/7_extended_write_serial.py b/examples/7_extended_write_serial.py index a9cdcd291e..8a8a077058 100755 --- a/examples/7_extended_write_serial.py +++ b/examples/7_extended_write_serial.py @@ -6,11 +6,11 @@ Authors: Axel Huebl, Fabian Koller License: LGPLv3+ """ -from openpmd_api import Series, Access, Dataset, Mesh_Record_Component, \ - Unit_Dimension import json -import numpy as np +import numpy as np +from openpmd_api import (Access, Dataset, Mesh_Record_Component, Series, + Unit_Dimension) SCALAR = Mesh_Record_Component.SCALAR diff --git a/examples/9_particle_write_serial.py b/examples/9_particle_write_serial.py index 4d96c83592..659ca846d1 100755 --- a/examples/9_particle_write_serial.py +++ b/examples/9_particle_write_serial.py @@ -6,10 +6,9 @@ Authors: Axel Huebl License: LGPLv3+ """ -from openpmd_api import Series, Access, Dataset, Mesh_Record_Component, \ - Unit_Dimension import numpy as np - +from openpmd_api import (Access, Dataset, Mesh_Record_Component, Series, + Unit_Dimension) SCALAR = Mesh_Record_Component.SCALAR diff --git a/setup.py b/setup.py index 34973eea4f..3ad2306b77 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,12 @@ +from distutils.version import LooseVersion import os -import re -import sys import platform +import re import subprocess +import sys -from setuptools import setup, Extension +from setuptools import Extension, setup from setuptools.command.build_ext import build_ext -from distutils.version import LooseVersion class CMakeExtension(Extension): diff --git a/src/binding/python/openpmd_api/DaskArray.py b/src/binding/python/openpmd_api/DaskArray.py index 1cb4260700..0d2a1ec4ad 100644 --- a/src/binding/python/openpmd_api/DaskArray.py +++ b/src/binding/python/openpmd_api/DaskArray.py @@ -6,7 +6,9 @@ License: LGPLv3+ """ import math + import numpy as np + try: from dask.array import from_array found_dask = True diff --git a/src/binding/python/openpmd_api/DaskDataFrame.py b/src/binding/python/openpmd_api/DaskDataFrame.py index 7d0fb9204c..fa18f7c076 100644 --- a/src/binding/python/openpmd_api/DaskDataFrame.py +++ b/src/binding/python/openpmd_api/DaskDataFrame.py @@ -6,6 +6,7 @@ License: LGPLv3+ """ import numpy as np + try: import dask.dataframe as dd from dask.delayed import delayed diff --git a/src/binding/python/openpmd_api/DataFrame.py b/src/binding/python/openpmd_api/DataFrame.py index 55e14fcafb..d0e01acab8 100644 --- a/src/binding/python/openpmd_api/DataFrame.py +++ b/src/binding/python/openpmd_api/DataFrame.py @@ -6,7 +6,9 @@ License: LGPLv3+ """ import math + import numpy as np + try: import pandas as pd found_pandas = True diff --git a/src/binding/python/openpmd_api/__init__.py b/src/binding/python/openpmd_api/__init__.py index af045c871c..e1bb49ef7e 100644 --- a/src/binding/python/openpmd_api/__init__.py +++ b/src/binding/python/openpmd_api/__init__.py @@ -1,9 +1,8 @@ from . import openpmd_api_cxx as cxx -from .openpmd_api_cxx import * # noqa -from .DataFrame import particles_to_dataframe -from .DaskDataFrame import particles_to_daskdataframe from .DaskArray import record_component_to_daskarray - +from .DaskDataFrame import particles_to_daskdataframe +from .DataFrame import particles_to_dataframe +from .openpmd_api_cxx import * # noqa __version__ = cxx.__version__ __doc__ = cxx.__doc__ diff --git a/src/binding/python/openpmd_api/ls/__main__.py b/src/binding/python/openpmd_api/ls/__main__.py index 2a4007755e..15d43875b9 100644 --- a/src/binding/python/openpmd_api/ls/__main__.py +++ b/src/binding/python/openpmd_api/ls/__main__.py @@ -9,6 +9,7 @@ License: LGPLv3+ """ import sys + from ..openpmd_api_cxx import _ls_run diff --git a/src/binding/python/openpmd_api/pipe/__main__.py b/src/binding/python/openpmd_api/pipe/__main__.py index 50392344d9..0c1b636028 100755 --- a/src/binding/python/openpmd_api/pipe/__main__.py +++ b/src/binding/python/openpmd_api/pipe/__main__.py @@ -8,11 +8,12 @@ Authors: Franz Poeschel License: LGPLv3+ """ -from .. import openpmd_api_cxx as io import argparse import os # os.path.basename import sys # sys.stderr.write +from .. import openpmd_api_cxx as io + # MPI is an optional dependency try: from mpi4py import MPI diff --git a/src/cli/pipe.py b/src/cli/pipe.py index df41971405..1d2ec3a05f 100755 --- a/src/cli/pipe.py +++ b/src/cli/pipe.py @@ -9,9 +9,10 @@ Authors: Franz Poeschel License: LGPLv3+ """ -import openpmd_api.pipe.__main__ as pipe import sys +import openpmd_api.pipe.__main__ as pipe + if __name__ == "__main__": pipe.main() sys.exit() diff --git a/test/python/unittest/API/APITest.py b/test/python/unittest/API/APITest.py index 8c314b7a00..0876e2c14a 100644 --- a/test/python/unittest/API/APITest.py +++ b/test/python/unittest/API/APITest.py @@ -6,13 +6,14 @@ License: LGPLv3+ """ -import openpmd_api as io - +import ctypes import gc import os import shutil import unittest -import ctypes + +import openpmd_api as io + try: import numpy as np found_numpy = True diff --git a/test/python/unittest/Test.py b/test/python/unittest/Test.py index b0652c3bbc..f094b46a2b 100644 --- a/test/python/unittest/Test.py +++ b/test/python/unittest/Test.py @@ -6,8 +6,8 @@ License: LGPLv3+ """ -import unittest import sys +import unittest # Import suites to run. from API.APITest import APITest