-
-
Notifications
You must be signed in to change notification settings - Fork 92
NW | 2026-mar-sdc | Zabihollah Namazi | Sprint 4 | implement shell tools with python #453
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
ZabihollahNamazi
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
ZabihollahNamazi: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.
+176
−0
Open
Changes from all 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 |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| node_modules | ||
| .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,48 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat shell tool", | ||
| description="making cat tool with oython" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "files", | ||
| nargs="+", | ||
| help="file to read" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-n", | ||
| "--number", | ||
| action="store_true", | ||
| help="add number to the lines" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-b", | ||
| "--number_nonblank", | ||
| action="store_true", | ||
| help="does not number the blank lines" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| number_line = 0 | ||
| for file in args.files: | ||
| with open(file, "r") as f: | ||
| lines = f.readlines() | ||
|
|
||
| for line in lines: | ||
| if args.number_nonblank: | ||
| if line.strip(): | ||
| number_line += 1 | ||
| print(f"{number_line} {line}", end="") | ||
| elif args.number: | ||
| number_line += 1 | ||
| print(f"{number_line} {line}", end="") | ||
| else: | ||
| print(line) | ||
|
|
||
|
|
||
|
|
||
|
|
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 @@ | ||
|
|
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,42 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls shell tool", | ||
| description="making ls tool with python" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-1", | ||
| dest="one_per_line", | ||
| action="store_true", | ||
| help="lists one per line" | ||
| ) | ||
| parser.add_argument( | ||
| "-a", | ||
| dest="show_hidden", | ||
| action="store_true", | ||
| help="show files even hidden files" | ||
| ) | ||
| parser.add_argument( | ||
| "items", | ||
| nargs="*", #defualt to current directory | ||
| help="items to list" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| paths = args.items if args.items else ['.'] | ||
|
|
||
| for path in paths: | ||
| try: | ||
| entries = os.listdir(path) | ||
| if not args.show_hidden: | ||
| entries = [e for e in entries if not e.startswith(".")] | ||
| if args.one_per_line: | ||
| for entry in entries: | ||
| print(entry) | ||
| else: | ||
| print(" ".join(entries)) | ||
| except: | ||
| print(f"ls can not access this 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,82 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="wc shell tool", | ||
| description="making wc tool with python" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "files", | ||
| nargs="+", | ||
| help="file or files to work on" | ||
| ) | ||
| parser.add_argument( | ||
| "-w", | ||
| action="store_true", | ||
| help="counting words" | ||
| ) | ||
| parser.add_argument( | ||
| "-l", | ||
| action="store_true", | ||
| help="counting lines" | ||
| ) | ||
| parser.add_argument( | ||
| "-c", | ||
| action="store_true", | ||
| help="counting char" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_chars = 0 | ||
|
|
||
| for file in args.files: | ||
| try: | ||
| if os.path.isdir(file): | ||
| print(f"wc: {file}: read: Is a directory") | ||
| continue | ||
|
|
||
| with open(file, "rb") as f: | ||
| content = f.read() | ||
| word_count = len(content.decode('utf-8', errors='ignore').split()) | ||
| line_count = content.count(b"\n") | ||
| char_count = len(content) | ||
|
|
||
| total_lines += line_count | ||
| total_words += word_count | ||
| total_chars += char_count | ||
|
|
||
| output_parts = [] | ||
| if args.l: | ||
| output_parts.append(f"{line_count:>7}") | ||
| if args.w: | ||
| output_parts.append(f"{word_count:>7}") | ||
| if args.c: | ||
| output_parts.append(f"{char_count:>7}") | ||
|
|
||
| if not args.w and not args.l and not args.c: | ||
| print(f"{line_count:>7}{word_count:>7}{char_count:>7} {file}") | ||
| else: | ||
| output_parts.append(f" {file}") | ||
| print("".join(output_parts)) | ||
|
|
||
| except FileNotFoundError: | ||
| print(f"wc: {file}: open: No such file or directory") | ||
|
|
||
| if len(args.files) > 1: | ||
| total_parts = [] | ||
| if args.l: | ||
| total_parts.append(f"{total_lines:>7}") | ||
| if args.w: | ||
| total_parts.append(f"{total_words:>7}") | ||
| if args.c: | ||
| total_parts.append(f"{total_chars:>7}") | ||
|
|
||
| if not args.w and not args.l and not args.c: | ||
| print(f"{total_lines:>7}{total_words:>7}{total_chars:>7} total") | ||
| else: | ||
| total_parts.append(" total") | ||
| print("".join(total_parts)) | ||
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.
Good start. Some of this code is repeated quite a lot though, like all the printing, formatting. Can you find a way to generalise and reduce some of this duplication?