Skip to content
Myr

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

3 files+20 added−19 removed

modified:jinja2/testsuite/filters.py+4 added-4 removed
82 unmodified lines
83 '{{ 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 ))9192 def test_first(self):93 tmpl = env.from_string('{{ foo|first }}')94 out = tmpl.render(foo=range(10))
262 unmodified lines
modified:jinja2/filters.py+15 added-15 removed
13 unmodified lines
14from operator import itemgetter15from itertools import imap, groupby16from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode17from jinja2.runtime import Undefined18from jinja2.exceptions import FilterArgumentError, SecurityError18from jinja2.exceptions import FilterArgumentError192021_word_re = re.compile(r'\w+(?u)')22
322 unmodified lines
345 """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)368369370def do_pprint(value, verbose=False):371 """Pretty print a variable. Useful for debugging.
430 unmodified lines
modified:CHANGES+1 added
6 unmodified lines
78- 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.1213Version 2.614-----------15(codename Convolution, released on July 24th 2011)
263 unmodified lines