From 6e58bc4535541d2c535971f0edc5c08ee575c80d Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Fri, 11 Oct 2024 15:19:47 +0200 Subject: [PATCH 1/9] add compatibility with pathlib.Path to root_io --- python/podio/root_io.py | 51 ++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/python/podio/root_io.py b/python/podio/root_io.py index 830eb62e7..63ddca96d 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """Python module for reading root files containing podio Frames""" +from pathlib import Path from ROOT import gSystem gSystem.Load("libpodioRootIO") # noqa: E402 @@ -10,6 +11,32 @@ from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position # noqa: E402 +def convert_to_str_paths(filenames): + """Converts filenames to string paths, handling both string and pathlib.Path objects and + iterables of such objects. + + Args: + filenames (str, Path, or Iterable[str | Path]): A single filepath or an iterable of + filepaths to convert to str object(s). + + Returns: + list[str]: A list of filepaths as strings. + """ + if isinstance(filenames, (str, Path)): + return (str(filenames),) + + if isinstance(filenames, list): + str_filenames = [] + for f in filenames: + if isinstance(f, (str, Path)): + str_filenames.append(str(f)) + else: + raise TypeError(f"Invalid filename type: {f} (type: {type(f)})") + return str_filenames + + raise TypeError(f"Invalid filenames argument: {filenames} (type: {type(filenames)})") + + class Reader(BaseReaderMixin): """Reader class for reading podio root files.""" @@ -17,11 +44,9 @@ def __init__(self, filenames): """Create a reader that reads from the passed file(s). Args: - filenames (str or list[str]): file(s) to open and read data from + filenames (str or list[str] or Path or list[Path]): file(s) to open and read data from """ - if isinstance(filenames, str): - filenames = (filenames,) - + filenames = convert_to_str_paths(filenames) self._reader = podio.ROOTReader() self._reader.openFiles(filenames) @@ -35,11 +60,9 @@ def __init__(self, filenames): """Create an RNTuple reader that reads from the passed file(s). Args: - filenames (str or list[str]): file(s) to open and read data from + filenames (str or list[str] or Path or list[Path]): file(s) to open and read data from """ - if isinstance(filenames, str): - filenames = (filenames,) - + filenames = convert_to_str_paths(filenames) self._reader = podio.RNTupleReader() self._reader.openFiles(filenames) @@ -57,11 +80,9 @@ def __init__(self, filenames): """Create a reader that reads from the passed file(s). Args: - filenames (str or list[str]): file(s) to open and read data from + filenames (str or list[str] or Path or list[Path]): file(s) to open and read data from """ - if isinstance(filenames, str): - filenames = (filenames,) - + filenames = convert_to_str_paths(filenames) self._reader = podio.ROOTLegacyReader() self._reader.openFiles(filenames) self._is_legacy = True @@ -76,8 +97,9 @@ def __init__(self, filename): """Create a writer for writing files Args: - filename (str): The name of the output file + filename (str or Path): The name of the output file """ + filename = convert_to_str_paths(filename)[0] self._writer = podio.ROOTWriter(filename) super().__init__() @@ -89,7 +111,8 @@ def __init__(self, filename): """Create a writer for writing files Args: - filename (str): The name of the output file + filename (str or Path): The name of the output file """ + filename = convert_to_str_paths(filename)[0] self._writer = podio.RNTupleWriter(filename) super().__init__() From 542b7477904fe0dc3b41dab0027282e76f7195ac Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Wed, 23 Oct 2024 18:10:19 +0200 Subject: [PATCH 2/9] outsource TypeError handling to os.fspath --- python/podio/root_io.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/python/podio/root_io.py b/python/podio/root_io.py index 63ddca96d..e43d8fa51 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 """Python module for reading root files containing podio Frames""" +from collections.abc import Iterable +import os from pathlib import Path from ROOT import gSystem @@ -22,19 +24,11 @@ def convert_to_str_paths(filenames): Returns: list[str]: A list of filepaths as strings. """ - if isinstance(filenames, (str, Path)): - return (str(filenames),) - - if isinstance(filenames, list): - str_filenames = [] - for f in filenames: - if isinstance(f, (str, Path)): - str_filenames.append(str(f)) - else: - raise TypeError(f"Invalid filename type: {f} (type: {type(f)})") - return str_filenames - - raise TypeError(f"Invalid filenames argument: {filenames} (type: {type(filenames)})") + + if isinstance(filenames, Iterable) and not isinstance(filenames, (str, Path)): + return [os.fspath(fn) for fn in filenames] + + return [os.fspath(filenames)] class Reader(BaseReaderMixin): From faea5395f6bf88f4168e86e5e5d56b8ed381cb4d Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Thu, 24 Oct 2024 13:40:49 +0200 Subject: [PATCH 3/9] add pathlib compatibility in sio_io as well --- python/podio/sio_io.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/python/podio/sio_io.py b/python/podio/sio_io.py index 51c303f2a..ef4deba03 100644 --- a/python/podio/sio_io.py +++ b/python/podio/sio_io.py @@ -2,6 +2,7 @@ """Python module for reading sio files containing podio Frames""" from ROOT import gSystem +from root_io import convert_to_str_paths if gSystem.DynamicPathName("libpodioSioIO.so", True): gSystem.Load("libpodioSioIO") # noqa: 402 @@ -20,8 +21,9 @@ def __init__(self, filename): """Create a reader that reads from the passed file. Args: - filename (str): File to open and read data from + filename (str or Path): File to open and read data from. """ + filename = convert_to_str_paths(filename) self._reader = podio.SIOReader() self._reader.openFile(filename) @@ -39,8 +41,9 @@ def __init__(self, filename): """Create a reader that reads from the passed file. Args: - filename (str): File to open and read data from + filename (str or Path): File to open and read data from. """ + filename = convert_to_str_paths(filename) self._reader = podio.SIOLegacyReader() self._reader.openFile(filename) self._is_legacy = True @@ -49,13 +52,15 @@ def __init__(self, filename): class Writer(BaseWriterMixin): - """Writer class for writing podio root files""" + """Writer class for writing podio root files.""" def __init__(self, filename): - """Create a writer for writing files + """Create a writer for writing files. Args: - filename (str): The name of the output file + filename (str or Path): The name of the output file. """ + filename = convert_to_str_paths(filename) self._writer = podio.SIOWriter(filename) + super().__init__() From 6549246d04b64e82c3957e633430779d010c49e9 Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Wed, 6 Nov 2024 13:43:33 +0100 Subject: [PATCH 4/9] sio_io: only store first path in list returned by convert_to_str_paths --- python/podio/sio_io.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/podio/sio_io.py b/python/podio/sio_io.py index ef4deba03..19c03b012 100644 --- a/python/podio/sio_io.py +++ b/python/podio/sio_io.py @@ -23,7 +23,7 @@ def __init__(self, filename): Args: filename (str or Path): File to open and read data from. """ - filename = convert_to_str_paths(filename) + filename = convert_to_str_paths(filename)[0] self._reader = podio.SIOReader() self._reader.openFile(filename) @@ -43,7 +43,7 @@ def __init__(self, filename): Args: filename (str or Path): File to open and read data from. """ - filename = convert_to_str_paths(filename) + filename = convert_to_str_paths(filename)[0] self._reader = podio.SIOLegacyReader() self._reader.openFile(filename) self._is_legacy = True @@ -60,7 +60,7 @@ def __init__(self, filename): Args: filename (str or Path): The name of the output file. """ - filename = convert_to_str_paths(filename) + filename = convert_to_str_paths(filename)[0] self._writer = podio.SIOWriter(filename) super().__init__() From 692a72801e8427ee42882d836818f1253735da43 Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Wed, 6 Nov 2024 14:39:57 +0100 Subject: [PATCH 5/9] make ruff import sorting happy --- python/podio/root_io.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/podio/root_io.py b/python/podio/root_io.py index e43d8fa51..c9d8b9263 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -9,8 +9,12 @@ gSystem.Load("libpodioRootIO") # noqa: E402 from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position -from podio.base_reader import BaseReaderMixin # pylint: disable=wrong-import-position # noqa: E402 -from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position # noqa: E402 +from podio.base_reader import ( # pylint: disable=wrong-import-position # noqa: E402 + BaseReaderMixin, +) +from podio.base_writer import ( # pylint: disable=wrong-import-position # noqa: E402 + BaseWriterMixin, +) def convert_to_str_paths(filenames): From ef7098652c42fc8d4d1749d487a3a6e9496b8447 Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Wed, 6 Nov 2024 14:40:49 +0100 Subject: [PATCH 6/9] outsource convert_to_str_paths --- python/podio/root_io.py | 23 ++--------------------- python/podio/sio_io.py | 3 ++- python/podio/utils.py | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 python/podio/utils.py diff --git a/python/podio/root_io.py b/python/podio/root_io.py index c9d8b9263..efb0405ce 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 """Python module for reading root files containing podio Frames""" -from collections.abc import Iterable -import os -from pathlib import Path from ROOT import gSystem +from utils import convert_to_str_paths + gSystem.Load("libpodioRootIO") # noqa: E402 from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position @@ -17,24 +16,6 @@ ) -def convert_to_str_paths(filenames): - """Converts filenames to string paths, handling both string and pathlib.Path objects and - iterables of such objects. - - Args: - filenames (str, Path, or Iterable[str | Path]): A single filepath or an iterable of - filepaths to convert to str object(s). - - Returns: - list[str]: A list of filepaths as strings. - """ - - if isinstance(filenames, Iterable) and not isinstance(filenames, (str, Path)): - return [os.fspath(fn) for fn in filenames] - - return [os.fspath(filenames)] - - class Reader(BaseReaderMixin): """Reader class for reading podio root files.""" diff --git a/python/podio/sio_io.py b/python/podio/sio_io.py index 19c03b012..848729d79 100644 --- a/python/podio/sio_io.py +++ b/python/podio/sio_io.py @@ -2,7 +2,8 @@ """Python module for reading sio files containing podio Frames""" from ROOT import gSystem -from root_io import convert_to_str_paths + +from utils import convert_to_str_paths if gSystem.DynamicPathName("libpodioSioIO.so", True): gSystem.Load("libpodioSioIO") # noqa: 402 diff --git a/python/podio/utils.py b/python/podio/utils.py new file mode 100644 index 000000000..def26d853 --- /dev/null +++ b/python/podio/utils.py @@ -0,0 +1,21 @@ +import os +from collections.abc import Iterable +from pathlib import Path + + +def convert_to_str_paths(filenames): + """Converts filenames to string paths, handling both string and pathlib.Path objects and + iterables of such objects. + + Args: + filenames (str, Path, or Iterable[str | Path]): A single filepath or an iterable of + filepaths to convert to str object(s). + + Returns: + list[str]: A list of filepaths as strings. + """ + + if isinstance(filenames, Iterable) and not isinstance(filenames, (str, Path)): + return [os.fspath(fn) for fn in filenames] + + return [os.fspath(filenames)] From 65a7f4dc9d11b63920611121024e5d5adfd2798d Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Wed, 6 Nov 2024 19:37:28 +0100 Subject: [PATCH 7/9] Revert "make ruff import sorting happy" This reverts commit df74ff477b17e1b7fa6329ff3ae051bcbb13fef8. --- python/podio/root_io.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/python/podio/root_io.py b/python/podio/root_io.py index efb0405ce..d2cd8a23e 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -8,12 +8,8 @@ gSystem.Load("libpodioRootIO") # noqa: E402 from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position -from podio.base_reader import ( # pylint: disable=wrong-import-position # noqa: E402 - BaseReaderMixin, -) -from podio.base_writer import ( # pylint: disable=wrong-import-position # noqa: E402 - BaseWriterMixin, -) +from podio.base_reader import BaseReaderMixin # pylint: disable=wrong-import-position # noqa: E402 +from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position # noqa: E402 class Reader(BaseReaderMixin): From 7dfb788f2247082ad81d90c5c057d0debb717a51 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 8 Nov 2024 13:20:18 +0100 Subject: [PATCH 8/9] Fix imports to be absolute --- python/podio/root_io.py | 3 +-- python/podio/sio_io.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/python/podio/root_io.py b/python/podio/root_io.py index d2cd8a23e..95702f086 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -3,13 +3,12 @@ from ROOT import gSystem -from utils import convert_to_str_paths - gSystem.Load("libpodioRootIO") # noqa: E402 from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position from podio.base_reader import BaseReaderMixin # pylint: disable=wrong-import-position # noqa: E402 from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position # noqa: E402 +from podio.utils import convert_to_str_paths # pylint: disable=wrong-import-position # noqa: E402 class Reader(BaseReaderMixin): diff --git a/python/podio/sio_io.py b/python/podio/sio_io.py index 848729d79..9926b33ff 100644 --- a/python/podio/sio_io.py +++ b/python/podio/sio_io.py @@ -3,8 +3,6 @@ from ROOT import gSystem -from utils import convert_to_str_paths - if gSystem.DynamicPathName("libpodioSioIO.so", True): gSystem.Load("libpodioSioIO") # noqa: 402 else: @@ -13,6 +11,7 @@ from podio.base_reader import BaseReaderMixin # pylint: disable=wrong-import-position from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position +from podio.utils import convert_to_str_paths # pylint: disable=wrong-import-position # noqa: E402 class Reader(BaseReaderMixin): From 2a6513955c7ff4f4052b20139ed78a0e2d750fc6 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 8 Nov 2024 14:11:41 +0100 Subject: [PATCH 9/9] Add module docstring to fix pylint complaints --- python/podio/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/podio/utils.py b/python/podio/utils.py index def26d853..5b7221c88 100644 --- a/python/podio/utils.py +++ b/python/podio/utils.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 +"""utility functionality for podio""" + import os from collections.abc import Iterable from pathlib import Path