-
-
Notifications
You must be signed in to change notification settings - Fork 88
London | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 4 | Implement Shell Tools-Python #261
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
zohrehKazemianpour
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
zohrehKazemianpour:shell-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 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
53fa600
Add prep to gitignore
zohrehKazemianpour 6a33a5e
Implement basic cat command in Python
zohrehKazemianpour d1c1354
Add -n flag to number lines in cat command
zohrehKazemianpour 6607cde
Add -b flag to number only non-empty lines
zohrehKazemianpour d1ed6ca
Add support for multiple files in cat command
zohrehKazemianpour ba64d28
implement ls-1 command
zohrehKazemianpour 37c7132
Add -a flag to ls to show hidden files
zohrehKazemianpour 5d1476b
Implement basic wc command in Python
zohrehKazemianpour df5b8e5
Implement wc command with -l, -w, and -c flags
zohrehKazemianpour 3602022
Add total line for multiple files in wc
zohrehKazemianpour 3b7cf4b
Address PR review: reduce duplication and fix flag handling
zohrehKazemianpour 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 | ||
| prep/ |
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,50 @@ | ||
| import sys | ||
| import argparse | ||
|
|
||
| # Setup argument parser | ||
| parser = argparse.ArgumentParser( | ||
| prog="cat", | ||
| description="Concatenate and display 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-empty lines only") | ||
| parser.add_argument("paths", nargs='+', help="Files to read") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| line_number = 0 # Shared counter across all files | ||
|
|
||
| # Process each file | ||
| for path in args.paths: # LEVEL 1: for loop starts | ||
| with open(path, "r") as f: # LEVEL 2: inside for loop | ||
| content = f.read() # LEVEL 3: inside with block | ||
|
|
||
| # Check if numbering is needed | ||
| if args.number: # LEVEL 2: inside for loop | ||
| lines = content.split("\n") # LEVEL 3: inside if | ||
| numbered_lines = [] | ||
|
|
||
| for index, line in enumerate(lines): # LEVEL 3: inside if | ||
| line_number = line_number + 1 # LEVEL 4: inside inner for | ||
| numbered_line = f"{line_number:6}\t{line}" | ||
| numbered_lines.append(numbered_line) | ||
|
|
||
| print("\n".join(numbered_lines)) # LEVEL 3: inside if | ||
|
|
||
| elif args.number_nonblank: # LEVEL 2: inside for loop | ||
| lines = content.split("\n") # LEVEL 3: inside elif | ||
| numbered_lines = [] | ||
|
|
||
| for line in lines: # LEVEL 3: inside elif | ||
| if line.strip() == "": # LEVEL 4: inside inner for | ||
| numbered_lines.append(line) | ||
| else: | ||
| line_number = line_number + 1 | ||
| numbered_line = f"{line_number:6}\t{line}" | ||
| numbered_lines.append(numbered_line) | ||
|
|
||
| print("\n".join(numbered_lines)) | ||
|
|
||
| else: # LEVEL 2: inside for loop | ||
| print(content) # LEVEL 3: inside else | ||
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,24 @@ | ||
| import os | ||
| import argparse | ||
|
|
||
| # Setup argument parser | ||
| parser = argparse.ArgumentParser( | ||
| prog="ls", | ||
| description="Lists contents of a directory" | ||
| ) | ||
|
|
||
| parser.add_argument("-1", dest="one_column", action="store_true", help="List one file per line") | ||
| parser.add_argument("-a", "--all", action="store_true", help="Include hidden files") | ||
| parser.add_argument("path", nargs='?', default=".", help="Directory to list (default: current directory)") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # List directory contents | ||
| files = os.listdir(args.path) | ||
|
|
||
| # Filter out hidden files unless -a flag is used | ||
| if not args.all: | ||
| files = [f for f in files if not f.startswith('.')] | ||
|
|
||
| for file in files: | ||
| print(file) | ||
|
LonMcGregor marked this conversation as resolved.
Outdated
|
||
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,55 @@ | ||
| import argparse | ||
|
|
||
| # Setup argument parser | ||
| parser = argparse.ArgumentParser( | ||
| prog="wc", | ||
| description="Count lines, words, and characters" | ||
| ) | ||
| parser.add_argument("paths", nargs='+', help="Files to count") | ||
| parser.add_argument("-l", "--lines", action="store_true", help="Count lines only") | ||
| parser.add_argument("-w", "--words", action="store_true", help="Count words only") | ||
| parser.add_argument("-c", "--chars", action="store_true", help="Count characters only") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Track totals | ||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_chars = 0 | ||
|
|
||
| for path in args.paths: | ||
| # Read the file | ||
| with open(path, "r") as f: | ||
| content = f.read() | ||
|
|
||
| # Count lines, words, characters | ||
| lines = len(content.rstrip('\n').split('\n')) | ||
| words = len(content.split()) | ||
| chars = len(content) | ||
|
|
||
| # Add to totals | ||
| total_lines += lines | ||
| total_words += words | ||
| total_chars += chars | ||
|
|
||
| if args.lines: | ||
|
LonMcGregor marked this conversation as resolved.
Outdated
|
||
| print(f"{lines:8} {path}") | ||
| elif args.words: | ||
| print(f"{words:8} {path}") | ||
| elif args.chars: | ||
| print(f"{chars:8} {path}") | ||
| else: | ||
| print(f"{lines:8}{words:8}{chars:8} {path}") | ||
|
|
||
|
|
||
| # Print totals if multiple files | ||
| if len(args.paths) > 1: | ||
| if args.lines: | ||
| print(f"{total_lines:8} total") | ||
| elif args.words: | ||
| print(f"{total_words:8} total") | ||
| elif args.chars: | ||
| print(f"{total_chars:8} total") | ||
| else: | ||
| print(f"{total_lines:8}{total_words:8}{total_chars:8} total") | ||
|
LonMcGregor marked this conversation as resolved.
Outdated
|
||
|
|
||
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.