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"

@Hamzajani89
Copy link

{
"Browser History": [
{
"page_transition": "LINK",
"title": "Google Takeout",
"ptoken": {},
"url": "https://takeout.google.com/takeout/custom/chrome?hl=en-GB&continue=https://myaccount.google.com/dashboard&pli=1",
"client_id": "r0Ye2TDehbGyhWk9nLjzhw==",
"time_usec": 1720603398797893
},
{
"favicon_url": "https://www.gstatic.com/images/branding/product/2x/contacts_2022_96dp.png",
"page_transition": "LINK",
"title": "Google Contacts",
"ptoken": {},
"url": "https://contacts.google.com/u/1/?hl=en-GB&pli=1&pageId=none",
"client_id": "r0Ye2TDehbGyhWk9nLjzhw==",
"time_usec": 1720603379364260
},
{
"page_transition": "LINK",
"title": "Google Contacts",
"ptoken": {},
"url": "https://contacts.google.com/u/1/?hl=en-GB&pli=1&pageId=none",
"client_id": "r0Ye2TDehbGyhWk9nLjzhw==",
"time_usec": 1720603379034161
},
{
"favicon_url": "https://www.gstatic.com/images/branding/product/2x/contacts_2022_96dp.png",
"page_transition": "LINK",
"title": "Merge and fix",
"ptoken": {},
"url": "https://contacts.google.com/suggestions?hl=en-GB",
"client_id": "r0Ye2TDehbGyhWk9nLjzhw==",
"time_usec": 1720603370379167
},
{
"favicon_

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