Add a format_utils module to the common package
authorAntoine Busque <abusque@efficios.com>
Fri, 15 Jan 2016 21:44:34 +0000 (16:44 -0500)
committerAntoine Busque <abusque@efficios.com>
Thu, 11 Feb 2016 18:58:52 +0000 (13:58 -0500)
Signed-off-by: Antoine Busque <abusque@efficios.com>
lttnganalyses/common/format_utils.py [new file with mode: 0644]

diff --git a/lttnganalyses/common/format_utils.py b/lttnganalyses/common/format_utils.py
new file mode 100644 (file)
index 0000000..9d8224f
--- /dev/null
@@ -0,0 +1,68 @@
+# The MIT License (MIT)
+#
+# Copyright (C) 2016 - Antoine Busque <abusque@efficios.com>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+import math
+
+
+def format_size(size, binary_prefix=True):
+    """Convert an integral number of bytes to a human-readable string
+
+    Args:
+        size (int): a non-negative number of bytes
+        binary_prefix (bool, optional): whether to use binary units
+            prefixes, over SI prefixes. default: True
+
+    Returns:
+        The formatted string comprised of the size and units
+
+    Raises:
+        ValueError: if size < 0
+    """
+    if size < 0:
+        raise ValueError('Cannot format negative size')
+
+    if binary_prefix:
+        base = 1024
+        units = ['  B', 'KiB', 'MiB', 'GiB','TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
+    else:
+        base = 1000
+        units = [' B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
+
+    if size == 0:
+        exponent = 0
+    else:
+        exponent = int(math.log(size, base))
+        if exponent >= len(units):
+            # Don't try and use a unit above YiB/YB
+            exponent = len(units) - 1
+
+        size /= math.pow(base, exponent)
+
+    unit = units[exponent]
+
+    if exponent == 0:
+        # Don't display fractions of a byte
+        format_str = '{:0.0f} {}'
+    else:
+        format_str = '{:0.2f} {}'
+
+    return format_str.format(size, unit)
This page took 0.026328 seconds and 5 git commands to generate.