Created
August 28, 2015 05:13
-
-
Save MattMS/cca303ff11b7d5b6ff86 to your computer and use it in GitHub Desktop.
Create a Python timedelta from text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
import logging | |
logger = logging.getLogger(__name__) | |
def get_time_delta_from_text(text): | |
kwargs = dict() | |
for pair in text.split(','): | |
name, sep, value = pair.partition('=') | |
try: | |
kwargs[name] = int(value) | |
except ValueError as e: | |
logger.error('Non-integer value (%s) for %s.', value, name) | |
raise e | |
return timedelta(**kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment