Skip to content

Commit

Permalink
Fix units in fs.filesize (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos authored and willmcgugan committed Oct 6, 2017
1 parent 1cbe560 commit d22fba6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
29 changes: 23 additions & 6 deletions fs/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from __future__ import division
from __future__ import unicode_literals

__all__ = ['traditional', 'decimal']
__all__ = ['traditional', 'decimal', 'binary']



def _to_str(size, suffixes, base):
try:
size = int(size)
except ValueError:
raise ValueError(
raise TypeError(
"filesize requires a numeric value, not {!r}".format(size)
)
if size == 1:
Expand All @@ -35,7 +35,7 @@ def _to_str(size, suffixes, base):
def traditional(size):
"""
Convert a filesize in to a string representation with traditional
(base 2) units.
(base 2) units and JDEC prefixes.
:param int size: A file size.
:returns: A string containing a abbreviated file size and units.
Expand All @@ -49,18 +49,35 @@ def traditional(size):
)


def binary(size):
"""
Convert a filesize in to a string representation with binary units
and SI binary prefixes.
:param int size: A file size.
:param bool si: True to use SI prefixes, False to use JDEC prefixes.
:returns: A string containing a abbreviated file size and units.
:rtype str:
"""
return _to_str(
size,
('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'),
1024
)


def decimal(size):
"""
Convert a filesize in to a string representation with decimal
units.
units and SI decimal prefixes.
:param int size: A file size.
:returns: A string containing a abbreviated file size and units.
:rtype str:
"""

return _to_str(
size,
('kbit', 'Mbit', 'Gbit', 'Tbit', 'Pbit', 'Ebit', 'Zbit', 'Ybit'),
('kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
1000
)
48 changes: 42 additions & 6 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@ def test_traditional(self):
'1.5 MB'
)

def test_binary(self):

self.assertEqual(
filesize.binary(0),
'0 bytes'
)
self.assertEqual(
filesize.binary(1),
'1 byte'
)
self.assertEqual(
filesize.binary(2),
'2 bytes'
)
self.assertEqual(
filesize.binary(1024),
'1.0 KiB'
)

self.assertEqual(
filesize.binary(1024 * 1024),
'1.0 MiB'
)

self.assertEqual(
filesize.binary(1024 * 1024 + 1),
'1.0 MiB'
)

self.assertEqual(
filesize.binary(1.5 * 1024 * 1024),
'1.5 MiB'
)



def test_decimal(self):

self.assertEqual(
Expand All @@ -57,25 +93,25 @@ def test_decimal(self):
)
self.assertEqual(
filesize.decimal(1000),
'1.0 kbit'
'1.0 kB'
)

self.assertEqual(
filesize.decimal(1000 * 1000),
'1.0 Mbit'
'1.0 MB'
)

self.assertEqual(
filesize.decimal(1000 * 1000 + 1),
'1.0 Mbit'
'1.0 MB'
)

self.assertEqual(
filesize.decimal(1200 * 1000),
'1.2 Mbit'
'1.2 MB'
)

def test_errors(self):

with self.assertRaises(ValueError):
filesize.traditional('foo')
with self.assertRaises(TypeError):
filesize.traditional('foo')

0 comments on commit d22fba6

Please sign in to comment.