generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 66
London | 25-SDC-July | Eyuel Abraham | Sprint 4 | Implement shell tool python #154
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
eyuell21
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
eyuell21:implement-shell-tool-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
6 commits
Select commit
Hold shift + click to select a range
5b484f1
Completed implementing shell tools in python.
eyuell21 a18c426
added .venv files to gitignore
eyuell21 fded319
Fixes according to review
eyuell21 53a9aa9
Add support for -1 (single column) flag to ls implementation
eyuell21 5981cd7
Added a helper function to Format the output
eyuell21 b47f75e
Implement single column output for directory entries
eyuell21 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 +1,2 @@ | ||
| node_modules | ||
| .venv/ |
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,68 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description="Reads and prints one or more files, optionally numbering lines continuously" | ||
| ) | ||
| parser.add_argument("paths", nargs='+', help="One or more file paths") | ||
| 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") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| file_paths = args.paths | ||
| number_nonblank = args.number_nonblank | ||
| number_all = args.number and not number_nonblank | ||
|
|
||
|
|
||
| total_lines = 0 | ||
| total_nonblank_lines = 0 | ||
|
|
||
| for path in file_paths: | ||
| try: | ||
| with open(path, 'r', encoding='utf-8') as f: | ||
| lines = f.readlines() | ||
| total_lines += len(lines) | ||
| total_nonblank_lines += sum(1 for line in lines if line.strip()) | ||
| except Exception as e: | ||
| print(f'Error reading file "{path}": {e}', file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if number_nonblank: | ||
| max_digits = len(str(total_nonblank_lines)) | ||
| elif number_all: | ||
| max_digits = len(str(total_lines)) | ||
|
|
||
| line_number = 1 | ||
|
|
||
|
|
||
| for path in file_paths: | ||
| try: | ||
| with open(path, 'r', encoding='utf-8') as f: | ||
| lines = f.readlines() | ||
|
|
||
| if number_nonblank: | ||
| for line in lines: | ||
| if line.strip() == "": | ||
| print() | ||
| else: | ||
| num_str = str(line_number).rjust(max_digits) | ||
| print(f"{num_str}\t{line.rstrip()}") | ||
| line_number += 1 | ||
|
|
||
| elif number_all: | ||
| for line in lines: | ||
| num_str = str(line_number).rjust(max_digits) | ||
| print(f"{num_str}\t{line.rstrip()}") | ||
| line_number += 1 | ||
|
|
||
| else: | ||
| for line in lines: | ||
| print(line, end='') | ||
| if lines and not lines[-1].endswith('\n'): | ||
| print() | ||
|
|
||
| except Exception as e: | ||
| print(f'Error reading file "{path}": {e}', file=sys.stderr) | ||
| sys.exit(1) |
|
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. This is a very good implementation, but I think you might have misread |
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,60 @@ | ||
| import os | ||
| import sys | ||
| import stat | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description="List files in a directory (simplified ls implementation)" | ||
| ) | ||
| parser.add_argument("paths", nargs="*", default=["."], help="One or more file or directory paths") | ||
| parser.add_argument("-l", "--longList", action="store_true", help="Long listing format") | ||
| parser.add_argument("-a", "--all", action="store_true", help="Include hidden files") | ||
|
|
||
| parser.add_argument("-1", "--singleColumn", action="store_true", help="List one file per line") | ||
| args = parser.parse_args() | ||
|
|
||
| file_paths = args.paths | ||
| show_long = args.longList | ||
| show_all = args.all | ||
| force_single_column = args.singleColumn | ||
|
|
||
| def format_permissions(mode): | ||
| return stat.filemode(mode) | ||
|
|
||
| for input_path in file_paths: | ||
| try: | ||
| if not os.path.exists(input_path): | ||
| raise FileNotFoundError(f'No such file or directory: {input_path}') | ||
|
|
||
| if os.path.isfile(input_path): | ||
| if show_long: | ||
| file_stat = os.stat(input_path) | ||
| perms = format_permissions(file_stat.st_mode) | ||
| size = str(file_stat.st_size).rjust(6) | ||
| print(f"{perms} {size} {input_path}") | ||
| else: | ||
| print(input_path) | ||
|
|
||
| elif os.path.isdir(input_path): | ||
| entries = os.listdir(input_path) | ||
| if not show_all: | ||
| entries = [e for e in entries if not e.startswith(".")] | ||
|
|
||
| # Optional: sort entries for consistent output | ||
| entries.sort() # (optional for predictable output) | ||
|
|
||
| for entry in entries: | ||
| full_path = os.path.join(input_path, entry) | ||
| entry_stat = os.stat(full_path) | ||
| if show_long: | ||
| perms = format_permissions(entry_stat.st_mode) | ||
| size = str(entry_stat.st_size).rjust(6) | ||
| print(f"{perms} {size} {entry}") | ||
| elif force_single_column: | ||
| print(entry) | ||
| else: | ||
| print(entry) | ||
|
|
||
| except Exception as e: | ||
| print(f'Error reading "{input_path}": {e}', file=sys.stderr) | ||
| sys.exit(1) |
LonMcGregor marked this conversation as resolved.
Show resolved
Hide resolved
|
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,68 @@ | ||
| import os | ||
| import sys | ||
| import argparse | ||
|
|
||
| # CLI argument parsing | ||
| parser = argparse.ArgumentParser(description="Simplified implementation of wc") | ||
| parser.add_argument("paths", nargs="*", default=["."], help="One or more file or directory paths") | ||
| parser.add_argument("-l", "--line", action="store_true", help="Count lines") | ||
| parser.add_argument("-w", "--word", action="store_true", help="Count words") | ||
| parser.add_argument("-c", "--character", action="store_true", help="Count characters") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # If no flags are set, show all | ||
| show_all = not (args.line or args.word or args.character) | ||
|
|
||
| # Count content in a string | ||
| def count_content(content): | ||
| lines = content.splitlines() | ||
| words = content.strip().split() | ||
| characters = len(content) | ||
| return len(lines), len(words), characters | ||
|
|
||
| # Format output line based on flags | ||
| def format_output(lines, words, chars, label): | ||
| parts = [] | ||
| if args.line or show_all: | ||
| parts.append(f"{lines:8}") | ||
| if args.word or show_all: | ||
| parts.append(f"{words:8}") | ||
| if args.character or show_all: | ||
| parts.append(f"{chars:8}") | ||
| parts.append(label) | ||
| return " ".join(parts) | ||
|
|
||
| # Totals for multiple files | ||
| total = {"lines": 0, "words": 0, "characters": 0} | ||
| file_count = 0 | ||
|
|
||
| for input_path in args.paths: | ||
| try: | ||
| if os.path.isdir(input_path): | ||
| print(f"{input_path} is a directory. Skipping.") | ||
| continue | ||
|
|
||
| with open(input_path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
|
|
||
| lines, words, characters = count_content(content) | ||
|
|
||
| total["lines"] += lines | ||
| total["words"] += words | ||
| total["characters"] += characters | ||
| file_count += 1 | ||
|
|
||
| print(format_output(lines, words, characters, input_path)) | ||
|
|
||
| except Exception as e: | ||
| print(f'Error reading "{input_path}": {e}', file=sys.stderr) | ||
|
|
||
| # Print totals if more than one file processed | ||
| if file_count > 1: | ||
| print(format_output( | ||
| total["lines"], | ||
| total["words"], | ||
| total["characters"], | ||
| "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.
Uh oh!
There was an error while loading. Please reload this page.