def precise_format_timedelta(delta, locale, threshold=0.85, decimals=2):
 seconds = delta.total_seconds()
 for (unit, secs_per_unit) in TIMEDELTA_UNITS:
  value = (abs(seconds) / secs_per_unit)
  if (value >= threshold):
   plural_form = locale.plural_form(value)
   pattern = None
   for choice in ((unit + ':medium'), unit):
    patterns = locale._data['unit_patterns'].get(choice)
    if (patterns is not None):
     pattern = patterns[plural_form]
     break
   if (pattern is None):
    return u''
   decimals = int(decimals)
   format_string = (('%.' + str(decimals)) + 'f')
   return pattern.replace('{0}', (format_string % value))
 return u''

def precise_format_timedelta ( delta , locale , threshold = 0.85 , decimals = 2 ) : seconds = delta . total_seconds ( ) for ( unit , secs_per_unit ) in TIMEDELTA_UNITS : value = ( abs ( seconds ) / secs_per_unit ) if ( value >= threshold ) : plural_form = locale . plural_form ( value ) pattern = None for choice in ( ( unit + ':medium' ) , unit ) : patterns = locale . _data [ 'unit_patterns' ] . get ( choice ) if ( patterns is not None ) : pattern = patterns [ plural_form ] break if ( pattern is None ) : return u'' decimals = int ( decimals ) format_string = ( ( '%.' + str ( decimals ) ) + 'f' ) return pattern . replace ( '{0}' , ( format_string % value ) ) return u''
like babel .