Skip to content

Instantly share code, notes, and snippets.

@beugley
Created August 24, 2016 17:51
Show Gist options
  • Save beugley/ccd69945346759eb6142272a6d69b4e0 to your computer and use it in GitHub Desktop.
Save beugley/ccd69945346759eb6142272a6d69b4e0 to your computer and use it in GitHub Desktop.
Python function to convert a human-readable byte string (e.g. 2G, 10GB, 30MB, 20KB) to a number of bytes
def human_readable_to_bytes(size):
"""Given a human-readable byte string (e.g. 2G, 10GB, 30MB, 20KB),
return the number of bytes. Will return 0 if the argument has
unexpected form.
"""
if (size[-1] == 'B'):
size = size[:-1]
if (size.isdigit()):
bytes = int(size)
else:
bytes = size[:-1]
unit = size[-1]
if (bytes.isdigit()):
bytes = int(bytes)
if (unit == 'G'):
bytes *= 1073741824
elif (unit == 'M'):
bytes *= 1048576
elif (unit == 'K'):
bytes *= 1024
else:
bytes = 0
else:
bytes = 0
return bytes,size+'B'
@jaepetto
Copy link

Based on your code, I managed to handle the way docker returns the sizes as follows:

def human_readable_to_bytes(size):
    """Given a human-readable byte string (e.g. 2G, 30M, 20K),
    return the number of bytes.  Will raise an exception if the argument has
    unexpected form.
    """
    # Try to parse the size as if the unit was coded on 1 char.
    try:
        numeric_size = float(size[:-1])
        unit = size[-1]
    except ValueError:
        try:
          # Try to parse the size as if the unit was coded on 2 chars.
          numeric_size = float(size[:-2])
          unit = size[-2:-1]
        except ValueError:
          raise ValueError("Can't convert %r to bytes" % size)

    unit = unit.upper()

    # Now we have a numeric value and a unit. Check the unit and convert to bytes.
    if unit == "G":
        bytes = numeric_size * 1073741824
    elif unit == "M":
        bytes = numeric_size * 1048576
    elif unit == "K":
        bytes = numeric_size * 1024
    else:
        bytes = numeric_size

    return int(bytes), f"{int(numeric_size)}{unit}B"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment