Skip to content

Commit

Permalink
FileContainer: add path and meta properties (#121)
Browse files Browse the repository at this point in the history
* `FileContainer`: add path and meta properies

* changelog

* update typing
  • Loading branch information
mathause authored Nov 4, 2024
1 parent ddeadd1 commit 78c20cc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

Expand Down
9 changes: 9 additions & 0 deletions filefinder/_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
import warnings
from typing import Any

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions filefinder/tests/test_filecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 78c20cc

Please sign in to comment.