Skip to content

Instantly share code, notes, and snippets.

@n0nuser
Last active February 23, 2022 17:19
Show Gist options
  • Save n0nuser/32c5450378d276b9ccc7f3f2f3609379 to your computer and use it in GitHub Desktop.
Save n0nuser/32c5450378d276b9ccc7f3f2f3609379 to your computer and use it in GitHub Desktop.
Python functions
def randomPassword(length):
"""
pass = randomPassword(16)
Ref: https://stackoverflow.com/a/2257449
"""
return(''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length)))
def _get_size(bytes: int, suffix="B"):
"""Scale bytes to its proper format.
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
Args:
bytes (int): Numeric size to format
suffix (str): Format of introduced size: 'B', 'K', 'M', 'G', 'T', 'P'
Returns:
string: Formatted size
"""
factor = 1024
for unit in ["", "K", "M", "G", "T", "P"]:
if bytes < factor:
return f"{bytes:.2f}{unit}{suffix}"
bytes /= factor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment