Fixed filesizeformat
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 1898c24 · Browse files · 3 files·+20 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+20 added−19 removed
- other:+20 added−19 removed
Provenance
- Commit
b1b7b08- Tree
25720da- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1898c24
3 files changed+20 added−19 removed
modified:jinja2/testsuite/filters.py+4 added-4 removed
82 unmodified lines83 '{{ 1000000000|filesizeformat(true) }}|'84 '{{ 1000000000000|filesizeformat(true) }}'85 )86 out = tmpl.render()87- assert out == (88- '100 Bytes|0.0 kB|0.0 MB|0.0 GB|0.0 TB|100 Bytes|'89- '1000 Bytes|1.0 KiB|0.9 MiB|0.9 GiB'90- )87+ self.assert_equal(out, (88+ '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'89+ '1000 Bytes|1.0 MiB|0.9 GiB|0.9 TiB'90+ ))91 92 def test_first(self):93 tmpl = env.from_string('{{ foo|first }}')94 out = tmpl.render(foo=range(10))262 unmodified linesmodified:jinja2/filters.py+15 added-15 removed
13 unmodified lines14 from operator import itemgetter15 from itertools import imap, groupby16 from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode17 from jinja2.runtime import Undefined18-from jinja2.exceptions import FilterArgumentError, SecurityError18+from jinja2.exceptions import FilterArgumentError19 20 21 _word_re = re.compile(r'\w+(?u)')22 322 unmodified lines345 """346 bytes = float(value)347 base = binary and 1024 or 1000348 prefixes = [349- (binary and "KiB" or "kB"),350- (binary and "MiB" or "MB"),351- (binary and "GiB" or "GB"),352- (binary and "TiB" or "TB"),353- (binary and "PiB" or "PB"),354- (binary and "EiB" or "EB"),355- (binary and "ZiB" or "ZB"),356- (binary and "YiB" or "YB")349+ (binary and 'KiB' or 'kB'),350+ (binary and 'MiB' or 'MB'),351+ (binary and 'GiB' or 'GB'),352+ (binary and 'TiB' or 'TB'),353+ (binary and 'PiB' or 'PB'),354+ (binary and 'EiB' or 'EB'),355+ (binary and 'ZiB' or 'ZB'),356+ (binary and 'YiB' or 'YB')357 ]358 if bytes == 1:359- return "1 Byte"359+ return '1 Byte'360 elif bytes < base:361- return "%d Bytes" % bytes361+ return '%d Bytes' % bytes362 else:363 for i, prefix in enumerate(prefixes):364- unit = base * base ** (i + 1)365- if bytes < unit:366- return "%.1f %s" % ((bytes / unit), prefix)367- return "%.1f %s" % ((bytes / unit), prefix)364+ unit = base ** (i + 1)365+ if bytes <= unit:366+ return '%.1f %s' % ((bytes / unit), prefix)367+ return '%.1f %s' % ((bytes / unit), prefix)368 369 370 def do_pprint(value, verbose=False):371 """Pretty print a variable. Useful for debugging.430 unmodified linesmodified:CHANGES+1 added
6 unmodified lines7 8 - Choice and prefix loaders now dispatch source and template lookup9 separately in order to work in combination with module loaders as10 advertised.11+- Fixed filesizeformat.12 13 Version 2.614 -----------15 (codename Convolution, released on July 24th 2011)263 unmodified lines