Created
August 24, 2016 17:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{
"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_