-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26-Jul-SDC | Roman Sanaye | Sprint 4 | Implement shell tool in Python #637
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
RomanSanaye
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
RomanSanaye: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.
+197
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7070feb
Implemented cat with python
RomanSanaye b5eff5f
Implemented ls with python
RomanSanaye a73b96a
Implemented wc in python version
RomanSanaye f68a675
Use argparse for cat argument parsing
RomanSanaye 6ef3bd5
Use argparse for ls argument parsing
RomanSanaye 860d37b
Use argparse and improve wc output formatting
RomanSanaye 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,61 @@ | ||
| # in built module | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
|
|
||
| parser.add_argument("-n", action="store_true") | ||
| parser.add_argument("-b", action="store_true") | ||
| parser.add_argument("file_paths", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| flag = "" | ||
|
|
||
| if args.n: | ||
| flag = "-n" | ||
| elif args.b: | ||
| flag = "-b" | ||
|
|
||
| file_paths = args.file_paths | ||
|
|
||
|
|
||
| # Read all files and combine their contents into one string | ||
| content = "" | ||
|
|
||
| for file in file_paths: | ||
| with open(file, "r") as f: | ||
| content += f.read() | ||
|
|
||
|
|
||
| # Split the file content into separate lines | ||
| lines = content.splitlines() | ||
|
|
||
|
|
||
| # Handle -n flag: add a number to every line | ||
| if flag == "-n": | ||
| new_lines = [] | ||
|
|
||
| for index, line in enumerate(lines): | ||
| new_lines.append(f"{index + 1} {line}") | ||
|
|
||
| lines = new_lines | ||
|
|
||
|
|
||
| # Handle -b flag: number only non-empty lines | ||
| if flag == "-b": | ||
| line_number = 1 | ||
| new_lines = [] | ||
|
|
||
| for line in lines: | ||
| # Keep empty lines without adding numbers | ||
| if line.strip() == "": | ||
| new_lines.append(line) | ||
|
|
||
| # Add a number only to lines that contain text | ||
| else: | ||
| new_lines.append(f"{line_number} {line}") | ||
| line_number += 1 | ||
|
|
||
| lines = new_lines | ||
|
|
||
| print("\n".join(lines)) |
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 | ||
| import os | ||
|
|
||
| # Set up command-line argument parser | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| # Add supported flags and file/directory paths | ||
| parser.add_argument("-a", action="store_true") | ||
| parser.add_argument("-1", dest="one", action="store_true") | ||
| parser.add_argument("paths", nargs="*") | ||
|
|
||
| # Parse the user's command-line arguments | ||
| args = parser.parse_args() | ||
|
|
||
| flags = [] | ||
|
|
||
| if args.a: | ||
| flags.append("-a") | ||
|
|
||
| if args.one: | ||
| flags.append("-1") | ||
|
|
||
| paths = args.paths | ||
|
|
||
|
|
||
| # Use the current directory if no path is provided | ||
| if not paths: | ||
| paths = ["."] | ||
|
|
||
|
|
||
| # Process each path | ||
| for path in paths: | ||
|
|
||
| # If the path is a file, print its name | ||
| if os.path.isfile(path): | ||
| print(path) | ||
|
|
||
| # If the path is a directory, get its contents | ||
| elif os.path.isdir(path): | ||
| contents = os.listdir(path) | ||
| if "-a" not in flags: | ||
| contents = [file for file in contents if not file.startswith(".")] | ||
| if "-1" in flags: | ||
| print("\n".join(contents)) | ||
| else: | ||
| print(" ".join(contents)) | ||
| # Handle invalid paths | ||
| else: | ||
| print(f"No such file or directory: {path}") |
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,87 @@ | ||
| import argparse | ||
|
|
||
| # Set up command-line argument parser | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| # Add supported flags | ||
| parser.add_argument("-l", action="store_true") | ||
| parser.add_argument("-w", action="store_true") | ||
| parser.add_argument("-c", action="store_true") | ||
|
|
||
| # Accept one or more file paths | ||
| parser.add_argument("paths", nargs="+") | ||
|
|
||
| # Parse the user's command-line arguments | ||
| args = parser.parse_args() | ||
|
|
||
| flags = [] | ||
|
|
||
| # Store selected flags for the existing logic | ||
| if args.l: | ||
| flags.append("-l") | ||
|
|
||
| if args.w: | ||
| flags.append("-w") | ||
|
|
||
| if args.c: | ||
| flags.append("-c") | ||
|
|
||
| paths = args.paths | ||
|
|
||
|
|
||
| # Build the output based on the selected flags | ||
| def get_output(lines, words, chars, flags): | ||
| output = [] | ||
|
|
||
| # Show all counts when no flag is provided | ||
| if len(flags) == 0: | ||
| output = [lines, words, chars] | ||
|
|
||
| else: | ||
| if "-l" in flags: | ||
| output.append(lines) | ||
|
|
||
| if "-w" in flags: | ||
| output.append(words) | ||
|
|
||
| if "-c" in flags: | ||
| output.append(chars) | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_chars = 0 | ||
|
|
||
|
|
||
| # Process each file | ||
| for file in paths: | ||
|
|
||
| # Read file as bytes | ||
| with open(file, "rb") as f: | ||
| content = f.read() | ||
|
|
||
| # Count lines, words, and characters | ||
| lines = content.count(b"\n") | ||
| words = len(content.split()) | ||
| chars = len(content) | ||
|
|
||
| # Create and print output for this file | ||
| output = get_output(lines, words, chars, flags) | ||
|
|
||
| # Print counts with aligned columns | ||
| print(" ".join(f"{value:3}" for value in output), file) | ||
|
|
||
| # Add this file's counts to the totals | ||
| total_lines += lines | ||
| total_words += words | ||
| total_chars += chars | ||
|
|
||
|
|
||
| # Print total only when multiple files are provided | ||
| if len(paths) > 1: | ||
| total_output = get_output(total_lines, total_words, total_chars, flags) | ||
|
|
||
| # Print aligned total | ||
| print(" ".join(f"{value:3}" for value in total_output), "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 run this with multiple files, how does the output look? What change could make it neater?
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.
Thanks. I’ve updated the output formatting to make the results neater when using multiple files.