Anonymous avatar Anonymous committed 1d71c23

Put number_to_human_size helper in unfinished directory for legacy
applications.

Comments (0)

Files changed (3)

   - Disable ``convert_misc_characters``: it fails its doctests and there's
     no consensus on what it should do.
 
+* "number_to_human_size.py" is in unfinished directory. This is an old rails
+  helper from WebHelpers 0.6.4. It's here pending a more comprehensive helper;
+  see http://bitbucket.org/bbangert/webhelpers/issue/2/reinstate-number_to_human_size
+
 1.0b4 (2010-01-24)
 ------------------
 

docs/whats_new.rst

 helpers was replaced by new helpers in the ``date``, ``html``, ``misc``,
 ``number``, and ``text`` packages. Prototype and Scriptaculous are not
 replaced; WebHelpers no longer ships with Javascript libraries.  ``pagination``
-was replaced by ``paginate``.  If you can't switch to the replacement helpers,
+was replaced by ``paginate``.  ``number_to_human_size()`` is in the unfinished
+directory in the source distribution; you can copy it to your application if
+you need it.  If you can't switch to the replacement helpers,
 stick with WebHelpers 0.6.4.
 
 secure_form

unfinished/number_to_human_size.py

+"""number_to_human_size() helper from WebHelpers 0.6.4
+
+If you need the existing function, copy this file to your application.
+We are working on a more comprehensive alternative, see
+http://bitbucket.org/bbangert/webhelpers/issue/2/reinstate-number_to_human_size
+"""
+
+def number_to_human_size(size, precision=1):
+    """
+    Return a formatted-for-humans file size.
+
+    ``precision``
+        The level of precision, defaults to 1
+    
+    Examples::
+    
+        >>> number_to_human_size(123)
+        '123 Bytes'
+        >>> number_to_human_size(1234)
+        '1.2 KB'
+        >>> number_to_human_size(12345)
+        '12.1 KB'
+        >>> number_to_human_size(1234567)
+        '1.2 MB'
+        >>> number_to_human_size(1234567890)
+        '1.1 GB'
+        >>> number_to_human_size(1234567890123)
+        '1.1 TB'
+        >>> number_to_human_size(1234567, 2)
+        '1.18 MB'
+        
+    """
+    if size == 1:
+        return "1 Byte"
+    elif size < 1024:
+        return "%d Bytes" % size
+    elif size < (1024**2):
+        return ("%%.%if KB" % precision) % (size / 1024.00)
+    elif size < (1024**3):
+        return ("%%.%if MB" % precision) % (size / 1024.00**2)
+    elif size < (1024**4):
+        return ("%%.%if GB" % precision) % (size / 1024.00**3)
+    elif size < (1024**5):
+        return ("%%.%if TB" % precision) % (size / 1024.00**4)
+    else:
+        return ""
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.