Timedelta constructor with string parameter

2014-09-23 Thread felipou
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'((?Phours\d+?)h)?((?Pminutes\d+?)m)?((?Pseconds\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?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Timedelta constructor with string parameter

2014-09-23 Thread Skip Montanaro
On Tue, Sep 23, 2014 at 4:11 AM, feli...@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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Timedelta constructor with string parameter

2014-09-23 Thread Felipe Menezes Machado
On Tue, Sep 23, 2014 at 6:39 PM, Skip Montanaro s...@pobox.com wrote:


 On Tue, Sep 23, 2014 at 4:11 AM, feli...@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
-- 
https://mail.python.org/mailman/listinfo/python-list