Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Human-friendly file-size utils #13

Open
thclark opened this issue Oct 1, 2020 · 0 comments
Open

Human-friendly file-size utils #13

thclark opened this issue Oct 1, 2020 · 0 comments
Assignees
Labels
backend Related to the back end feature A new feature of the app quirk Quirky UX or styling problems that aren't major but reflect poorly on us

Comments

@thclark
Copy link
Contributor

thclark commented Oct 1, 2020

Code presently in amy.utils.files should be refactored to the Octue SDK utils, allowing us to present friendly file sizes for datasets and datafiles:

def size_kb(size_bytes):
    if not size_bytes:
        return 0
    return size_bytes / 1024


def size_mb(size_bytes):
    if not size_bytes:
        return 0
    return size_bytes / 1048576


def size_gb(size_bytes):
    if not size_bytes:
        return 0
    return size_bytes / 1073741824


def size_tb(size_bytes):
    if not size_bytes:
        return 0
    return size_bytes / 1099511627776


def size_str(size_bytes, fmt='%.02f '):
    """ Return sensible human formatted size string
    :param size_bytes: file/dataset size in bytes
    :param fmt: string format specifier for the floating point size. default '%.02f '
    :return: str
    """
    if size_bytes >= 1099511627776:
        return fmt % size_tb(size_bytes) + 'tb'
    if size_bytes >= 1073741824:
        return fmt % size_gb(size_bytes) + 'gb'
    if size_bytes >= 1048576:
        return fmt % size_mb(size_bytes) + 'mb'
    if size_bytes >= 1024:
        return fmt % size_kb(size_bytes) + 'kb'
    return fmt % size_bytes + 'b'
@thclark thclark self-assigned this Oct 1, 2020
@thclark thclark added backend Related to the back end feature A new feature of the app labels Oct 1, 2020
@thclark thclark added the quirk Quirky UX or styling problems that aren't major but reflect poorly on us label Oct 1, 2020
@thclark thclark moved this to Priority 1 (Low) in Octue Board Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to the back end feature A new feature of the app quirk Quirky UX or styling problems that aren't major but reflect poorly on us
Projects
Status: Priority 1 (Low)
Development

No branches or pull requests

1 participant