-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1314 from pyiron/no_pint
Do not import pint when it is not needed
- Loading branch information
Showing
3 changed files
with
40 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import math | ||
import os | ||
import pint | ||
|
||
|
||
def _size_conversion(size: pint.Quantity): | ||
sign_prefactor = 1 | ||
if size < 0: | ||
sign_prefactor = -1 | ||
size *= -1 | ||
elif size == 0: | ||
return size | ||
|
||
prefix_index = math.floor(math.log2(size) / 10) - 1 | ||
prefix = ["Ki", "Mi", "Gi", "Ti", "Pi"] | ||
|
||
size *= sign_prefactor | ||
if prefix_index < 0: | ||
return size | ||
elif prefix_index < 5: | ||
return size.to(f"{prefix[prefix_index]}byte") | ||
else: | ||
return size.to(f"{prefix[-1]}byte") | ||
|
||
|
||
def get_folder_size(path): | ||
size = ( | ||
sum( | ||
[ | ||
sum([os.path.getsize(os.path.join(path, f)) for f in files]) | ||
for path, dirs, files in os.walk(path) | ||
] | ||
) | ||
* pint.UnitRegistry().byte | ||
) | ||
return _size_conversion(size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters