Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78202 > unrolled thread

Timedelta constructor with string parameter

Started byfelipou@gmail.com
First post2014-09-23 02:11 -0700
Last post2014-09-23 19:15 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Timedelta constructor with string parameter felipou@gmail.com - 2014-09-23 02:11 -0700
    Re: Timedelta constructor with string parameter Skip Montanaro <skip@pobox.com> - 2014-09-23 11:39 -0500
    Re: Timedelta constructor with string parameter Felipe Menezes Machado <felipou@gmail.com> - 2014-09-23 19:15 +0200

#78202 — Timedelta constructor with string parameter

Fromfelipou@gmail.com
Date2014-09-23 02:11 -0700
SubjectTimedelta constructor with string parameter
Message-ID<a63a4890-eeb3-495c-bb81-3c1bbb85bff2@googlegroups.com>
Hello everyone!

I created some code recently to parse a string and create a timedelta from it.

Right now it only accepts positive integers, and only hours, minutes and seconds, but I think it could be easily extended to support everything that timedelta accepts.


time_delta_regex = re.compile(
    r'((?P<hours>\d+?)h)?((?P<minutes>\d+?)m)?((?P<seconds>\d+?)s)?' )

def parseTimeDelta( value ):
    timedelta_params = value

    if isinstance( value, basestring ):
        parts = time_delta_regex.match( value )
        if not parts:
            return None
        parts = parts.groupdict()

        timedelta_params = {}
        for ( name, param ) in parts.iteritems():
            if param:
                timedelta_params[ name ] = int( param )

    return datetime.timedelta( **timedelta_params )

parseTimeDelta("5h32m15s")


I was thinking this conversion could be very useful to lots of people, and so I thought about proposing it as an enhancement to the standard library.

But before doing anything, I thought it would be a good idea to see what people here think.

I think this would be a nice addition as a constructor of the timedelta class, but I'm not sure if it would conflict with any existing code. I can't see any downside to including this in the constructor or as a separate function.

So, what do you guys think?

[toc] | [next] | [standalone]


#78225

FromSkip Montanaro <skip@pobox.com>
Date2014-09-23 11:39 -0500
Message-ID<mailman.14272.1411490392.18130.python-list@python.org>
In reply to#78202

[Multipart message — attachments visible in raw view] — view raw

On Tue, Sep 23, 2014 at 4:11 AM, <felipou@gmail.com> wrote:

> I created some code recently to parse a string and create a timedelta from
> it.


Interesting. I notice that dateutil.parser.parse already understands you
notation:

>>> x = dateutil.parser.parse("5h32m15s")
>>> x
datetime.datetime(2014, 9, 23, 5, 32, 15)

All that would be necessary is some way to tell it to generate a timedelta
instead of a datetime. Might be better to feed changes back to the dateutil
package maintainers.

Skip

[toc] | [prev] | [next] | [standalone]


#78228

FromFelipe Menezes Machado <felipou@gmail.com>
Date2014-09-23 19:15 +0200
Message-ID<mailman.14275.1411492570.18130.python-list@python.org>
In reply to#78202

[Multipart message — attachments visible in raw view] — view raw

On Tue, Sep 23, 2014 at 6:39 PM, Skip Montanaro <skip@pobox.com> wrote:

>
> On Tue, Sep 23, 2014 at 4:11 AM, <felipou@gmail.com> wrote:
>
>> I created some code recently to parse a string and create a timedelta
>> from it.
>
>
> Interesting. I notice that dateutil.parser.parse already understands you
> notation:
>
> >>> x = dateutil.parser.parse("5h32m15s")
> >>> x
> datetime.datetime(2014, 9, 23, 5, 32, 15)
>
> All that would be necessary is some way to tell it to generate a timedelta
> instead of a datetime. Might be better to feed changes back to the dateutil
> package maintainers.
>
>
Cool, thanks for the tip, I didn't know dateutil yet.

I looked at the code for the dateutil.parser.parse function, and under the
hood it is so much more than my function does. It accepts many different
formats, and also handles timezones. So I'd have to study it a little to
think of a way for it to generate timedelta objects instead of datetime
ones. But I'll definitely keep that in mind!

Felipe

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web