-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26-jul-SDC | Shaghayegh far| Sprint 4 | Implement shell tools with python #653
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
shaghayeghfar
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
shaghayeghfar: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
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,43 @@ | ||
| import sys | ||
|
|
||
| # Check if the user used a flag | ||
| if len(sys.argv) > 1 and sys.argv[1] == "-n": | ||
|
|
||
| # Start from the second argument because the first is "-n" | ||
| for filename in sys.argv[2:]: | ||
|
|
||
| with open(filename, "r") as file: | ||
|
|
||
| line_number = 1 | ||
|
|
||
| for line in file: | ||
| print(f"{line_number} {line}", end="") | ||
| line_number += 1 | ||
|
|
||
|
|
||
| elif len(sys.argv) > 1 and sys.argv[1] == "-b": | ||
|
|
||
| # Start from the second argument because the first is "-b" | ||
| for filename in sys.argv[2:]: | ||
|
|
||
| with open(filename, "r") as file: | ||
|
|
||
| line_number = 1 | ||
|
|
||
| for line in file: | ||
|
|
||
| # Only number non-empty lines | ||
| if line.strip() == "": | ||
| print(line, end="") | ||
| else: | ||
| print(f"{line_number} {line}", end="") | ||
| line_number += 1 | ||
|
|
||
|
|
||
| else: | ||
|
|
||
| # No flag, just print every file | ||
| for filename in sys.argv[1:]: | ||
|
|
||
| with open(filename, "r") as file: | ||
| print(file.read(), 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,65 @@ | ||
| import sys | ||
| import os | ||
|
|
||
|
|
||
| # Get arguments from the command line | ||
| arguments = sys.argv[1:] | ||
|
|
||
|
|
||
| # Check if -1 flag exists | ||
| show_one_per_line = False | ||
|
|
||
| if "-1" in arguments: | ||
| show_one_per_line = True | ||
| arguments.remove("-1") | ||
|
|
||
|
|
||
| # Check if -a flag exists | ||
| show_all_files = False | ||
|
|
||
| if "-a" in arguments: | ||
| show_all_files = True | ||
| arguments.remove("-a") | ||
|
|
||
|
|
||
| # If no path is given, use current directory | ||
| if len(arguments) == 0: | ||
| path = "." | ||
| else: | ||
| path = arguments[0] | ||
|
|
||
|
|
||
| # Check if the path exists | ||
| if not os.path.exists(path): | ||
| print(f"ls: cannot access '{path}': No such file or directory") | ||
| sys.exit() | ||
|
|
||
|
|
||
| # If the path is a file, just print the file name | ||
| if os.path.isfile(path): | ||
| print(path) | ||
|
|
||
|
|
||
| # If the path is a folder, list its contents | ||
| else: | ||
|
|
||
| files = os.listdir(path) | ||
|
|
||
|
|
||
| # Remove hidden files unless -a was used | ||
| if not show_all_files: | ||
| files = [ | ||
| file for file in files | ||
| if not file.startswith(".") | ||
| ] | ||
|
|
||
|
|
||
| # Print one file per line | ||
| if show_one_per_line: | ||
| for file in files: | ||
| print(file) | ||
|
|
||
|
|
||
| # Normal ls behaviour | ||
| 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,88 @@ | ||
| import sys | ||
|
|
||
|
|
||
| # Get everything the user typed after wc.py | ||
| arguments = sys.argv[1:] | ||
|
|
||
|
|
||
| # These are our options | ||
| count_lines = False | ||
| count_words = False | ||
| count_characters = False | ||
|
|
||
|
|
||
| # Store file names here | ||
| filenames = [] | ||
|
|
||
|
|
||
| # Check every argument | ||
| for argument in arguments: | ||
|
|
||
| if argument == "-l": | ||
| count_lines = True | ||
|
|
||
| elif argument == "-w": | ||
| count_words = True | ||
|
|
||
| elif argument == "-c": | ||
| count_characters = True | ||
|
|
||
| else: | ||
| filenames.append(argument) | ||
|
|
||
|
|
||
|
|
||
| # If the user did not give any flag, | ||
| # show all counts like normal wc | ||
| if (not count_lines | ||
| and not count_words | ||
| and not count_characters): | ||
|
|
||
| count_lines = True | ||
| count_words = True | ||
| count_characters = True | ||
|
|
||
|
|
||
|
|
||
| # Go through every file | ||
| for filename in filenames: | ||
|
|
||
| # Open the file | ||
| with open(filename, "r") as file: | ||
|
|
||
| # Read the whole file | ||
| content = file.read() | ||
|
|
||
|
|
||
| # Count lines | ||
| lines = len(content.splitlines()) | ||
|
|
||
|
Contributor
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. Why are you leaving so much space between lines here? |
||
|
|
||
| # Count words | ||
| words = len(content.split()) | ||
|
|
||
|
|
||
| # Count characters | ||
| characters = len(content) | ||
|
|
||
|
|
||
|
|
||
| # Prepare the answer | ||
| answer = "" | ||
|
|
||
|
|
||
| if count_lines: | ||
| answer += str(lines) + " " | ||
|
|
||
|
|
||
| if count_words: | ||
| answer += str(words) + " " | ||
|
|
||
|
|
||
| if count_characters: | ||
| answer += str(characters) + " " | ||
|
|
||
|
|
||
|
|
||
| # Print the result | ||
| print(answer + filename) | ||
|
Contributor
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. If you run this with multiple files, is there anything missing? |
||
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.
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.
Is there a library or API you could use to handle arguments for you?