generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Cape Town| 25-SDC-July | Faith Muzondo | Sprint 4| Implement shell tools python #145
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
Faithy4444
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
Faithy4444: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 4 commits
Commits
Show all changes
5 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,37 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat", | ||
| description="Read, display, and concatenate text files.", | ||
| ) | ||
|
|
||
| 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-blank output lines.") | ||
| parser.add_argument("paths", nargs="+", help="The file(s) to read.") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| for path in args.paths: | ||
| try: | ||
| with open(path, mode='r', encoding='utf-8') as f: | ||
| lines = f.readlines() | ||
| except Exception as err: | ||
| print(f"Error reading file '{path}': {err}") | ||
| continue | ||
|
|
||
| line_num = 1 | ||
|
|
||
| for line in lines: | ||
| if args.number_nonblank: | ||
| if line.strip() != "": | ||
| print(f"{line_num:6}\t{line}", end="") | ||
| line_num += 1 | ||
| else: | ||
| print(line, end="") | ||
| elif args.number: | ||
| print(f"{line_num:6}\t{line}", end="") | ||
| line_num += 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,31 @@ | ||
| import os | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls", | ||
| description="List directory contents." | ||
| ) | ||
|
|
||
| parser.add_argument("-1", "--one-per-line", action="store_true", help="List one file per line") | ||
| parser.add_argument("-a", "--all", action="store_true", help="Include hidden files (those starting with .)") | ||
| parser.add_argument("paths", nargs="*", default=["."], help="Directory path(s) to list") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| for path in args.paths: | ||
| try: | ||
| entries = os.listdir(path) | ||
| except Exception as e: | ||
| print(f"ls: cannot access '{path}': {e}") | ||
| continue | ||
|
|
||
| if not args.all: | ||
| entries = [entry for entry in entries if not entry.startswith(".")] | ||
|
|
||
| entries.sort() | ||
|
|
||
| if args.one_per_line: | ||
| for entry in entries: | ||
| print(entry) | ||
| else: | ||
| print(" ".join(entries)) |
|
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. There is some duplication of code here - can you think how to reduce this?
Author
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. i have removed the duplicate code |
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,52 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="wc", | ||
| description="Counts lines, words, and bytes in text files." | ||
| ) | ||
|
|
||
| parser.add_argument("-l", "--lines", action="store_true", help="Print the line 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("paths", nargs="+", help="One or more files to process") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not (args.lines or args.words or args.bytes): | ||
| args.lines = args.words = args.bytes = True | ||
|
|
||
| total_lines = total_words = total_bytes = 0 | ||
|
|
||
| for path in args.paths: | ||
| try: | ||
| with open(path, "rb") as file: | ||
| content = file.read() | ||
| except Exception as e: | ||
| print(f"wc: {path}: {e}") | ||
| continue | ||
|
|
||
| lines = content.count(b'\n') | ||
| words = len(content.decode('utf-8', errors='ignore').split()) | ||
| byte_count = len(content) | ||
|
|
||
| if args.lines: | ||
| print(f"{lines:>8}", end=" ") | ||
| if args.words: | ||
| print(f"{words:>8}", end=" ") | ||
| if args.bytes: | ||
| print(f"{byte_count:>8}", end=" ") | ||
|
|
||
| print(f"{path}") | ||
|
|
||
| total_lines += lines | ||
| total_words += words | ||
| total_bytes += byte_count | ||
|
|
||
| if len(args.paths) > 1: | ||
| if args.lines: | ||
| print(f"{total_lines:>8}", end=" ") | ||
| if args.words: | ||
| print(f"{total_words:>8}", end=" ") | ||
| if args.bytes: | ||
| print(f"{total_bytes:>8}", end=" ") | ||
| print("total") |
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
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
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.
Try running the original cat program and comparing your output, do you see any differences?
Also, there is some code duplication in here, can you figure out how to clean that up?