-
Notifications
You must be signed in to change notification settings - Fork 72
lesson_11 #678
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
2
commits into
jedzej:master
Choose a base branch
from
dychomon:kuszcjan/lesson_11_argparse_and_math
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_11 #678
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
11 changes: 11 additions & 0 deletions
11
students/dychowicz_monika/lesson_11_argparse_and_math/README.md
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,11 @@ | ||
| ### Lesson 11 - argparse + math | ||
| #### introduction | ||
| - [What is argparse?](http://www.bogotobogo.com/python/python_argparse.php) | ||
| - [argparse — Command-Line Option and Argument Parsing](https://pymotw.com/3/argparse/) | ||
| #### practice projects | ||
| 1. Modify project 1 from lesson 9 to use argparse for parsing input | ||
| 1. Modify project 2 from lesson 9 to use argparse for parsing input | ||
| 1. Modify project 3 from lesson 9 to use argparse for parsing input | ||
| 1. Modify project 1 from lesson 10 to use argparse for parsing input | ||
| 1. Modify project 2 from lesson 10 to use argparse for parsing input | ||
| 1. Modify project 3 from lesson 10 to use argparse for parsing input |
42 changes: 42 additions & 0 deletions
42
students/dychowicz_monika/lesson_11_argparse_and_math/filling_the_gaps_argparse.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,42 @@ | ||
| import os | ||
| import re | ||
| import argparse | ||
|
|
||
|
|
||
| def check_args(args=None): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-p", "--prefix", | ||
| help="prefix", | ||
| default="spam") | ||
| parser.add_argument("-d", "--directory", | ||
| help="directory", | ||
| default=r"C:\Python36\gaps") | ||
| results = parser.parse_args(args) | ||
| return results.prefix, results.directory | ||
|
|
||
|
|
||
| def fill_the_gaps(folder): | ||
| for foldername, subfolders, filenames in os.walk(folder): | ||
| filenames = sorted(filenames) | ||
| index = 1 | ||
| new_filename = "" | ||
| for filename in filenames: | ||
| if pattern.match(filename): | ||
| if index == 1: | ||
| new_filename = filename | ||
| print(filename, new_filename, index) | ||
| if filename != new_filename: | ||
| print( | ||
| "Lack of filename: " + new_filename + "\nFile " + | ||
| filename + " will be renamed.") | ||
| filename_path = os.path.join(foldername, filename) | ||
| new_filename_path = os.path.join(foldername, new_filename) | ||
| os.rename(filename_path, new_filename_path) | ||
| new_filename = filename.replace(str(index), str(index + 1)) | ||
| index += 1 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| prefix, directory = check_args() | ||
| pattern = re.compile(prefix + r'.*(\d{3})') | ||
| fill_the_gaps(directory) | ||
31 changes: 31 additions & 0 deletions
31
students/dychowicz_monika/lesson_11_argparse_and_math/regex_search_argparse.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,31 @@ | ||
| import os | ||
| import re | ||
| import argparse | ||
|
|
||
|
|
||
| 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") | ||
| results = parser.parse_args(args) | ||
| return results.regex, results.directory | ||
|
|
||
|
|
||
| def regex_search(reg, folder): | ||
| regex_text = re.compile(reg) | ||
| files_list = os.listdir(folder) | ||
| for file in files_list: | ||
| if file.endswith('.txt'): | ||
| with open(os.path.join(folder, file)) as textfile: | ||
| for line in textfile: | ||
| if regex_text.match(line): | ||
| print("Regex found " + line) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| regex, directory = check_args() | ||
| 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 |
||
31 changes: 31 additions & 0 deletions
31
students/dychowicz_monika/lesson_11_argparse_and_math/selective_copy_argparse.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,31 @@ | ||
| import os | ||
| import shutil | ||
| import argparse | ||
|
|
||
|
|
||
| def check_args(args=None): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-s", "--source", | ||
| help="source directory", | ||
| default=r'C:\python_to_copy') | ||
| parser.add_argument("-d", "--destination", | ||
| help="destination directory", | ||
| default=r'C:\python_copied') | ||
| parser.add_argument("-e", "--extension", | ||
| help="extension", | ||
| default=".txt") | ||
| results = parser.parse_args(args) | ||
| return results.source, results.destination, results.extension | ||
|
|
||
|
|
||
| def selective_copy(source_folder, destination_folder, ext): | ||
| for foldername, subfolders, filenames in os.walk(source_folder): | ||
| for filename in filenames: | ||
| if filename.lower().endswith(ext): | ||
| shutil.copy(os.path.join(source_folder, filename), | ||
| destination_folder) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| source, destination, extension = check_args() | ||
| selective_copy(source, destination, extension) | ||
|
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