Skip to content

Commit

Permalink
Merge pull request #1314 from pyiron/no_pint
Browse files Browse the repository at this point in the history
Do not import pint when it is not needed
  • Loading branch information
jan-janssen authored Feb 7, 2024
2 parents 83d9481 + f99ba0c commit 416df0b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
32 changes: 2 additions & 30 deletions pyiron_base/project/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import stat
from tqdm.auto import tqdm
import pandas
import pint
import math
import numpy as np

Expand Down Expand Up @@ -213,36 +212,9 @@ def size(self):
"""
Get the size of the project
"""
size = (
sum(
[
sum([os.path.getsize(os.path.join(path, f)) for f in files])
for path, dirs, files in os.walk(self.path)
]
)
* pint.UnitRegistry().byte
)
return self._size_conversion(size)
from pyiron_base.project.size import get_folder_size

@staticmethod
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")
return get_folder_size(path=self.path)

def copy(self):
"""
Expand Down
36 changes: 36 additions & 0 deletions pyiron_base/project/size.py
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)
3 changes: 2 additions & 1 deletion tests/project/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pint
import pickle
from pyiron_base.project.generic import Project
from pyiron_base.project.size import _size_conversion
from pyiron_base._tests import (
PyironTestCase, TestWithProject, TestWithFilledProject, ToyJob
)
Expand Down Expand Up @@ -90,7 +91,7 @@ def test__size_conversion(self):
byte = pint.UnitRegistry().byte
for value in conv_check.keys():
with self.subTest(value):
converted_size = self.project._size_conversion(value * byte)
converted_size = _size_conversion(value * byte)
self.assertEqual(converted_size.m, conv_check[value][0])
self.assertEqual(str(converted_size.u), conv_check[value][1])

Expand Down

0 comments on commit 416df0b

Please sign in to comment.