mathause / filefinder

find and parse file and folder names
MIT License
3 stars 1 forks source link

method/ keyword to require exactly one match #98

Open mathause opened 1 day ago

mathause commented 1 day ago

A pattern I often do is:

fc = ff.find_files(...)

if len(fc) > 1:
    raise ValueError("More than one simulation found - please adapt your query")

It would be nice to move this functionality into filefinder itself. Either using a keyword (but what?) or a separate method. How about:

ff.find_single_file(...)
ff.find_single_path(...)

?


find_files looks like:

https://github.com/mathause/filefinder/blob/dc449bd57d0d2ad084ac42f37830a0dabeedd65d/filefinder/_filefinder.py#L386-L388

it has the on_parse_error and _allow_empty keywords. But I think both should error, so we won't add them to the signature and the method should look something like:


    def find_single_file(self, keys=None, **keys_kwargs):

        fc = self.full.find(
            keys=keys, on_parse_error="raise", _allow_empty=False, **keys_kwargs
        )

        if len(fc) > 1:
            raise ValueError(
                "More than one file found - please adapt your query or use `find_files`")

cc @veni-vidi-vici-dormivi

mathause commented 21 hours ago

oh wrote to the wrong issue...

veni-vidi-vici-dormivi commented 21 hours ago

I think this is cool, personally I would prefer a separate method, and yes, it makes sense to set on_parse_error and _allow_empty as you did.