Last active
February 23, 2022 17:19
-
-
Save n0nuser/32c5450378d276b9ccc7f3f2f3609379 to your computer and use it in GitHub Desktop.
Python functions
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 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