Globals: added the dataString() function to format a size into a human readable string. conda

Tue, 29 Jan 2019 20:21:26 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 29 Jan 2019 20:21:26 +0100
branch
conda
changeset 6682
4326e802ff56
parent 6681
9c1513b488ef
child 6683
aca9d39fbfbd

Globals: added the dataString() function to format a size into a human readable string.

Globals/__init__.py file | annotate | diff | comparison | revisions
--- a/Globals/__init__.py	Tue Jan 29 19:48:13 2019 +0100
+++ b/Globals/__init__.py	Tue Jan 29 20:21:26 2019 +0100
@@ -412,6 +412,32 @@
     return QByteArray(txt)
 
 
+def dataString(size):
+    """
+    Module function to generate a formatted size string.
+    
+    @param size size to be formatted
+    @type int
+    @return formatted data string
+    @rtype str
+    """
+    if size < 1024:
+        return QCoreApplication.translate(
+            "Globals", "{0:.1f} Bytes").format(size)
+    elif size < 1024 * 1024:
+        size /= 1024
+        return QCoreApplication.translate(
+            "Globals", "{0:.1f} KiB").format(size)
+    elif size < 1024 * 1024 * 1024:
+        size /= 1024 * 1024
+        return QCoreApplication.translate(
+            "Globals", "{0:.2f} MiB").format(size)
+    else:
+        size /= 1024 * 1024 * 1024
+        return QCoreApplication.translate(
+            "Globals", "{0:.2f} GiB").format(size)
+
+
 ###############################################################################
 ## functions for converting QSetting return types to valid types
 ###############################################################################

eric ide

mercurial