From a195d8829cae11b8cb0bb5d89de8a48a66d299d3 Mon Sep 17 00:00:00 2001 From: Victor Schwan Date: Wed, 6 Nov 2024 14:40:49 +0100 Subject: [PATCH] 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)]