-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathfilesize.py
More file actions
126 lines (89 loc) · 3.66 KB
/
Copy pathfilesize.py
File metadata and controls
126 lines (89 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# coding: utf-8
"""Functions for reporting filesizes.
The functions declared in this module should cover the different
usecases needed to generate a string representation of a file size
using several different units. Since there are many standards regarding
file size units, three different functions have been implemented.
See Also:
* `Wikipedia: Binary prefix <https://en.wikipedia.org/wiki/Binary_prefix>`_
"""
from __future__ import division, unicode_literals
import typing
if typing.TYPE_CHECKING:
from typing import Iterable, SupportsInt, Text
__all__ = ["traditional", "decimal", "binary"]
def _to_str(size, suffixes, base):
# type: (SupportsInt, Iterable[Text], int) -> Text
try:
size = int(size)
except ValueError:
raise TypeError("filesize requires a numeric value, not {!r}".format(size))
if size == 1:
return "1 byte"
elif size < base:
return "{:,} bytes".format(size)
suffixes = list(suffixes)
for i, suffix in enumerate(suffixes, 2): # noqa: B007
unit = base**i
if size < unit:
break
mantissa = base * size / unit
# Rounding can push the mantissa to `base` (e.g. 999,999 B is 999.999 kB,
# which formats as "1,000.0 kB"). Carry into the next suffix instead.
if round(mantissa, 1) >= base and i - 1 < len(suffixes):
suffix = suffixes[i - 1]
mantissa = size / unit
return "{:,.1f} {}".format(mantissa, suffix)
def traditional(size):
# type: (SupportsInt) -> Text
"""Convert a filesize in to a string (powers of 1024, JDEC prefixes).
In this convention, ``1024 B = 1 KB``.
This is the format that was used to display the size of DVDs
(*700 MB* meaning actually about *734 003 200 bytes*) before
standardisation of IEC units among manufacturers, and still
used by **Windows** to report the storage capacity of hard
drives (*279.4 GB* meaning *279.4 × 1024³ bytes*).
Arguments:
size (int): A file size.
Returns:
`str`: A string containing an abbreviated file size and units.
Example:
>>> fs.filesize.traditional(30000)
'29.3 KB'
"""
return _to_str(size, ("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"), 1024)
def binary(size):
# type: (SupportsInt) -> Text
"""Convert a filesize in to a string (powers of 1024, IEC prefixes).
In this convention, ``1024 B = 1 KiB``.
This is the format that has gained adoption among manufacturers
to avoid ambiguity regarding size units, since it explicitly states
using a binary base (*KiB = kibi bytes = kilo binary bytes*).
This format is notably being used by the **Linux** kernel (see
``man 7 units``).
Arguments:
int (size): A file size.
Returns:
`str`: A string containing a abbreviated file size and units.
Example:
>>> fs.filesize.binary(30000)
'29.3 KiB'
"""
return _to_str(size, ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"), 1024)
def decimal(size):
# type: (SupportsInt) -> Text
"""Convert a filesize in to a string (powers of 1000, SI prefixes).
In this convention, ``1000 B = 1 kB``.
This is typically the format used to advertise the storage
capacity of USB flash drives and the like (*256 MB* meaning
actually a storage capacity of more than *256 000 000 B*),
or used by **Mac OS X** since v10.6 to report file sizes.
Arguments:
int (size): A file size.
Returns:
`str`: A string containing a abbreviated file size and units.
Example:
>>> fs.filesize.decimal(30000)
'30.0 kB'
"""
return _to_str(size, ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"), 1000)