-
-
Notifications
You must be signed in to change notification settings - Fork 108
London | 26-SDC-July | Kris Oldrini | Sprint 4 | Python Shell Tools #665
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
XiaoQuark
wants to merge
4
commits into
CodeYourFuture:main
Choose a base branch
from
XiaoQuark:python-shell-tools
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4ecd9f6
task: complete python implementation of cat
XiaoQuark 83ef771
complete python implementation of ls
XiaoQuark 7889a32
task: complete python implementation of wc
XiaoQuark 8104ffb
Merge branch 'CodeYourFuture:main' into python-shell-tools
XiaoQuark 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,66 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat", | ||
| description="Prints the content of files" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-n", | ||
| "--number", | ||
| action="store_true", | ||
| help="Numbers all lines" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-b", | ||
| "--number-nonblank", | ||
| action="store_true", | ||
| help="Number non-empty lines" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "paths", | ||
| nargs="+", | ||
| help="The file to print" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| line_number = 1 | ||
|
|
||
|
|
||
| def number_lines(lines, line_number, include_blank_lines): | ||
| numbered_lines = [] | ||
|
|
||
| for line in lines: | ||
| if include_blank_lines or line != "\n": | ||
| numbered_line = str(line_number).rjust(6) + " " + line | ||
| numbered_lines.append(numbered_line) | ||
| line_number += 1 | ||
| else: | ||
| numbered_lines.append(line) | ||
|
|
||
| return numbered_lines, line_number | ||
|
|
||
|
|
||
| for path in args.paths: | ||
| with open(path, "r") as file: | ||
| lines = file.readlines() | ||
|
|
||
| if args.number_nonblank: | ||
| lines, line_number = number_lines( | ||
| lines, | ||
| line_number, | ||
| False | ||
| ) | ||
| elif args.number: | ||
| lines, line_number = number_lines( | ||
| lines, | ||
| line_number, | ||
| True | ||
| ) | ||
|
|
||
| output = "".join(lines) | ||
| sys.stdout.write(output) | ||
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,74 @@ | ||
| import argparse | ||
| import sys | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls", | ||
| description="List directory contents" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-1", | ||
| "--format", | ||
| action="store_true", | ||
| help="List one file per line" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-a", | ||
| "--all", | ||
| action="store_true", | ||
| help="Do not ignore entries starting with ." | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "paths", | ||
| nargs="*", | ||
| help="The file to print" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if args.format: | ||
| separator = "\n" | ||
| else: | ||
| separator = " " | ||
|
|
||
| multiple_paths = len(args.paths) > 1 | ||
|
|
||
| file_paths = [] | ||
| directory_paths = [] | ||
|
|
||
| if len(args.paths) == 0: | ||
| args.paths.append(".") | ||
|
|
||
| for path in args.paths: | ||
| if os.path.isfile(path): | ||
| file_paths.append(path) | ||
|
|
||
| elif os.path.isdir(path): | ||
| directory_paths.append(path) | ||
|
|
||
| if len(file_paths) > 0: | ||
| sys.stdout.write(separator.join(file_paths) + "\n") | ||
|
|
||
| for path in directory_paths: | ||
| if multiple_paths: | ||
| sys.stdout.write("\n" + path + ":\n") | ||
|
|
||
| entries = os.listdir(path) | ||
| entries.sort() | ||
|
|
||
| if args.all: | ||
| entries = [".", ".."] + entries | ||
|
|
||
| else: | ||
| visible_entries = [] | ||
|
|
||
| for entry in entries: | ||
| if not entry.startswith("."): | ||
| visible_entries.append(entry) | ||
|
|
||
| entries = visible_entries | ||
|
|
||
| sys.stdout.write(separator.join(entries) + "\n") |
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,120 @@ | ||
| import argparse | ||
| import sys | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="wc", | ||
| description="Print newline, word, and byte counts for files" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-l", | ||
| "--lines", | ||
| action="store_true", | ||
| help="Print the newline counts" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-w", | ||
| "--words", | ||
| action="store_true", | ||
| help="Print the word counts" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", | ||
| "--bytes", | ||
| action="store_true", | ||
| help="Print the byte counts" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "paths", | ||
| nargs="*", | ||
| help="The file to print" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| total_line_count = 0 | ||
| total_word_count = 0 | ||
| total_byte_count = 0 | ||
|
|
||
| rows = [] | ||
|
|
||
| def get_counts(path): | ||
| with open(path, "r") as file: | ||
| content = file.readlines() | ||
|
|
||
| lines_count = len(content) | ||
|
|
||
| joined_content = "".join(content) | ||
|
|
||
| words = joined_content.split() | ||
| word_count = len(words) | ||
|
|
||
| byte_count = len(joined_content.encode()) | ||
|
|
||
| return lines_count, word_count, byte_count | ||
|
|
||
|
|
||
| def select_count(lines_count, word_count, byte_count): | ||
| selected_counts = [] | ||
|
|
||
| if args.lines: | ||
| selected_counts.append(lines_count) | ||
|
|
||
| if args.words: | ||
| selected_counts.append(word_count) | ||
|
|
||
| if args.bytes: | ||
| selected_counts.append(byte_count) | ||
|
|
||
| if len(selected_counts) == 0: | ||
| selected_counts.append(lines_count) | ||
| selected_counts.append(word_count) | ||
| selected_counts.append(byte_count) | ||
|
|
||
| return selected_counts | ||
|
|
||
|
|
||
| for path in args.paths: | ||
| lines_count, word_count, byte_count = get_counts(path) | ||
|
|
||
| total_line_count += lines_count | ||
| total_word_count += word_count | ||
| total_byte_count += byte_count | ||
|
|
||
| selected_counts = select_count( | ||
| str(lines_count), | ||
| str(word_count), | ||
| str(byte_count) | ||
| ) | ||
|
|
||
| rows.append((selected_counts, path)) | ||
|
|
||
| if len(args.paths) > 1: | ||
| total_counts = select_count( | ||
| str(total_line_count), | ||
| str(total_word_count), | ||
| str(total_byte_count) | ||
| ) | ||
| rows.append((total_counts, "total")) | ||
|
|
||
| padding = max( | ||
| len(count) | ||
| for counts, _ in rows | ||
| for count in counts | ||
| ) | ||
|
|
||
| output = [] | ||
|
|
||
| for counts, path in rows: | ||
| padded_counts = [] | ||
|
|
||
| for count in counts: | ||
| padded_counts.append(count.rjust(padding)) | ||
|
|
||
| output.append(" ".join(padded_counts) + " " + path) | ||
|
|
||
| print("\n".join(output)) |
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.
Given basically the same method call is used here, is there a reason for checking if args.number_nonblank here, then passing a parameter, then having another if; instead of checking this arg directly in the number_lines function?