caellion-python-commons
formatters.py
Go to the documentation of this file.
1 import math
2 
3 
4 class InvalidDurationException(Exception):
5  """!
6  This exception is raised whenever provided duration is zero or negative.
7  """
8 
9  pass
10 
11 
13  def formatSI(v, dec=0):
14  sign = ""
15  if v < 0:
16  sign = "-"
17  v = abs(v)
18  suffix = ""
19  SI_suffix = "kMGTPEZY"
20 
21  if v >= 1000:
22  value_mag = math.log10(v)
23 
24  suffix_index = min(len(SI_suffix) - 1, max(0, math.floor(value_mag / 3) - 1))
25 
26  value = v / pow(10, (suffix_index + 1) * 3)
27  suffix = SI_suffix[suffix_index]
28  else:
29  value = v
30 
31  return sign + ("{:." + str(dec) + "f}").format(value) + suffix
32 
33  def formatBinarySI(v, dec=0):
34  sign = ""
35  if v < 0:
36  sign = "-"
37  v = abs(v)
38  suffix = ""
39  SI_suffix = "kMGTPEZY"
40 
41  if v >= 1024:
42  value_mag = math.log(v, 2)
43 
44  suffix_index = min(len(SI_suffix) - 1, max(0, math.floor(value_mag / 10) - 1))
45 
46  value = v / pow(2, (suffix_index + 1) * 10)
47  suffix = SI_suffix[suffix_index] + "i"
48  else:
49  value = v
50 
51  return sign + ("{:." + str(dec) + "f}").format(value) + suffix
52 
53  # for formatting less than one (0.X) with suffixes like milli
54  def formatSISubValue(v, dec=0):
55  sign = ""
56  if v < 0:
57  sign = "-"
58  v = abs(v)
59  SI_suffix = "mμnpfazy"
60  max_log = (len(SI_suffix)) * 3
61 
62  if v >= 1 / pow(10, max_log):
63  value_mag = -1 * math.log10(v)
64  suffix_index = math.ceil(value_mag / 3) - 1
65  value = v * pow(10, (suffix_index + 1) * 3)
66  suffix = SI_suffix[suffix_index]
67  elif v > 0:
68  value = 0
69  suffix = "y"
70  else:
71  value = 0
72  suffix = ""
73 
74  return sign + ("{:." + str(dec) + "f}").format(value) + suffix
75 
76  # for formatting less than one (0.X) with suffixes like milli, binary
77  def formatSIBinarySubValue(v, dec=0): # pragma: no mutate
78  # this is unsupported!
79  return "BinSubValueError"
80 
81  # router for formatting all values
82  def formatSIFullRange(v, dec):
83  if v > -1 and v < 1:
84  return NumberFormatting.formatSISubValue(v, dec)
85  else:
86  return NumberFormatting.formatSI(v, dec)
87 
88  def formatUnitsPerIntervalDynamic(units, seconds, unit):
89 
90  if units < 0:
91  return "-" + NumberFormatting.formatUnitsPerIntervalDynamic(abs(units), seconds, unit)
92 
93  if seconds <= 0:
94  raise InvalidDurationException(str("{:s} is not a valid, positive duration").format(str(seconds)))
95 
96  units_per_second = units / seconds
97  units_per_millisecond = units_per_second / 1000.0
98  units_per_microsecond = units_per_second / 1000000.0
99  units_per_millenium = units_per_second * 1000.0 * 365.0 * 86400.0
100  units_per_century = units_per_second * 100.0 * 365.0 * 86400.0
101  units_per_year = units_per_second * 365.0 * 86400.0
102  units_per_week = units_per_second * 7.0 * 86400.0
103  units_per_day = units_per_second * 86400.0
104  units_per_hour = units_per_second * 3600.0
105  units_per_minute = units_per_second * 60.0
106 
107  if units_per_century < 1.0:
108  return str("{:.2f}{:s}/{:s}").format(units_per_millenium, unit, "millenium")
109  elif units_per_year < 1.0:
110  return str("{:.2f}{:s}/{:s}").format(units_per_century, unit, "century")
111  elif units_per_week < 1.0:
112  return str("{:.2f}{:s}/{:s}").format(units_per_year, unit, "year")
113  elif units_per_day < 1.0:
114  return str("{:.2f}{:s}/{:s}").format(units_per_week, unit, "week")
115  elif units_per_hour < 1.0:
116  return str("{:.2f}{:s}/{:s}").format(units_per_day, unit, "day")
117  elif units_per_minute < 1.0:
118  return str("{:.2f}{:s}/{:s}").format(units_per_hour, unit, "hour")
119  elif units_per_second < 1.0:
120  return str("{:.2f}{:s}/{:s}").format(units_per_minute, unit, "minute")
121  elif units_per_millisecond < 1.0:
122  return str("{:.2f}{:s}/{:s}").format(units_per_second, unit, "second")
123  elif units_per_microsecond < 1.0:
124  return str("{:.2f}{:s}/{:s}").format(units_per_millisecond, unit, "millisecond")
125  else:
126  return str("{:.2f}{:s}/{:s}").format(units_per_microsecond, unit, "microsecond")
This exception is raised whenever provided duration is zero or negative.
Definition: formatters.py:4