-
-
Notifications
You must be signed in to change notification settings - Fork 105
Glasgow | 25-SDC-Jul | Mirabelle Morah | Sprint 4 | implementing shell tools in python #657
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
mirabellemorah
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
mirabellemorah:sprint-4-implementing-shell-tools-in-py
base: main
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.
+162
−0
Open
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
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,49 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="check-for-cat", | ||
| description="Implement my own version of cat in python", | ||
| ) | ||
|
|
||
| parser.add_argument("paths", nargs="+", help="The file paths to process") | ||
| parser.add_argument("-n", action="store_true", help="Number lines") | ||
| parser.add_argument("-b", action="store_true", help="Number non-blank lines") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| paths = args.paths | ||
| show_n = args.n | ||
| show_b = args.b | ||
|
|
||
| line_number = 1 | ||
|
|
||
| for path in paths: | ||
| with open(path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
|
|
||
| ends_with_newline = content.endswith("\n") | ||
| lines = content.split("\n") | ||
| if ends_with_newline: | ||
| lines.pop() | ||
|
|
||
| new_lines = [] | ||
| for line in lines: | ||
| if show_b: | ||
| if line == "": | ||
| new_lines.append(line) | ||
| else: | ||
| numbered = str(line_number).rjust(6, " ") + "\t" + line | ||
| line_number += 1 | ||
| new_lines.append(numbered) | ||
| elif show_n: | ||
| numbered = str(line_number).rjust(6, " ") + "\t" + line | ||
| line_number += 1 | ||
| new_lines.append(numbered) | ||
| else: | ||
| new_lines.append(line) | ||
| lines = new_lines | ||
|
|
||
| output = "\n".join(lines) | ||
| if ends_with_newline: | ||
| output += "\n" | ||
| print(output, end="") | ||
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,43 @@ | ||
| import argparse | ||
| import os | ||
| import stat | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="check-for-ls", | ||
| description="Implement my own version of ls", | ||
| ) | ||
|
|
||
| parser.add_argument("paths", nargs="*", help="The file paths to process") | ||
| parser.add_argument("-1", "--one", action="store_true", help="This lists one file per line") | ||
| parser.add_argument("-a", action="store_true", help="This shows all files") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| show_one_line = args.one | ||
| show_all_files = args.a | ||
|
|
||
| paths = args.paths | ||
| file_path = paths | ||
| if len(file_path) == 0: | ||
| file_path = ["."] | ||
|
|
||
| for target in file_path: | ||
| info = os.stat(target) | ||
|
|
||
| if stat.S_ISDIR(info.st_mode): | ||
| show_files = os.listdir(target) | ||
|
|
||
| if show_all_files: | ||
| show_files = [".", "..", *show_files] | ||
| else: | ||
| show_files = [name for name in show_files if not name.startswith(".")] | ||
|
|
||
| show_files.sort() | ||
| else: | ||
| show_files = [target] | ||
|
|
||
| if show_one_line: | ||
| for file in show_files: | ||
| print(file) | ||
| else: | ||
| print(" ".join(show_files)) |
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,70 @@ | ||
| import argparse | ||
| import re | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="check-for-wc", | ||
| description="Implement my own version of wc", | ||
| ) | ||
|
|
||
| parser.add_argument("paths", nargs="+", help="The file paths to process") | ||
| parser.add_argument("-l", action="store_true", help="Counts the total number of lines") | ||
| parser.add_argument("-c", action="store_true", help="Counts the total number of characters") | ||
| parser.add_argument("-w", action="store_true", help="Counts the total number of words") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| paths = args.paths | ||
| show_lines = args.l | ||
| show_words = args.w | ||
| show_char = args.c | ||
|
|
||
| no_flags_given = not show_lines and not show_words and not show_char | ||
|
|
||
| columns = [] | ||
|
|
||
| for path in paths: | ||
| with open(path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
|
|
||
| line_count = content.count("\n") | ||
|
|
||
| word_count = len([word for word in re.split(r"\s+", content) if word != ""]) | ||
|
|
||
| char_count = len(content.encode("utf-8")) | ||
|
|
||
| columns.append({ | ||
| "path": path, | ||
| "lines": line_count, | ||
| "words": word_count, | ||
| "char": char_count, | ||
| }) | ||
|
|
||
| if len(columns) > 1: | ||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_char = 0 | ||
|
|
||
| for result in columns: | ||
| total_lines += result["lines"] | ||
| total_words += result["words"] | ||
| total_char += result["char"] | ||
|
|
||
| columns.append({ | ||
| "path": "total", | ||
| "lines": total_lines, | ||
| "words": total_words, | ||
| "char": total_char, | ||
| }) | ||
|
|
||
| for result in columns: | ||
| line = "" | ||
| if no_flags_given or show_lines: | ||
| line += str(result["lines"]).rjust(6, " ") | ||
| if no_flags_given or show_words: | ||
| line += str(result["words"]).rjust(6, " ") | ||
| if no_flags_given or show_char: | ||
| line += str(result["char"]).rjust(6, " ") | ||
|
|
||
| line += " " + result["path"] | ||
|
|
||
| print(line) |
Oops, something went wrong.
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.
There's some repeated code patterns here, can that be reduced?