Commit 0ad47f89 by Peter Weidenbach

hurry.filesize replaced with bitmath

parent 3de2ab42
...@@ -3,7 +3,7 @@ from .file_functions import read_in_chunks, get_directory_for_filename, create_d ...@@ -3,7 +3,7 @@ from .file_functions import read_in_chunks, get_directory_for_filename, create_d
from .git_functions import get_version_string_from_git from .git_functions import get_version_string_from_git
__version__ = '0.1.9' __version__ = '0.2'
__all__ = [ __all__ = [
'get_directory_for_filename', 'get_directory_for_filename',
......
import os import os
from hurry.filesize import size, alternative import bitmath
def read_in_chunks(file_object, chunk_size=1024): def read_in_chunks(file_object, chunk_size=1024):
""" '''
Helper function to read large file objects iteratively in smaller chunks. Can be used like this:: Helper function to read large file objects iteratively in smaller chunks. Can be used like this::
file_object = open('somelargefile.xyz', 'rb') file_object = open('somelargefile.xyz', 'rb')
...@@ -14,7 +14,7 @@ def read_in_chunks(file_object, chunk_size=1024): ...@@ -14,7 +14,7 @@ def read_in_chunks(file_object, chunk_size=1024):
:param chunk_size: Number of bytes to read per chunk. :param chunk_size: Number of bytes to read per chunk.
:type chunk_size: int :type chunk_size: int
:return: Returns a generator to iterate over all chunks, see above for usage. :return: Returns a generator to iterate over all chunks, see above for usage.
""" '''
while True: while True:
data = file_object.read(chunk_size) data = file_object.read(chunk_size)
if not data: if not data:
...@@ -23,34 +23,35 @@ def read_in_chunks(file_object, chunk_size=1024): ...@@ -23,34 +23,35 @@ def read_in_chunks(file_object, chunk_size=1024):
def get_directory_for_filename(filename): def get_directory_for_filename(filename):
""" '''
Convenience function which returns the absolute path to the directory that contains the given file name. Convenience function which returns the absolute path to the directory that contains the given file name.
:param filename: Path of the file. Can be absolute or relative to the current directory. :param filename: Path of the file. Can be absolute or relative to the current directory.
:type filename: str :type filename: str
:return: Absolute path of the directory :return: Absolute path of the directory
""" '''
return os.path.dirname(os.path.abspath(filename)) return os.path.dirname(os.path.abspath(filename))
def create_dir_for_file(file_path): def create_dir_for_file(file_path):
""" '''
Creates all directories of file path. File path may include the file as well. Creates all directories of file path. File path may include the file as well.
:param file_path: Path of the file. Can be absolute or relative to the current directory. :param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str :type file_path: str
:return: None :return: None
""" '''
directory = os.path.dirname(os.path.abspath(file_path)) directory = os.path.dirname(os.path.abspath(file_path))
os.makedirs(directory, exist_ok=True) os.makedirs(directory, exist_ok=True)
def human_readable_file_size(size_in_bytes): def human_readable_file_size(size_in_bytes):
""" '''
Returns a nicly human readable file size Returns a nicly human readable file size
:param size_in_bytes: Size in Bytes :param size_in_bytes: Size in Bytes
:type size_in_bytes: int :type size_in_bytes: int
:return: str :return: str
""" '''
return size(size_in_bytes, system=alternative) tmp = bitmath.Byte(bytes=size_in_bytes).best_prefix()
return tmp.format('{value:.2f} {unit}')
hurry.filesize
\ No newline at end of file
...@@ -2,15 +2,15 @@ from setuptools import setup, find_packages ...@@ -2,15 +2,15 @@ from setuptools import setup, find_packages
from common_helper_files import __version__ from common_helper_files import __version__
setup( setup(
name="common_helper_files", name='common_helper_files',
version=__version__, version=__version__,
packages=find_packages(), packages=find_packages(),
install_requires=[ install_requires=[
'hurry.filesize' 'bitmath'
], ],
description="file operation helper functions", description='file operation helper functions',
author="Fraunhofer FKIE", author='Fraunhofer FKIE',
author_email="peter.weidenbach@fkie.fraunhofer.de", author_email='peter.weidenbach@fkie.fraunhofer.de',
url="http://www.fkie.fraunhofer.de", url='http://www.fkie.fraunhofer.de',
license="GPL-3.0" license='GPL-3.0'
) )
...@@ -5,8 +5,5 @@ from common_helper_files import human_readable_file_size ...@@ -5,8 +5,5 @@ from common_helper_files import human_readable_file_size
class Test_file_functions(unittest.TestCase): class Test_file_functions(unittest.TestCase):
def test_human_readable_file_size(self): def test_human_readable_file_size(self):
self.assertEqual(human_readable_file_size(1024), "1 KB") self.assertEqual(human_readable_file_size(1024), '1.00 KiB')
self.assertEqual(human_readable_file_size(5000), '4.88 KiB')
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment