-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26-SDC-July| Mariia Serhiienko | Sprint 4 | Implement shell tools in Python #661
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
Konvaly
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
Konvaly:implement-shell-tools-python-2
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
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,30 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat", | ||
| description="Concatenate files and print on the standard output", | ||
| ) | ||
| parser.add_argument("-n", "--number", action="store_true", | ||
| help="Number all output lines") | ||
| parser.add_argument("-b", "--number-nonblank", action="store_true", | ||
| help="Number non-empty output lines, overrides -n") | ||
| parser.add_argument("files", nargs="+", help="The files to print") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| counter = 0 | ||
| for path in args.files: | ||
| with open(path, "r") as f: | ||
| for line in f: | ||
| if args.number_nonblank: | ||
| if line.strip("\n") == "": | ||
| sys.stdout.write(line) | ||
| else: | ||
| counter += 1 | ||
| sys.stdout.write(f"{counter:6}\t{line}") | ||
| elif args.number: | ||
| counter += 1 | ||
| sys.stdout.write(f"{counter:6}\t{line}") | ||
| else: | ||
| sys.stdout.write(line) | ||
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,53 @@ | ||
| import argparse | ||
| import os | ||
| import sys | ||
|
|
||
| parser = argparse.ArgumentParser(prog="ls", description="List directory contents") | ||
| parser.add_argument("-1", dest="one_per_line", action="store_true", | ||
|
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. What is the -1 argument used for? |
||
| 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="*", default=["."], | ||
| help="The files or directories to list") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
|
|
||
| def entries(directory): | ||
| names = os.listdir(directory) | ||
| if args.all: | ||
| names = names + [".", ".."] | ||
| else: | ||
| names = [name for name in names if not name.startswith(".")] | ||
| return sorted(names, key=lambda name: name.lstrip(".").lower()) | ||
|
|
||
|
|
||
| files = [] | ||
| directories = [] | ||
| for path in args.paths: | ||
| if os.path.isdir(path): | ||
| directories.append(path) | ||
| elif os.path.exists(path): | ||
| files.append(path) | ||
| else: | ||
| print(f"ls: cannot access '{path}': No such file or directory", | ||
| file=sys.stderr) | ||
|
|
||
| files.sort() | ||
| directories.sort() | ||
|
|
||
| show_headers = len(args.paths) > 1 | ||
| printed_anything = False | ||
|
|
||
| for path in files: | ||
| print(path) | ||
| printed_anything = True | ||
|
|
||
| for directory in directories: | ||
| if show_headers: | ||
| if printed_anything: | ||
| print() | ||
| print(f"{directory}:") | ||
| for name in entries(directory): | ||
| print(name) | ||
| printed_anything = True | ||
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,48 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser(prog="wc", description="Print newline, word and byte counts") | ||
| 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("files", nargs="+", help="The files to count") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| show_all = not (args.lines or args.words or args.bytes) | ||
| show_lines = args.lines or show_all | ||
| show_words = args.words or show_all | ||
| show_bytes = args.bytes or show_all | ||
|
|
||
| rows = [] | ||
| total = [0, 0, 0] | ||
|
|
||
| for path in args.files: | ||
| with open(path, "rb") as f: | ||
| data = f.read() | ||
| counts = [data.count(b"\n"), len(data.split()), len(data)] | ||
| for i in range(3): | ||
| total[i] += counts[i] | ||
| rows.append((counts, path)) | ||
|
|
||
| if len(args.files) > 1: | ||
| rows.append((total, "total")) | ||
| width = len(str(sum(os.path.getsize(path) for path in args.files))) | ||
| else: | ||
| width = 1 | ||
|
|
||
|
|
||
| def selected(counts): | ||
| chosen = [] | ||
| if show_lines: | ||
| chosen.append(counts[0]) | ||
| if show_words: | ||
| chosen.append(counts[1]) | ||
| if show_bytes: | ||
| chosen.append(counts[2]) | ||
| return chosen | ||
|
|
||
|
|
||
| for counts, label in rows: | ||
| columns = " ".join(f"{value:{width}}" for value in selected(counts)) | ||
| print(f"{columns} {label}") |
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?