Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to add a list of time

Tags:

python

jira

I have a dictionary that contains a date and a list of time (in string).

input = {
    '2016-02-11': [
        u'30m',
        u'2h 30m',
        u'1h',
        u'2h',
        u'30m',
        u'1h',
        u'30m',
        u'1h',
        u'45m'
    ],
    '2016-01-27': [
        u'1d'
    ],
    '2016-01-28': [
        u'30m',
        u'5h',
        u'30m',
        u'1h',
        u'45m'
    ],
    '2016-01-29': [
        u'30m',
        u'6h 30m',
        u'45m'
    ],
    '2016-02-09': [
        u'30m',
        u'15m',
        u'4h',
        u'15m',
        u'2h',
        u'45m'
    ]
}

How do I add each time in the list? Such that the new dictionary would look like this:

output = {
    '2016-02-11': [
        '9h 45m'
    ],
    '2016-01-27': [
        '8h'
    ],
    '2016-01-28': [
        '7h 45m'
    ],
    '2016-01-29': [
        '7h 45m'
    ],
    '2016-02-09': [
        '7h 45m'
    ]
}

Some notes:

  • 1d == 8h
  • I want to count the timeSpent of a person per day that is why I need to add the list
  • If anyone was wondering, data is gathered from Jira using this API
like image 498
Mico Avatar asked Dec 01 '25 02:12

Mico


1 Answers

Make helper functions that will convert strings like 1h 30m to 90 (meaning 90 minutes), and reverse function:

def parse_time(s):
    """ '1h 30m' -> 90 """
    m = 0
    for x in s.split():
        if x.endswith('d'):
            m += int(x[:-1]) * 60 * 8  # NOTE: 8, not 24
        elif x.endswith('h'):
            m += int(x[:-1]) * 60
        elif x.endswith('m'):
            m += int(x[:-1])
    return m

def to_time(m):
    """ 90 -> '1h 30m' """
    d, m = divmod(m, 60 * 8)  # NOTE: 8, not 24
    h, m = divmod(m, 60)
    ret = []
    if d:
        ret.append('{}d'.format(d))
    if h:
        ret.append('{}h'.format(h))
    if m:
        ret.append('{}m'.format(m))
    return ' '.join(ret) or '0m'

Usage: Convert time strings to int values (minutes), and sum those values, convert the minutes back to time strings:

>>> parse_time('1h 30m')
90
>>> to_time(90)
'1h 30m'
>>> to_time(parse_time('1h 30m') + parse_time('30m'))
'2h'

>>> times = {
...     '2016-02-11': [ u'30m', u'2h 30m', u'1h', u'2h', u'30m',
...                     u'1h', u'30m', u'1h', u'45m' ],
...     '2016-01-27': [ u'1d' ],
...     '2016-01-28': [ u'30m', u'5h', u'30m', u'1h', u'45m' ],
...     '2016-01-29': [ u'30m', u'6h 30m', u'45m' ],
...     '2016-02-09': [ u'30m', u'15m', u'4h', u'15m', u'2h', u'45m' ]
... }
>>> {d: to_time(sum(map(parse_time, ts))) for d, ts in times.items()}
{'2016-01-27': '1d',
 '2016-01-28': '7h 45m',
 '2016-01-29': '7h 45m',
 '2016-02-09': '7h 45m',
 '2016-02-11': '1d 1h 45m'}

If you need date-strings to lists:

>>> {d: [to_time(sum(map(parse_time, ts)))] for d, ts in times.items()}
{'2016-01-27': ['1d'],
 '2016-01-28': ['7h 45m'],
 '2016-01-29': ['7h 45m'],
 '2016-02-09': ['7h 45m'],
 '2016-02-11': ['1d 1h 45m']}
like image 169
falsetru Avatar answered Dec 02 '25 17:12

falsetru