-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26-Jul-SDC | Roman Sanaye | Sprint 4 | Implement shell tool in Python #637
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
7070feb
b5eff5f
a73b96a
f68a675
6ef3bd5
860d37b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # in built module | ||
| import sys | ||
|
|
||
| args = sys.argv[1:] | ||
|
|
||
| flag = "" | ||
| file_paths = [] | ||
|
|
||
|
|
||
| # Check if the first argument is a flag like -n or -b | ||
| if args and args[0].startswith("-"): | ||
| flag = args[0] | ||
| file_paths = args[1:] | ||
| else: | ||
| file_paths = args | ||
|
|
||
|
|
||
| # Read all files and combine their contents into one string | ||
| content = "" | ||
|
|
||
| for file in file_paths: | ||
| with open(file, "r") as f: | ||
| content += f.read() | ||
|
|
||
|
|
||
| # Split the file content into separate lines | ||
| lines = content.splitlines() | ||
|
|
||
|
|
||
| # Handle -n flag: add a number to every line | ||
| if flag == "-n": | ||
| new_lines = [] | ||
|
|
||
| for index, line in enumerate(lines): | ||
| new_lines.append(f"{index + 1} {line}") | ||
|
|
||
| lines = new_lines | ||
|
|
||
|
|
||
| # Handle -b flag: number only non-empty lines | ||
| if flag == "-b": | ||
| line_number = 1 | ||
| new_lines = [] | ||
|
|
||
| for line in lines: | ||
| # Keep empty lines without adding numbers | ||
| if line.strip() == "": | ||
| new_lines.append(line) | ||
|
|
||
| # Add a number only to lines that contain text | ||
| else: | ||
| new_lines.append(f"{line_number} {line}") | ||
| line_number += 1 | ||
|
|
||
| lines = new_lines | ||
|
|
||
| sys.stdout.write("\n".join(lines)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Access command-line arguments from the terminal | ||
| import sys | ||
|
|
||
| # Work with the operating system (files and directories) | ||
| import os | ||
|
|
||
| # Get command-line arguments | ||
| args = sys.argv[1:] | ||
|
|
||
| flags = [] | ||
| paths = [] | ||
|
|
||
|
|
||
| # Separate flags and paths | ||
| for arg in args: | ||
| if arg.startswith("-"): | ||
| flags.append(arg) | ||
| else: | ||
| paths.append(arg) | ||
|
|
||
|
|
||
| # Use the current directory if no path is provided | ||
| if not paths: | ||
| paths = ["."] | ||
|
|
||
|
|
||
| # Process each path | ||
| for path in paths: | ||
|
|
||
| # If the path is a file, print its name | ||
| if os.path.isfile(path): | ||
| print(path) | ||
|
|
||
| # If the path is a directory, get its contents | ||
| elif os.path.isdir(path): | ||
| contents = os.listdir(path) | ||
| if "-a" not in flags: | ||
| contents=[ | ||
| file for file in contents | ||
| if not file.startswith(".") | ||
| ] | ||
| if "-1" in flags: | ||
| print("\n".join(contents)) | ||
| else: | ||
| print(" ".join(contents)) | ||
| # Handle invalid paths | ||
| else: | ||
| print(f"No such file or directory: {path}") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import sys | ||
|
|
||
| args = sys.argv[1:] | ||
|
|
||
| flags = [] | ||
| paths = [] | ||
|
|
||
| # Separate flags and paths | ||
| for arg in args: | ||
| if arg.startswith("-"): | ||
| flags.append(arg) | ||
| else: | ||
| paths.append(arg) | ||
|
|
||
|
|
||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_chars = 0 | ||
|
|
||
|
|
||
| for file in paths: | ||
|
|
||
| # Read file as bytes | ||
| with open(file, "rb") as f: | ||
| content = f.read() | ||
|
|
||
| # Count | ||
| lines = content.count(b"\n") | ||
| words = len(content.split()) | ||
| chars = len(content) | ||
|
|
||
| # Output for this file | ||
| output = [] | ||
|
|
||
| if len(flags) == 0: | ||
| output = [lines, words, chars] | ||
|
|
||
| else: | ||
| if "-l" in flags: | ||
| output.append(lines) | ||
|
|
||
| if "-w" in flags: | ||
| output.append(words) | ||
|
|
||
| if "-c" in flags: | ||
| output.append(chars) | ||
|
|
||
| print(*output, file) | ||
|
|
||
| # Add to totals | ||
| total_lines += lines | ||
| total_words += words | ||
| total_chars += chars | ||
|
|
||
|
|
||
| # Print total only when multiple files | ||
| if len(paths) > 1: | ||
|
|
||
| total_output = [] | ||
|
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. What do you think about the output code here? Can you spot any duplication across the file?
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. Thanks. I removed the duplicated output logic by creating a |
||
|
|
||
| if len(flags) == 0: | ||
| total_output = [total_lines, total_words, total_chars] | ||
|
|
||
| else: | ||
| if "-l" in flags: | ||
| total_output.append(total_lines) | ||
|
|
||
| if "-w" in flags: | ||
| total_output.append(total_words) | ||
|
|
||
| if "-c" in flags: | ||
| total_output.append(total_chars) | ||
|
|
||
| print(*total_output, "total") | ||
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.
If you run this with multiple files, how does the output look? What change could make it neater?
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.
Thanks. I’ve updated the output formatting to make the results neater when using multiple files.