-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-Mar-SDC | Craig D'Silva | Sprint 4 | Implement Shell Tools Python #482
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
craig-dsilva
wants to merge
16
commits into
CodeYourFuture:main
Choose a base branch
from
craig-dsilva:implement-shell-tools-python
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
16 commits
Select commit
Hold shift + click to select a range
7305e8f
cat-read-single-file
craig-dsilva 3baaa84
cat-read-multiple-files
craig-dsilva 0a8c55d
-n flag
craig-dsilva 5ce2651
-b flag
craig-dsilva e31edff
cat - complete
craig-dsilva 64cfdd2
cat: error
craig-dsilva 21c46a6
ls: Init
craig-dsilva 2bcbf2f
ls: -a flag
craig-dsilva 9901396
ls: -a flag format
craig-dsilva 4b88392
ls: complete
craig-dsilva 02de813
Create dictionary
craig-dsilva 466c007
wc: line and word count
craig-dsilva f0dc6b0
wc: byte count
craig-dsilva 15d73be
Print helper
craig-dsilva f89d3d8
Add flag support
craig-dsilva 4a8a83e
Proper variable names for dicts
craig-dsilva 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,39 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser(prog="cat", description="Prints the output of a file to the console") | ||
| parser.add_argument("-n", "--number", help="Displays the lines along with their number", action="store_true") | ||
| parser.add_argument("-b", "--nonblank", help="Displays the lines along with their number skipping the blank lines", action="store_true") | ||
| parser.add_argument("path", help="The file path", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| show_lines = args.number | ||
| non_blank = args.nonblank | ||
|
|
||
| if show_lines == True and non_blank == True: | ||
| print("Error: Cannot use -n and -b together. Please use only one flag at a time.") | ||
| exit() | ||
|
|
||
| text = "" | ||
| i = 1 | ||
|
|
||
| for file in args.path: | ||
| f = open(file) | ||
| text += f.read() | ||
|
|
||
| text_list = text.split("\n") | ||
| text_list.pop(len(text_list) - 1) | ||
|
|
||
| if (show_lines == False and non_blank == False): | ||
| print("\n".join(text_list)) | ||
| exit() | ||
|
|
||
| for line in text_list: | ||
| if non_blank == True and line != "": | ||
| print(" " + str(i) + " " + line) | ||
| i += 1 | ||
| elif non_blank == False: | ||
| print(" " + str(i) + " " + line) | ||
| i += 1 | ||
| else: | ||
| print("") | ||
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,29 @@ | ||
| import argparse | ||
| from os import listdir | ||
|
|
||
| parser = argparse.ArgumentParser(prog="ls", description="List directory contents. Ignore files and directories starting with a '.' by default") | ||
| parser.add_argument("-1", dest="one", help="List one file per line.", action="store_true") | ||
| parser.add_argument("-a", help="Do not ignore hidden files (files with names that start with '.').", action="store_true") | ||
| parser.add_argument("path", help="The directory path (optional).", default=".", nargs="?") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| path = args.path | ||
|
|
||
| show_hidden = args.a | ||
| one_per_line = args.one | ||
|
|
||
| contents = [] | ||
|
|
||
| for f in listdir(path): | ||
| if show_hidden == False and f[0] != ".": | ||
| contents.append(f) | ||
| if show_hidden == True: | ||
| contents.append(f) | ||
|
|
||
| contents.sort() | ||
|
|
||
| if one_per_line == True: | ||
| print("\n".join(contents)) | ||
| else: | ||
| print(" ".join(contents)) |
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,62 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser(prog="wc", description="Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.") | ||
| parser.add_argument("-w", "--words", help="print the word counts", action="store_true") | ||
| parser.add_argument("-l", "--line", help="print the newline counts", action="store_true") | ||
| parser.add_argument("-c", "--bytes", help="print the byte counts", action="store_true") | ||
| parser.add_argument("path", help="The file path", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| lines = 0 | ||
| words = 0 | ||
| bytes = 0 | ||
|
|
||
| l = True | ||
| w = True | ||
| c = True | ||
|
|
||
| if args.words == True or args.line == True or args.bytes == True: | ||
| l = args.line | ||
| w = args.words | ||
| c = args.bytes | ||
|
|
||
| file_data = {} | ||
| file_data_with_newline = {} | ||
|
|
||
| for file in args.path: | ||
| f = open(file) | ||
| file_data[file] = f.read().split("\n") | ||
|
|
||
| for file in args.path: | ||
| f = open(file) | ||
| file_data_with_newline[file] = f.read() | ||
|
|
||
| def print_helper(line, word, byte, file_name): | ||
| text = [" "] | ||
| if l == True: | ||
| text.append(str(line)) | ||
| text.append(" ") | ||
| if w == True: | ||
| text.append(str(word)) | ||
| text.append(" ") | ||
| if c == True: | ||
| text.append(str(byte)) | ||
| text.append(" ") | ||
| text.append(file_name) | ||
| print("".join(text)) | ||
|
|
||
| for f in file_data: | ||
| word_per_line = 0 | ||
| byte_per_line = 0 | ||
| for line in file_data[f]: | ||
| word_per_line += len(line.split()) | ||
| for line in file_data_with_newline[f]: | ||
| byte_per_line += len(line.encode("utf-8")) | ||
| lines += len(file_data[f]) - 1 | ||
| words += word_per_line | ||
| bytes += byte_per_line | ||
| print_helper(len(file_data[f]) - 1, word_per_line, byte_per_line, f) | ||
|
|
||
| if len(args.path) > 1: | ||
| print_helper(lines, words, bytes, "total") |
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.
If you are testing booleans, is there a better way of writing these if statements?