From 78c20ccfd12d9d7b2971836334703529c6c0049f Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Mon, 4 Nov 2024 14:11:47 +0100 Subject: [PATCH] `FileContainer`: add path and meta properties (#121) * `FileContainer`: add path and meta properies * changelog * update typing --- CHANGELOG.md | 2 ++ filefinder/_filefinder.py | 9 ++++++++ filefinder/tests/test_filecontainer.py | 29 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c548a04..0843ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ - Renamed the `"filename"` column to `"path"` and made it a `pd.Index`, thus removing this column from the underlying `DataFrame` ([#113](https://github.com/mathause/filefinder/pull/113)). + - Added `meta` and `paths` properties to `FileContainer` which allow to iterate over them + ([#121](https://github.com/mathause/filefinder/pull/121)). - Deprecated `combine_by_key` ([#115](https://github.com/mathause/filefinder/pull/115)). - Added the number of paths to the repr ([#116](https://github.com/mathause/filefinder/pull/116)). diff --git a/filefinder/_filefinder.py b/filefinder/_filefinder.py index 2d86f80..9dd983a 100644 --- a/filefinder/_filefinder.py +++ b/filefinder/_filefinder.py @@ -5,6 +5,7 @@ import os import re import warnings +from typing import Any import numpy as np import pandas as pd @@ -607,6 +608,14 @@ def __getitem__(self, key): ret.df = self.df.iloc[key] return ret + @property + def meta(self) -> list[dict[str, Any]]: + return self.df.to_dict("records") + + @property + def paths(self) -> list[str]: + return self.df.index.to_list() + def combine_by_key(self, keys=None, sep="."): warnings.warn( "`combine_by_key` has been deprecated and will be removed in a future version", diff --git a/filefinder/tests/test_filecontainer.py b/filefinder/tests/test_filecontainer.py index 9e31804..530687d 100644 --- a/filefinder/tests/test_filecontainer.py +++ b/filefinder/tests/test_filecontainer.py @@ -80,6 +80,35 @@ def test_fc_getitem(example_df, example_fc): assert_filecontainer_empty(result, columns=["model", "scen", "res"]) +def test_filecontainer_paths(example_fc): + + result = example_fc.paths + expected = [ + "file0", + "file1", + "file2", + "file3", + "file4", + ] + + assert result == expected + + +def test_filecontainer_meta(example_fc): + + result = example_fc.meta + + expected = [ + {"model": "a", "scen": "d", "res": "r"}, + {"model": "a", "scen": "h", "res": "r"}, + {"model": "b", "scen": "h", "res": "r"}, + {"model": "b", "scen": "d", "res": "r"}, + {"model": "c", "scen": "d", "res": "r"}, + ] + + assert result == expected + + def test_filecontainer_search(example_df, example_fc): with pytest.raises(KeyError):