-
Notifications
You must be signed in to change notification settings - Fork 72
lesson_12 #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dychomon
wants to merge
1
commit into
jedzej:master
Choose a base branch
from
dychomon:kuszcjan/lesson_12_debbuging
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
lesson_12 #696
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| ### Lesson 12 - Debugging | ||
| #### introduction | ||
| - [Automate the Boring Stuff with Python / Chapter 10](https://automatetheboringstuff.com/chapter10/) | ||
| #### practice projects | ||
| 1. [Automate the Boring Stuff with Python / Chapter 10 / Debugging Coin Toss](https://automatetheboringstuff.com/chapter10/) | ||
| 1. Verbose output - add configurable logger and 'verbose output' command line argument to project 1 from lesson 11 to allow the user to follow intermediate steps of program execution. The program must allow to configure verbosity on at least 3 levels - disabled, warning (warns and errors only) and info (most detailed) | ||
| 1. Implement 'verbose output' for project 2 from lesson 11 | ||
| 1. Implement 'verbose output' for project 3 from lesson 11 | ||
| 1. Validated user base - write script that takes email, password, phone number and postal code, validates these fields and if validation passes, saves it to a file as CSV with email considered as unique field. If a record with the same email is already in the file, the old record should be altered by new one. Use validators implemented in lesson 8, exercises 2, 3, 4 and 5. As part of this exercise write combined_validator function that takes email, password, phone number and postal code and throws exceptions if any of arguments doesn't pass validation. Add 'verbose output'. |
Binary file added
BIN
+1.03 KB
students/dychowicz_monika/lesson_12_debbugging/__pycache__/regex_validator.cpython-36.pyc
Binary file not shown.
28 changes: 28 additions & 0 deletions
28
students/dychowicz_monika/lesson_12_debbugging/coin_toss.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import random | ||
| import logging | ||
|
|
||
| logging.basicConfig(level=logging.DEBUG, | ||
| format=' %(asctime)s - %(levelname)s- %(message)s') | ||
|
|
||
| guess = '' | ||
|
|
||
| toss_int = random.randint(0, 1) # 0 is tails, 1 is heads | ||
| if toss_int == 0: | ||
| toss = "heads" | ||
| else: | ||
| toss = "tails" | ||
|
|
||
| logging.debug("toss = " + str(toss)) | ||
|
|
||
| while guess not in ('heads', 'tails'): | ||
| print('Guess the coin toss! Enter heads or tails:') | ||
| guess = input() | ||
| if toss == guess: | ||
| print('You got it!') | ||
| else: | ||
| print('Nope! Guess again!') | ||
| guess = input() | ||
| if toss == guess: | ||
| print('You got it!') | ||
| else: | ||
| print('Nope. You are really bad at this game.') | ||
34 changes: 34 additions & 0 deletions
34
students/dychowicz_monika/lesson_12_debbugging/regex_validator.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import re | ||
|
|
||
|
|
||
| def email_validator(some_email): | ||
| email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+") | ||
| result = email_regex.match(some_email) | ||
| if result is None: | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
|
|
||
| def phone_number_validator(some_number): | ||
| phone_regex = re.compile(r"^\d{3}[- ]?\d{3}[- ]?\d{3}$") | ||
| if phone_regex.match(some_number) is None: | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
|
|
||
| def postal_code_validator(some_postal_code): | ||
| postal_code_regex = re.compile(r"^\d\d-\d\d\d$") | ||
| if postal_code_regex.match(some_postal_code) is None: | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
|
|
||
| def pass_checker(some_password): | ||
| if re.match(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$', | ||
| some_password) is None: | ||
| return False | ||
| else: | ||
| return True |
81 changes: 81 additions & 0 deletions
81
students/dychowicz_monika/lesson_12_debbugging/validated_user_base.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import sys | ||
| import regex_validator | ||
| import logging | ||
| import csv | ||
| import argparse | ||
|
|
||
| logging.basicConfig(format=' %(asctime)s - %(levelname)s- %(message)s') | ||
|
|
||
|
|
||
| def combined_validator(email_addr, phone_nr, postal_code, passwd): | ||
| if not regex_validator.email_validator(email_addr): | ||
| raise Exception("Incorrect email") | ||
| if not regex_validator.phone_number_validator(phone_nr): | ||
| raise Exception("Incorrect phone number") | ||
| if not regex_validator.postal_code_validator(postal_code): | ||
| raise Exception("Incorrect postal code") | ||
| if not regex_validator.pass_checker(passwd): | ||
| raise Exception("Strong password required") | ||
|
|
||
|
|
||
| def set_verbose_level(verbose_level): | ||
| logger = logging.getLogger() | ||
| if verbose_level == "info": | ||
| logger.setLevel(logging.INFO) | ||
| elif verbose_level == "warning": | ||
| logger.setLevel(logging.WARNING) | ||
| else: | ||
| logger.disabled = True | ||
|
|
||
|
|
||
| def check_args(args=None): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-e", "--email", | ||
| help="email", | ||
| required=True) | ||
| parser.add_argument("-p", "--password", | ||
| help="password", | ||
| required=True) | ||
| parser.add_argument("-n", "--number", | ||
| help="phone number", | ||
| required=True) | ||
| parser.add_argument("-c", "--code", | ||
| help="postal code", | ||
| required=True) | ||
| parser.add_argument("-v", "--verbose", | ||
| help="verbose level", | ||
| choices=['disabled', 'warning', 'info'], | ||
| default="warning") | ||
| res = parser.parse_args(args) | ||
| return res.email, res.password, res.number, res.code, res.verbose | ||
|
|
||
|
|
||
| email, password, number, code, verbose = check_args(sys.argv[1:]) | ||
| user_data = {"email": email, "password": password, "number": number, | ||
| "code": code} | ||
|
|
||
|
|
||
| def write_csv(write_file, read_file): | ||
| with open(write_file, 'a') as csv_write_file, open(read_file, | ||
| 'r') as csv_read_file: | ||
| fieldnames = ['email', 'password', 'number', 'code'] | ||
| reader = csv.DictReader(csv_read_file, fieldnames=fieldnames) | ||
| writer = csv.DictWriter(csv_write_file, fieldnames=fieldnames) | ||
| email_exists = False | ||
| for i in reader: | ||
| logging.info("Data stored in file: {}".format(i)) | ||
| if i['email'] == user_data['email']: | ||
| logging.info("email {} exists. Replacing user's data".format( | ||
| i['email'])) | ||
| email_exists = True | ||
| if not email_exists: | ||
| writer.writerow(user_data) | ||
| logging.info("User doesn't exists, adding new user's data.") | ||
| logging.info("Added user data: {}".format(user_data)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| level = verbose | ||
| set_verbose_level(level) | ||
| combined_validator(email, number, code, password) | ||
| write_csv("test.csv", "test.csv") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
57 changes: 57 additions & 0 deletions
57
students/dychowicz_monika/lesson_12_debbugging/verbose_output.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import os | ||
| import re | ||
| import argparse | ||
| import logging | ||
|
|
||
| logging.basicConfig(format=' %(asctime)s - %(levelname)s- %(message)s') | ||
|
|
||
|
|
||
| def check_args(args=None): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-r", "--regex", | ||
| help="regex", | ||
| default=".*") | ||
| parser.add_argument("-d", "--directory", | ||
| help="directory", | ||
| default=r"C:\Python36\files") | ||
| parser.add_argument("-v", "--verbose", | ||
| help="verbose level", | ||
| choices=['disabled', 'warning', 'info'], | ||
| default="warning") | ||
| results = parser.parse_args(args) | ||
| return results.regex, results.directory, results.verbose | ||
|
|
||
|
|
||
| def set_verbose_level(verbose_level): | ||
| logger = logging.getLogger() | ||
| if verbose_level == "info": | ||
| logger.setLevel(logging.INFO) | ||
| elif verbose_level == "warning": | ||
| logger.setLevel(logging.WARNING) | ||
| else: | ||
| logger.disabled = True | ||
|
|
||
|
|
||
| def regex_search(reg, folder): | ||
| regex_text = re.compile(reg) | ||
| logging.info("Regex = {}".format(regex_text)) | ||
| files_list = os.listdir(folder) | ||
| logging.info("Files: {}".format(files_list)) | ||
| for file in files_list: | ||
| if file.endswith('.txt'): | ||
| logging.info("Textfile found: {}".format(file)) | ||
| with open(os.path.join(folder, file)) as textfile: | ||
| for line in textfile: | ||
| if regex_text.match(line): | ||
| print("Regex found " + line) | ||
| logging.info("Regex found " + line) | ||
| else: | ||
| logging.warning("Regex not found" + line) | ||
| else: | ||
| logging.warning("File {} is not a text file".format(file)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| regex, directory, level = check_args() | ||
| set_verbose_level(level) | ||
| regex_search(regex, directory) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W292 no newline at end of file