Skip to content

Instantly share code, notes, and snippets.

@cherylli
Created September 16, 2019 12:00
Show Gist options
  • Save cherylli/d58826fa80b9622b10e5a92b0cf01b30 to your computer and use it in GitHub Desktop.
Save cherylli/d58826fa80b9622b10e5a92b0cf01b30 to your computer and use it in GitHub Desktop.
Automate the Boring Stuff with Python, Chaper 7 Practice Project, Strong Password Detection
import unittest
from strong_password_detection import isStrongPassword
# A strong password is defined as one that is
# at least eight characters long,
# contains both uppercase and lowercase characters,
# and has at least one digit.
class TestStrongPasswords(unittest.TestCase):
def test_empty_password(self):
self.assertFalse(isStrongPassword(''))
def test_less_than_eight_characters(self):
self.assertFalse(isStrongPassword('rYer1'))
def test_more_than_eight_characters(self):
self.assertTrue(isStrongPassword('rnerrYer1rYer1rYer1'))
def test_only_lower_case(self):
self.assertFalse(isStrongPassword('rnerryer1ryer1ryer1'))
def test_strong_password(self):
self.assertTrue(isStrongPassword('T5hjmrtj'))
if __name__ == '__main__':
unittest.main()
import re
# A strong password is defined as one that is
# at least eight characters long,
# contains both uppercase and lowercase characters,
# and has at least one digit.
def isStrongPassword(pw):
return bool(re.match("""^(?=.*[a-z]) #contains lowercase characters
(?=.*[A-Z]) # contains uppercase
(?=.*[0-9]) # has at least one digit
(?=.{8,})""" # at least eight characters long
, pw, re.VERBOSE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment