parameters: (time_string, mode='timedelta')
docstring: Transform time string to timedelta or second, only support 'weeks(W), days(D),
          hours(H), minutes(M), seconds(S)
          :param time_string: string,  time string like '10S', '20H', '3W'.
          :param mode: string, 'timedelta' or 'to_second', 'timedelta' represent transform
          time_string to timedelta, 'to_second' represent transform time_string to second.
          :return: 'mode' is 'timedelta', return datetime.timedelta; 'mode' is 'to_second',
          return int(second).
docstring_summary: Transform time string to timedelta or second, only support 'weeks(W), days(D),
          hours(H), minutes(M), seconds(S)
          :param time_string: string,  time string like '10S', '20H', '3W'.
          :param mode: string, 'timedelta' or 'to_second', 'timedelta' represent transform
          time_string to timedelta, 'to_second' represent transform time_string to second.
          :return: 'mode' is 'timedelta', return datetime.timedelta; 'mode' is 'to_second',
          return int(second).
docstring_tokens: Transform time string to timedelta or second only support weeks ( W ) days ( D ) hours ( H ) minutes ( M ) seconds ( S ) : param time_string : string time string like 10S 20H 3W . : param mode : string timedelta or to_second timedelta represent transform time_string to timedelta to_second represent transform time_string to second . : return : mode is timedelta return datetime . timedelta ; mode is to_second return int ( second ) .
function: def transform_time_string(time_string, mode='timedelta'):
          """
          Transform time string to timedelta or second, only support 'weeks(W), days(D),
          hours(H), minutes(M), seconds(S)
          :param time_string: string,  time string like '10S', '20H', '3W'.
          :param mode: string, 'timedelta' or 'to_second', 'timedelta' represent transform
          time_string to timedelta, 'to_second' represent transform time_string to second.
          :return: 'mode' is 'timedelta', return datetime.timedelta; 'mode' is 'to_second',
          return int(second).
          """
          if mode not in ('timedelta', 'to_second'):
              raise ValueError('wrong mode {mode} in time_transfer.'.format(mode=mode))
  
          time_prefix, time_suffix = re.match(r'(\d+)?([WDHMS])', time_string).groups()
  
          if time_suffix is None:
              raise ValueError('wrong format {time_string} for time_string in time_transfer.'.format(
                  time_string=time_string))
  
          if time_prefix is None:
              time_prefix = 1
          else:
              time_prefix = int(time_prefix)
  
          timedelta_mapper = {'W': timedelta(weeks=1),
                              'D': timedelta(days=1),
                              'H': timedelta(hours=1),
                              'M': timedelta(minutes=1),
                              'S': timedelta(seconds=1)}
  
          second_mapper = {'W': 7 * 24 * 3600, 'D': 24 * 3600, 'H': 3600, 'M': 60, 'S': 1}
  
          if mode == 'timedelta':
              return timedelta_mapper.get(time_suffix) * time_prefix
          if mode == 'to_second':
              return second_mapper.get(time_suffix) * time_prefix
function_tokens: def transform_time_string ( time_string , mode = 'timedelta' ) : if mode not in ( 'timedelta' , 'to_second' ) : raise ValueError ( 'wrong mode {mode} in time_transfer.' . format ( mode = mode ) ) time_prefix , time_suffix = re . match ( r'(\d+)?([WDHMS])' , time_string ) . groups ( ) if time_suffix is None : raise ValueError ( 'wrong format {time_string} for time_string in time_transfer.' . format ( time_string = time_string ) ) if time_prefix is None : time_prefix = 1 else : time_prefix = int ( time_prefix ) timedelta_mapper = { 'W' : timedelta ( weeks = 1 ) , 'D' : timedelta ( days = 1 ) , 'H' : timedelta ( hours = 1 ) , 'M' : timedelta ( minutes = 1 ) , 'S' : timedelta ( seconds = 1 ) } second_mapper = { 'W' : 7 * 24 * 3600 , 'D' : 24 * 3600 , 'H' : 3600 , 'M' : 60 , 'S' : 1 } if mode == 'timedelta' : return timedelta_mapper . get ( time_suffix ) * time_prefix if mode == 'to_second' : return second_mapper . get ( time_suffix ) * time_prefix