generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
ZA | 25-SDC-Nov | Rashaad Ebrahim | Sprint 4 | Implement Shell Tools (Python) #276
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
Rashaad-Ebrahim
wants to merge
13
commits into
CodeYourFuture:main
Choose a base branch
from
Rashaad-Ebrahim: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
13 commits
Select commit
Hold shift + click to select a range
ac1aaf7
New files added for each command
Rashaad-Ebrahim 06abf21
Project set up
Rashaad-Ebrahim d9799f8
Basic cat command implemented
Rashaad-Ebrahim 621a896
-n and -b implemented
Rashaad-Ebrahim 5f62f75
gitignore updated
Rashaad-Ebrahim d843352
argparse information added to ls.py
Rashaad-Ebrahim 8ee7c0e
ls command implemented
Rashaad-Ebrahim 7d85fc5
wc argparse info loaded
Rashaad-Ebrahim 95b2ec8
logic for file details complete
Rashaad-Ebrahim d8dc9a7
Logic for totals completed.
Rashaad-Ebrahim 66b0147
wc implemented and working for wc sample-files/*
Rashaad-Ebrahim 58e5807
Code cleaned up
Rashaad-Ebrahim 7df0722
**/requirements.txt removed from gitignore file
Rashaad-Ebrahim 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,3 @@ | ||
| 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,51 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="py-cat", | ||
| description="A Python implementation of the Unix cat command", | ||
| ) | ||
|
|
||
| parser.add_argument("-n", "--number", action="store_true", help="Number all output lines") | ||
| parser.add_argument("-b", "--numberNonBlank", action="store_true", help="Numbers only non-empty lines. Overrides -n option") | ||
| parser.add_argument("path", nargs="+", help="The file path to process") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| def number_all(lines): | ||
| numbered = [] | ||
| for i, line in enumerate(lines): | ||
| numbered.append(f"{i + 1:>6}\t{line}") | ||
| return numbered | ||
|
|
||
| def number_non_blank(lines): | ||
| numbered = [] | ||
| counter = 1 | ||
| for line in lines: | ||
| if line == "": | ||
| numbered.append(line) | ||
| else: | ||
| numbered.append(f"{counter:>6}\t{line}") | ||
| counter += 1 | ||
| return numbered | ||
|
|
||
| # Read and concatenate file contents | ||
| content = "" | ||
|
|
||
| for path in args.path: | ||
| with open(path, "r") as f: | ||
| content += f.read() | ||
|
|
||
| if content.endswith("\n"): | ||
| content = content[:-1] | ||
|
|
||
| # Split content into lines | ||
| lines = content.split("\n") | ||
|
|
||
| # Output logic | ||
|
|
||
| if args.numberNonBlank: | ||
| print("\n".join(number_non_blank(lines))) | ||
| elif args.number: | ||
| print("\n".join(number_all(lines))) | ||
| else: | ||
| print("\n".join(lines)) | ||
Rashaad-Ebrahim 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,25 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="py-ls", | ||
| description="A Python implementation of the Unix ls command", | ||
| ) | ||
|
|
||
| parser.add_argument("-1", dest="_1", action="store_true", help="List one file per line") | ||
| parser.add_argument("-a", "--all", action="store_true", help="Include entries that begin with a dot (.)") | ||
| parser.add_argument("path", nargs="?", default=".", help="Directory to list") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| files = os.listdir(args.path) | ||
|
|
||
| if args.all: | ||
| files = [".", ".."] + files | ||
| else: | ||
| files = [file for file in files if not file.startswith(".")] | ||
|
|
||
| if args._1: | ||
| print("\n".join(files)) | ||
| else: | ||
| print(" ".join(files)) |
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,89 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="py-wc", | ||
| description="A Python implementation of the Unix wc command", | ||
| ) | ||
|
|
||
| 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("path", nargs="+", help="The file path to process") | ||
|
|
||
| args = parser.parse_args() | ||
| file_paths = args.path | ||
| lines_flag, words_flag, bytes_flag = args.lines, args.words, args.bytes | ||
|
|
||
| def format_output(details_list, totals=None, flags=None): | ||
| lines_flag, words_flag, bytes_flag = flags | ||
| show_all = not (lines_flag or words_flag or bytes_flag) | ||
| output_lines = [] | ||
|
|
||
| # Per-file output | ||
| for d in details_list: | ||
| line = "" | ||
|
|
||
| if show_all or lines_flag: | ||
| line += f"{d['line_count']:>3} " | ||
|
|
||
| if show_all or words_flag: | ||
| line += f"{d['word_count']:>3} " | ||
|
|
||
| if show_all or bytes_flag: | ||
| line += f"{d['file_size']:>3} " | ||
|
|
||
| line += d["file_path"] | ||
| output_lines.append(line) | ||
|
|
||
| # Totals (only if more than one file) | ||
| if totals and len(details_list) > 1: | ||
| total_line = "" | ||
|
|
||
| if show_all or lines_flag: | ||
| total_line += f"{totals['line_count']:>3} " | ||
|
|
||
| if show_all or words_flag: | ||
| total_line += f"{totals['word_count']:>3} " | ||
|
|
||
| if show_all or bytes_flag: | ||
| total_line += f"{totals['file_size']:>3} " | ||
|
|
||
| total_line += "total" | ||
| output_lines.append(total_line) | ||
|
|
||
| return "\n".join(output_lines) | ||
|
|
||
|
|
||
| # Collect file details | ||
| file_details_list = [] | ||
| line_count_total = 0 | ||
| word_count_total = 0 | ||
| file_size_total = 0 | ||
|
|
||
| for file_path in file_paths: | ||
| with open(file_path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
|
|
||
| details = { | ||
| "line_count": content.count("\n"), | ||
| "word_count": len(content.split()), | ||
| "file_size": os.path.getsize(file_path), | ||
| "file_path": file_path, | ||
| } | ||
|
|
||
| line_count_total += details["line_count"] | ||
| word_count_total += details["word_count"] | ||
| file_size_total += details["file_size"] | ||
|
|
||
| file_details_list.append(details) | ||
|
|
||
| totals_details = { | ||
| "line_count": line_count_total, | ||
| "word_count": word_count_total, | ||
| "file_size": file_size_total, | ||
| } | ||
|
|
||
| # Final output | ||
| flags = (lines_flag, words_flag, bytes_flag) | ||
| print(format_output(file_details_list, totals_details, flags)) |
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.