unicode()on the objects). It so happens that reconstructing a timedelta from its string representation can be implemented using a relatively simple regular expression:
from datetime import timedelta
"""Create timedelta object representing time delta
expressed in a string
Takes a string in the format produced by calling str() on
a python timedelta object and returns a timedelta instance
that would produce that string.
Acceptable formats are: "X days, HH:MM:SS" or "HH:MM:SS".
if s is None:
d = re.match(
r'((?P<days>\d+) days, )?(?P<hours>\d+):'
return timedelta(**dict(( (key, int(value))
for key, value in d.items() )))
But the other types are not quite so easy. Next time, I'll post my implementation for reconstructing datetime objects from their string representation.