Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absl flags pathlib support #165

Open
bsarden opened this issue Feb 26, 2021 · 4 comments
Open

Absl flags pathlib support #165

bsarden opened this issue Feb 26, 2021 · 4 comments

Comments

@bsarden
Copy link

bsarden commented Feb 26, 2021

Are there any plans to support a flags.DEFINE_path or something similar that utilizes python's pathlib module to support declaring os.PathLike operations for the default option?

For context, I often find myself doing the following:

from absl import app, flags
from pathlib import Path

FLAGS = flags.FLAGS
flags.DEFINE_string("filepath", "path/to/some.txt", "Input filepath for program")

def main(argv_):
    # Override FLAGS.filepath with a pathlib.Path version
    FLAGS.filepath = Path(FLAGS.filepath)
    
    # Rest of program uses pathlib for path operations

if __name__ == "__main__":
    app.run(main)

It would be awesome if there was an easy way to define a Path object from the DEFINE_ definition in the first place. If adding such a feature is not feasible, I'm curious how other people are using pathlib with absl.flags as well.

Thanks!

@gpshead
Copy link
Contributor

gpshead commented Feb 26, 2021

Internally I do see one recent user contributed library that implements its own DEFINE_path for use with absl. The gist of that goes something like this (simplified for illustration purposes) in apath_flag.py library:

flags.disclaim_key_flags()  # Prevent absl from attributing flags to this module.

class _PathParser(flags.ArgumentParser):
    def parse(self, value):
        return pathlib.PurePath(value)

class _PathSerializer(flags.ArgumentSerializer):
    def serialize(self, value):
        return str(value)

def DEFINE_path(...):
    return flags.DEFINE(_PathParser(), name, default, help, flag_values,
                        _PathSerializer(), **kwargs)

I agree, it'd make sense to add something like that to absl itself as pathlib is gaining popularity.

Internal source: pyglib.contrib.gpathlib - If we do this, presumably we might just refactor it out of that.

@danielkelshaw
Copy link

Just wondering if there was any update on this? I'd say the pathlib library is fairly standard now so it would be nice to have support from absl if possible.

@yilei
Copy link
Contributor

yilei commented Dec 15, 2022

Yes this is in my queue, hopefully I can get to it after the holidays if not earlier.

@aryamccarthy
Copy link

Perhaps DEFINE_path can happen by these holidays? :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants