From 187f3e616e9f1010ee1d99fc2d84e29fcf0f3bbe Mon Sep 17 00:00:00 2001 From: Kilian Valkhof Date: Tue, 2 Feb 2010 15:02:20 +0100 Subject: [PATCH] add hurry.filesize, add gitignore to ignore pyc files --- .gitignore | 2 + hurry/__init__.py | 7 +++ hurry/filesize/README.txt | 47 ++++++++++++++++ hurry/filesize/__init__.py | 4 ++ hurry/filesize/filesize.py | 110 +++++++++++++++++++++++++++++++++++++ hurry/filesize/tests.py | 7 +++ 6 files changed, 177 insertions(+) create mode 100644 .gitignore create mode 100644 hurry/__init__.py create mode 100644 hurry/filesize/README.txt create mode 100644 hurry/filesize/__init__.py create mode 100644 hurry/filesize/filesize.py create mode 100644 hurry/filesize/tests.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f78cf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc + diff --git a/hurry/__init__.py b/hurry/__init__.py new file mode 100644 index 0000000..2e2033b --- /dev/null +++ b/hurry/__init__.py @@ -0,0 +1,7 @@ +# this is a namespace package +try: + import pkg_resources + pkg_resources.declare_namespace(__name__) +except ImportError: + import pkgutil + __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/hurry/filesize/README.txt b/hurry/filesize/README.txt new file mode 100644 index 0000000..f5aa749 --- /dev/null +++ b/hurry/filesize/README.txt @@ -0,0 +1,47 @@ +hurry.filesize +============== + +hurry.filesize a simple Python library that can take a number of bytes and +returns a human-readable string with the size in it, in kilobytes (K), +megabytes (M), etc. + +The default system it uses is "traditional", where multipliers of 1024 +increase the unit size:: + + >>> from hurry.filesize import size + >>> size(1024) + '1K' + +An alternative, slightly more verbose system:: + + >>> from hurry.filesize import alternative + >>> size(1, system=alternative) + '1 byte' + >>> size(10, system=alternative) + '10 bytes' + >>> size(1024, system=alternative) + '1 KB' + +A verbose system:: + + >>> from hurry.filesize import verbose + >>> size(10, system=verbose) + '10 bytes' + >>> size(1024, system=verbose) + '1 kilobyte' + >>> size(2000, system=verbose) + '1 kilobyte' + >>> size(3000, system=verbose) + '2 kilobytes' + >>> size(1024 * 1024, system=verbose) + '1 megabyte' + >>> size(1024 * 1024 * 3, system=verbose) + '3 megabytes' + +You can also use the SI system, where multipliers of 1000 increase the unit +size:: + + >>> from hurry.filesize import si + >>> size(1000, system=si) + '1K' + diff --git a/hurry/filesize/__init__.py b/hurry/filesize/__init__.py new file mode 100644 index 0000000..d130dac --- /dev/null +++ b/hurry/filesize/__init__.py @@ -0,0 +1,4 @@ +from hurry.filesize.filesize import size +from hurry.filesize.filesize import traditional, alternative, verbose, iec, si + + diff --git a/hurry/filesize/filesize.py b/hurry/filesize/filesize.py new file mode 100644 index 0000000..32065fe --- /dev/null +++ b/hurry/filesize/filesize.py @@ -0,0 +1,110 @@ + +traditional = [ + (1024 ** 5, 'P'), + (1024 ** 4, 'T'), + (1024 ** 3, 'G'), + (1024 ** 2, 'M'), + (1024 ** 1, 'K'), + (1024 ** 0, 'B'), + ] + +alternative = [ + (1024 ** 5, ' PB'), + (1024 ** 4, ' TB'), + (1024 ** 3, ' GB'), + (1024 ** 2, ' MB'), + (1024 ** 1, ' KB'), + (1024 ** 0, (' byte', ' bytes')), + ] + +verbose = [ + (1024 ** 5, (' petabyte', ' petabytes')), + (1024 ** 4, (' terabyte', ' terabytes')), + (1024 ** 3, (' gigabyte', ' gigabytes')), + (1024 ** 2, (' megabyte', ' megabytes')), + (1024 ** 1, (' kilobyte', ' kilobytes')), + (1024 ** 0, (' byte', ' bytes')), + ] + +iec = [ + (1024 ** 5, 'Pi'), + (1024 ** 4, 'Ti'), + (1024 ** 3, 'Gi'), + (1024 ** 2, 'Mi'), + (1024 ** 1, 'Ki'), + (1024 ** 0, ''), + ] + +si = [ + (1000 ** 5, 'P'), + (1000 ** 4, 'T'), + (1000 ** 3, 'G'), + (1000 ** 2, 'M'), + (1000 ** 1, 'K'), + (1000 ** 0, 'B'), + ] + + + +def size(bytes, system=traditional): + """Human-readable file size. + + Using the traditional system, where a factor of 1024 is used:: + + >>> size(10) + '10B' + >>> size(100) + '100B' + >>> size(1000) + '1000B' + >>> size(2000) + '1K' + >>> size(10000) + '9K' + >>> size(20000) + '19K' + >>> size(100000) + '97K' + >>> size(200000) + '195K' + >>> size(1000000) + '976K' + >>> size(2000000) + '1M' + + Using the SI system, with a factor 1000:: + + >>> size(10, system=si) + '10B' + >>> size(100, system=si) + '100B' + >>> size(1000, system=si) + '1K' + >>> size(2000, system=si) + '2K' + >>> size(10000, system=si) + '10K' + >>> size(20000, system=si) + '20K' + >>> size(100000, system=si) + '100K' + >>> size(200000, system=si) + '200K' + >>> size(1000000, system=si) + '1M' + >>> size(2000000, system=si) + '2M' + + """ + for factor, suffix in system: + if bytes >= factor: + break + amount = int(bytes/factor) + if isinstance(suffix, tuple): + singular, multiple = suffix + if amount == 1: + suffix = singular + else: + suffix = multiple + return str(amount) + suffix + diff --git a/hurry/filesize/tests.py b/hurry/filesize/tests.py new file mode 100644 index 0000000..1e89a32 --- /dev/null +++ b/hurry/filesize/tests.py @@ -0,0 +1,7 @@ +import unittest, doctest + +def test_suite(): + return unittest.TestSuite(( + doctest.DocFileSuite('README.txt'), + doctest.DocTestSuite('hurry.filesize.filesize'), + ))