|
18 | 18 | import os |
19 | 19 | import pathlib |
20 | 20 | import random |
21 | | -from typing import List, TextIO |
| 21 | +from typing import List, TextIO, Union |
22 | 22 |
|
23 | 23 | import portalocker |
24 | 24 |
|
@@ -59,7 +59,10 @@ def chunks(iterator, n): |
59 | 59 | yield itertools.chain([first], rest_of_chunk) |
60 | 60 |
|
61 | 61 |
|
62 | | -def load_json(path: str, default=None, lock=False, display_warning=True): |
| 62 | +def load_json(path: Union[str, pathlib.Path], |
| 63 | + default=None, |
| 64 | + lock=False, |
| 65 | + display_warning=True): |
63 | 66 | """ |
64 | 67 | Load the contents of the given file as a JSON and return it's value, |
65 | 68 | or default if the file can't be loaded. |
@@ -218,3 +221,16 @@ def generate_random_token(num_bytes: int = 32) -> str: |
218 | 221 | for _ in range(0, -(num_bytes // -64))]) |
219 | 222 | idx = random.randrange(0, len(hash_value) - num_bytes + 1) |
220 | 223 | return hash_value[idx:(idx + num_bytes)] |
| 224 | + |
| 225 | + |
| 226 | +def format_size(num: float, suffix: str = 'B') -> str: |
| 227 | + """ |
| 228 | + Pretty print storage units. |
| 229 | + Source: http://stackoverflow.com/questions/1094841/ |
| 230 | + reusable-library-to-get-human-readable-version-of-file-size |
| 231 | + """ |
| 232 | + for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi', 'Ri']: |
| 233 | + if abs(num) < 1024.0: |
| 234 | + return f"{num:3.1f} {unit}{suffix}" |
| 235 | + num /= 1024.0 |
| 236 | + return f"{num:.1f} Qi{suffix}" |
0 commit comments