Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixed broken filesizeformat #53

Closed
wants to merge 2 commits into from

3 participants

@marcusti

value of filesizeformat was 10**3 too low
fixed filter and corresponding unittest

@ThomasWaldmann

more as a general comment than relating to this pull request:

maybe the code could be made much easier if the prefixes list would also contain "B" (abbreviation for "Byte") as first entry (at index 0), which directly corresponds to 1000^0 and 1024^0.

Then, "1B" as well as "2B" works out of the box, no need for specialcasing the singular/plural of the long form ("1 Byte", "2 Bytes").

Also, unit should be renamed to bigger_unit or next_unit, to make the code more clear.

@mitsuhiko mitsuhiko closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 5, 2011
  1. @marcusti

    fix filesizeformat filter

    marcusti authored
Commits on Aug 6, 2011
  1. @marcusti
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 14 deletions.
  1. +4 −4 jinja2/filters.py
  2. +13 −10 jinja2/testsuite/filters.py
View
8 jinja2/filters.py
@@ -337,7 +337,7 @@ def do_random(environment, seq):
return environment.undefined('No random item, sequence was empty.')
-def do_filesizeformat(value, binary=False):
+def do_filesizeformat(value, binary=False, precision=1):
"""Format the value like a 'human-readable' file size (i.e. 13 kB,
4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega,
Giga, etc.), if the second parameter is set to `True` the binary
@@ -361,10 +361,10 @@ def do_filesizeformat(value, binary=False):
return "%d Bytes" % bytes
else:
for i, prefix in enumerate(prefixes):
- unit = base * base ** (i + 1)
+ unit = base ** (i + 2)
if bytes < unit:
- return "%.1f %s" % ((bytes / unit), prefix)
- return "%.1f %s" % ((bytes / unit), prefix)
+ return "%.*f %s" % (precision, base * bytes / unit, prefix)
+ return "%.*f %s" % (precision, base * bytes / unit, prefix)
def do_pprint(value, verbose=False):
View
23 jinja2/testsuite/filters.py
@@ -73,20 +73,23 @@ def test_striptags(self):
def test_filesizeformat(self):
tmpl = env.from_string(
'{{ 100|filesizeformat }}|'
- '{{ 1000|filesizeformat }}|'
- '{{ 1000000|filesizeformat }}|'
- '{{ 1000000000|filesizeformat }}|'
- '{{ 1000000000000|filesizeformat }}|'
- '{{ 100|filesizeformat(true) }}|'
+ '{{ (10**3)|filesizeformat }}|'
+ '{{ (10**6)|filesizeformat }}|'
+ '{{ (10**9)|filesizeformat }}|'
+ '{{ (10**12)|filesizeformat }}|'
'{{ 1000|filesizeformat(true) }}|'
- '{{ 1000000|filesizeformat(true) }}|'
- '{{ 1000000000|filesizeformat(true) }}|'
- '{{ 1000000000000|filesizeformat(true) }}'
+ '{{ (2**10)|filesizeformat(true) }}|'
+ '{{ (2**20)|filesizeformat(true) }}|'
+ '{{ (2**30)|filesizeformat(true) }}|'
+ '{{ (2**40)|filesizeformat(true) }}|'
+ '{{ (10**3)|filesizeformat(binary=false, precision=2) }}|'
+ '{{ (2**10)|filesizeformat(binary=true, precision=2) }}'
)
out = tmpl.render()
assert out == (
- '100 Bytes|0.0 kB|0.0 MB|0.0 GB|0.0 TB|100 Bytes|'
- '1000 Bytes|1.0 KiB|0.9 MiB|0.9 GiB'
+ '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|'
+ '1000 Bytes|1.0 KiB|1.0 MiB|1.0 GiB|1.0 TiB|'
+ '1.00 kB|1.00 KiB'
)
def test_first(self):
Something went wrong with that request. Please try again.