Skip to content

Instantly share code, notes, and snippets.

@Igaryu
Last active April 16, 2023 09:21
Show Gist options
  • Save Igaryu/6076f080bf388d7616dbb215210bfe66 to your computer and use it in GitHub Desktop.
Save Igaryu/6076f080bf388d7616dbb215210bfe66 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Why importing tons of functions, when I can import the only the 6 I need?
from sys import exit
from random import choice
from string import ascii_letters, digits, punctuation
#
# Using try - except trap to verify if input data is a number
# if not then exit with exit code -9
#
try:
password_length=int(input('\nDigit the lentgh of password to generate: '))
except:
print("\nA number whas required!!\n")
exit(-9)
#
# A faster way to concatenate strings is .join() the + method is really slow and deprecated
#
password_characters=''.join([ascii_letters,digits,punctuation])
password = []
for x in range(password_length):
password.append(choice(password_characters))
#
# using the f-string is more python in printing in version 3
#
print(f"\n{''.join(password)} \n")
@Igaryu
Copy link
Author

Igaryu commented Jul 1, 2020

A lighter version could be obtained using a constant and not importing libraries like in this git:

https://gist.github.com/Igaryu/a0498c843a10f3d502d78a862bd7f2db

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