-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-SDC-Mar | Mariia Serhiienko | Sprint 4 |Implement shell tools python #529
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
5
commits into
CodeYourFuture:main
Choose a base branch
from
Konvaly: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.
+168
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba183ba
Add .venv and __pycache__ to .gitignore
Konvaly bb78345
Implement cat command in Python
Konvaly 60563bd
Implemented ls command in Python
Konvaly 4e760a2
Implemented command wc
Konvaly 1ea1827
Merge branch 'main' into implement-shell-tools-python
Konvaly 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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| node_modules | ||
| testoutput.txt | ||
| .venv | ||
| __pycache__ | ||
| *.pyc |
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,47 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat", | ||
| description="Concatenate and print files", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-n", | ||
| action="store_true", | ||
| help="Number all output lines" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-b", | ||
| action="store_true", | ||
| help="Number non-blank output lines" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "paths", | ||
| nargs="+", | ||
| help="The file(s) to print", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| line_number = 1 | ||
|
|
||
| for path in args.paths: | ||
| with open (path, "r") as f: | ||
| lines = f.readlines() | ||
|
|
||
| for line in lines: | ||
| is_blank = line.strip() == "" | ||
|
|
||
| if args.b: | ||
| if is_blank: | ||
| print(line, end="") | ||
| else: | ||
| print(f"{line_number:>6}\t{line}", end="") | ||
| line_number += 1 | ||
| elif args.n: | ||
| print(f"{line_number:>6}\t{line}", end="") | ||
| line_number += 1 | ||
| else: | ||
| print(line, 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,41 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls", | ||
| description="List directory content", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-1", | ||
| action="store_true", | ||
| dest="one", | ||
| help="List one file per line", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-a", | ||
| action="store_true", | ||
| help="Do not ignore entries starting with .", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "path", | ||
| nargs="?", | ||
| default=".", | ||
| help="The directory to list", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| items = os.listdir(args.path) | ||
|
|
||
| if args.a: | ||
| items = [".", ".."] + items | ||
| else: | ||
| items = [item for item in items if not item.startswith(".")] | ||
|
|
||
| items = sorted(items, key=lambda item: item.lstrip(".").lower()) | ||
|
|
||
| for item in items: | ||
| print(item) | ||
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,77 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="wc", | ||
| description="Count lines, words and characters in files", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-l", | ||
| action="store_true", | ||
| help="Count lines only" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-w", | ||
| action="store_true", | ||
| help="Count words only" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", | ||
| action="store_true", | ||
| help="Count characters only", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "paths", | ||
| nargs="+", | ||
| help="The file(s) to count", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_chars = 0 | ||
|
|
||
| counts = [] | ||
|
|
||
| for path in args.paths: | ||
| with open(path, "r") as f: | ||
| content = f.read() | ||
|
|
||
| lines = len(content.splitlines()) | ||
| words = len(content.split()) | ||
| chars = len(content) | ||
|
|
||
| total_lines += lines | ||
| total_words += words | ||
| total_chars += chars | ||
|
|
||
| counts.append((lines, words, chars, path)) | ||
|
|
||
| width_lines = max(len(str(total_lines)) + 1, 2) | ||
| width_words = max(len(str(total_words)) + 1, 3) | ||
| width_chars = max(len(str(total_chars)) + 1, 4) | ||
|
|
||
| for lines, words, chars, path in counts: | ||
| if args.l: | ||
| print(f"{lines:>{width_lines}} {path}") | ||
| elif args.w: | ||
| print(f"{words:>{width_words}} {path}") | ||
| elif args.c: | ||
|
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. Is there a way to combine these, say if I wanted words and chars, but not lines? |
||
| print(f"{chars:>{width_chars}} {path}") | ||
| else: | ||
| print(f"{lines:>{width_lines}}{words:>{width_words}}{chars:>{width_chars}} {path}") | ||
|
|
||
| if len(args.paths) > 1: | ||
| if args.l: | ||
| print(f"{total_lines:>{width_lines}} total") | ||
| elif args.w: | ||
| print(f"{total_words:>{width_words}} total") | ||
| elif args.c: | ||
| print(f"{total_chars:>{width_chars}} total") | ||
| else: | ||
| print(f"{total_lines:>{width_lines}}{total_words:>{width_words}}{total_chars:>{width_chars}} 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.
is there any difference between when -1 is used and when it is not?