def humanDuration(delta): """ Convert a duration in millisecond to human natural representation. Returns an unicode string. >>> humanDuration(0) u'0 ms' >>> humanDuration(213) u'213 ms' >>> humanDuration(4213) u'4 sec 213 ms' >>> humanDuration(6402309) u'1 hour 46 min 42 sec' """ if not isinstance(delta, timedelta): delta = timedelta(microseconds=delta * 1000) # Milliseconds text = [] if 1000 <= delta.microseconds: text.append(u"%u ms" % (delta.microseconds // 1000)) # Seconds minutes, seconds = divmod(delta.seconds, 60) hours, minutes = divmod(minutes, 60) if seconds: text.append(u"%u sec" % seconds) if minutes: text.append(u"%u min" % minutes) if hours: text.append(ngettext("%u hour", "%u hours", hours) % hours) # Days years, days = divmod(delta.days, 365) if days: text.append(ngettext("%u day", "%u days", days) % days) if years: text.append(ngettext("%u year", "%u years", years) % years) if 3 < len(text): text = text[-3:] elif not text: return u"0 ms" return u" ".join(reversed(text))
def humanDuration(delta): """ Convert a duration in millisecond to human natural representation. Returns an unicode string. >>> humanDuration(0) u'0 ms' >>> humanDuration(213) u'213 ms' >>> humanDuration(4213) u'4 sec 213 ms' >>> humanDuration(6402309) u'1 hour 46 min 42 sec' """ if not isinstance(delta, timedelta): delta = timedelta(microseconds=delta*1000) # Milliseconds text = [] if 1000 <= delta.microseconds: text.append(u"%u ms" % (delta.microseconds//1000)) # Seconds minutes, seconds = divmod(delta.seconds, 60) hours, minutes = divmod(minutes, 60) if seconds: text.append(u"%u sec" % seconds) if minutes: text.append(u"%u min" % minutes) if hours: text.append(ngettext("%u hour", "%u hours", hours) % hours) # Days years, days = divmod(delta.days, 365) if days: text.append(ngettext("%u day", "%u days", days) % days) if years: text.append(ngettext("%u year", "%u years", years) % years) if 3 < len(text): text = text[-3:] elif not text: return u"0 ms" return u" ".join(reversed(text))
def humanBitSize(size): """ Convert a size in bit to human classic representation. It uses the values: 1 Kbit is 1000 bits, 1 Mbit is 1000 Kbit, etc. The result is an unicode string. >>> humanBitSize(1) u'1 bit' >>> humanBitSize(790) u'790 bits' >>> humanBitSize(256960) u'257.0 Kbit' """ divisor = 1000 if size < divisor: return ngettext("%u bit", "%u bits", size) % size units = [u"Kbit", u"Mbit", u"Gbit", u"Tbit"] size = float(size) for unit in units: size = size / divisor if size < divisor: return "%.1f %s" % (size, unit) return u"%u %s" % (size, unit)
def humanFilesize(size): """ Convert a file size in byte to human natural representation. It uses the values: 1 KB is 1024 bytes, 1 MB is 1024 KB, etc. The result is an unicode string. >>> humanFilesize(1) u'1 byte' >>> humanFilesize(790) u'790 bytes' >>> humanFilesize(256960) u'250.9 KB' """ if size < 10000: return ngettext("%u byte", "%u bytes", size) % size units = [_("KB"), _("MB"), _("GB"), _("TB")] size = float(size) divisor = 1024 for unit in units: size = size / divisor if size < divisor: return "%.1f %s" % (size, unit) return "%u %s" % (size, unit)
def humanPixelSize(value): return ngettext("%s pixel", "%s pixels", value) % value
def humanAltitude(value): return ngettext("%.1f meter", "%.1f meters", value) % value
def createDictDesc(parent): return "Dict: %s" % (ngettext("%s key", "%s keys", parent.count) % parent.count)
def createTupleDesc(parent): count = parent["count"].value items = ngettext("%s item", "%s items", count) % count return "%s: %s" % (parent.code_info[2], items)