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

add argparse utility for parsing to timedelta #11

Open
aisch opened this issue May 24, 2016 · 3 comments
Open

add argparse utility for parsing to timedelta #11

aisch opened this issue May 24, 2016 · 3 comments

Comments

@aisch
Copy link

aisch commented May 24, 2016

i dont know if you think its good to put in the lib but i have something like:

class TimeDeltaAction(argparse.Action):

    def parse(self, value):
        parsed = pytimeparse.parse(value)
        if parsed is None:
            raise argparse.ArgumentError(
                self, '"{0}" invalid time-delta expression'.format(value),
            )
        return timedelta(seconds=parsed)

    # argparse.Action

    def __call__(self, parser, namespace, values, option_string=None):
        if isinstance(values, basestring):
            setattr(namespace, self.dest, self.parse(values))
        else:
            setattr(namespace, self.dest, map(self.parse, values))

copied around. would you consider a patch that adds it to pytimeparse?

@aisch aisch changed the title add argparse.Action utility for parsing time deltas add argparse.Action utility for parsing to timedelta May 24, 2016
@aisch
Copy link
Author

aisch commented May 25, 2016

@dnephin that's probably a more correct way to do it (type rather than action). would be nice to add that to this lib.

@aisch aisch changed the title add argparse.Action utility for parsing to timedelta add argparse utility for parsing to timedelta May 25, 2016
@wroberts
Copy link
Owner

@dnephin @aisch sounds like a good idea. Can either of you make a pull request?

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

3 participants