Skip to content

Commit

Permalink
Rewrote code, type hints, try except on request.get()
Browse files Browse the repository at this point in the history
Added type hints
Changed the contents of tuples to now include string module ASCII methods
Reworked how password arguments are used, the list of characters that can be chosen is now dynamic and will change depending on the arguments
Changed most arguments to bool instead of a string being 'true' or 'false'
Added try except block (haven't tested exceptions yet) to prevent crashing if the user doesn't have an internet connection
Fixed the max length of passphrase not being 50 (previously was 250)
  • Loading branch information
Wolfmyths authored Jan 25, 2023
1 parent fd63a7e commit 260f751
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions PasswordGenerator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import random
import requests
import string
import urllib3 # Added so py installer could find the correct modules
class password:
def __init__(self, strength='strong', length=0, capital='true'):
self.strength = strength
def __init__(self, symbols: bool = True, length: int = 0, capital: bool = True) -> None:
self.symbolsBool = symbols
self.length = length
self.capital = capital
self.alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
self.symbols = ('!', '@', '#', '$', '&', '_')
self.number = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
self.charChoice = (self.alphabet, self.number, self.symbols)
self.capitalBool = capital
self.lowerCase = tuple(string.ascii_lowercase)
self.upperCase = tuple(string.ascii_uppercase)
self.symbols = tuple(string.punctuation)
self.number = tuple(string.digits)
self.charChoice = [self.lowerCase, self.number]

def get_pass(self):
if self.symbolsBool:
self.charChoice.append(self.symbols)
if self.capitalBool:
self.charChoice.append(self.upperCase)


def get_pass(self) -> str:
password = ''

length = int(self.length)
Expand All @@ -22,43 +30,49 @@ def get_pass(self):

for i in range(length):

charChoice = self.charChoice[random.randint(0, 2)] if self.strength == 'strong' else self.charChoice[random.randint(0, 1)] # If the strength is set to strong it will include the index value for symbols
i = charChoice[random.randint(0, (len(charChoice) - 1))] # Subtracts 1 from the list length to compensate lists in python being starting sub 0

if self.capital == 'true' and i in self.alphabet: # If statement checks if the character can be capitalised
if random.randint(0, 1) == 1: # Coin flip
i = i.upper()
charChoice = random.choice(self.charChoice)
i = random.choice(charChoice)

password += i

return password


class passphrase:
def __init__(self, length=0, capitalize='', allcaps=''):
def __init__(self, length: int = 0, capitalize: bool = True, allcaps: bool = False) -> None:
self.length = length
self.capitalize = capitalize
self.allcaps = allcaps

def get_phrase(self):

def get_phrase(self) -> str:
password = ''

length = int(self.length)
if length <= 0:
length = random.randint(1, 50)
if length > 250:
return f'The length "{length}" is too high, use 250 or below'
if length > 50:
return f'The length "{length}" is too high, use 50 or below'

word = requests.get(f'https://random-word-api.herokuapp.com/word?number={length}') # Access the random word API
try:
word = requests.get(f'https://random-word-api.herokuapp.com/word?number={length}') # Access the random word API
except requests.exceptions.Timeout:
return 'Error: API connection timeout after 60 seconds.'
except requests.exceptions.HTTPError:
return 'Code 404 Error: Could not find API.'
except:
return 'Error: Something went wrong trying to connect to the API.'

word = word.text
word = word[1:-1]
word = word.split(',') # Converts the output of the api into a list instead of it being a string
for item in word:

item = item[1:-1] # Each item in word has quotations around it ( "Example" ) , this line removes those

if self.allcaps == 'true':
if self.allcaps:
item = item.upper()
elif self.capitalize == 'true':
elif self.capitalize:
item = item.title()

if password == '': # If statement determines if the word being added to the passphrase is the first word
Expand Down

0 comments on commit 260f751

Please sign in to comment.