-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-SDC-Mar | Khilola Rustamova| Sprint 4 |Implement shell tools python #537
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
HilolaRustam
wants to merge
14
commits into
CodeYourFuture:main
Choose a base branch
from
HilolaRustam: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.
+241
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
60d6701
created files for shell tools with python
HilolaRustam aee68b7
implementation of cat with python
HilolaRustam 80ca854
implementation of ls with python
HilolaRustam 1e3fc25
implementation of wc with python
HilolaRustam ffb4416
Merge branch 'main' into implement-shell-tools-python
HilolaRustam efadbb0
error handling corrected, structure is changed, indentation fixed
HilolaRustam ad6883f
corrected error handling issue and moved the main up.
HilolaRustam db7c86a
small changes
HilolaRustam cff33e0
fixing glob expansion and error exit
HilolaRustam 2909e56
deleting unneeded code
HilolaRustam a5a8cb4
fixed the design and expanding
HilolaRustam 0090014
fix
HilolaRustam 64688f9
fix
HilolaRustam aac3908
fix
HilolaRustam 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,72 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import sys | ||
|
|
||
| def main(): | ||
| args = sys.argv[1:] | ||
|
|
||
| if not args: | ||
| print("Usage: cat [-n|-b] file...", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| number_mode = "none" | ||
| paths = [] | ||
|
|
||
| for a in args: | ||
| if a == "-n": | ||
| number_mode = "all" | ||
| elif a == "-b": | ||
| number_mode = "non_empty" | ||
| else: | ||
| paths.append(a) | ||
|
|
||
|
|
||
|
|
||
| had_error = print_lines( | ||
| paths, | ||
| number_mode=number_mode, | ||
| ) | ||
|
|
||
| if had_error: | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def read_lines(file): | ||
| with open(file, "r", encoding="utf-8") as f: | ||
| return f.readlines() | ||
|
|
||
| def print_lines(files, number_mode="none"): | ||
| had_error = False | ||
| line_no = 1 | ||
| for file in files: | ||
| try: | ||
| lines = read_lines(file) | ||
| except FileNotFoundError: | ||
| print(f"cat: {file}: No such file or directory", file=sys.stderr) | ||
| had_error = True | ||
| continue | ||
|
|
||
| for line in lines: | ||
| is_empty = (line.strip() == "") | ||
|
|
||
| if number_mode == "non_empty": | ||
| if not is_empty: | ||
| prefix = f"{line_no:6}\t" | ||
| line_no += 1 | ||
| else: | ||
| prefix = "" | ||
| elif number_mode=="all": | ||
| prefix = f"{line_no:6}\t" | ||
| line_no += 1 | ||
| else: | ||
| prefix = "" | ||
|
|
||
| # avoid double newlines: line already includes '\n' | ||
| sys.stdout.write(prefix + line) | ||
|
|
||
| return had_error | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
|
|
||
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 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import sys | ||
| import os | ||
|
|
||
|
|
||
|
|
||
| def main(): | ||
| args = sys.argv[1:] | ||
|
|
||
| show_all = False | ||
| one_per_line = False | ||
| paths = [] | ||
|
|
||
| for a in args: | ||
| if a == "-a": | ||
| show_all = True | ||
| elif a == "-1": | ||
| one_per_line = True | ||
| else: | ||
| paths.append(a) | ||
|
|
||
| if not paths: | ||
| paths = ["."] | ||
|
|
||
| had_error = False | ||
|
|
||
| for path in paths: | ||
| if list_dir( | ||
| path, | ||
| show_all=show_all, | ||
| one_per_line=one_per_line, | ||
| ): | ||
| had_error = True | ||
|
|
||
| if had_error: | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def list_dir(path, show_all=False, one_per_line=False): | ||
| try: | ||
| entries = os.listdir(path) | ||
| except FileNotFoundError: | ||
| print(f"ls: cannot access '{path}': No such file or directory", | ||
| file=sys.stderr, | ||
| ) | ||
| return True | ||
|
|
||
| entries = sorted(entries) | ||
|
|
||
| if show_all: | ||
| normal = sorted([e for e in entries if not e.startswith('.')]) | ||
| hidden = sorted([e for e in entries if e.startswith('.')]) | ||
|
|
||
| entries = [".", ".."] + normal + hidden | ||
| else: | ||
| entries = sorted([e for e in entries if not e.startswith('.')]) | ||
|
|
||
| for entry in entries: | ||
| print(entry) | ||
|
|
||
| return False | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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,104 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import sys | ||
|
|
||
|
|
||
| def count_file(path): | ||
| with open(path, "rb") as f: | ||
| content = f.read() | ||
|
|
||
| byte_count = len(content) | ||
| text = content.decode("utf-8", errors="ignore") | ||
|
|
||
| line_count = text.count("\n") | ||
| word_count = len(text.split()) | ||
|
|
||
| return line_count, word_count, byte_count | ||
|
|
||
|
|
||
|
|
||
| def main(): | ||
| args = sys.argv[1:] | ||
|
|
||
| show_l = False | ||
| show_w = False | ||
| show_c = False | ||
|
|
||
| paths = [] | ||
|
|
||
|
|
||
| # parse args | ||
| for a in args: | ||
| if a == "-l": | ||
| show_l = True | ||
| elif a == "-w": | ||
| show_w = True | ||
| elif a == "-c": | ||
| show_c = True | ||
| else: | ||
| paths.append(a) | ||
|
|
||
| # default: show all | ||
| if not (show_l or show_w or show_c): | ||
| show_l = show_w = show_c = True | ||
|
|
||
| files = paths | ||
|
|
||
| total_l = 0 | ||
| total_w = 0 | ||
| total_c = 0 | ||
|
|
||
| results = [] | ||
| had_error = False | ||
|
|
||
| for file in files: | ||
| try: | ||
| l, w, c = count_file(file) | ||
| except FileNotFoundError: | ||
| print( | ||
| f"wc: {file}: No such file or directory", | ||
| file=sys.stderr, | ||
| ) | ||
| had_error = True | ||
| continue | ||
|
|
||
| total_l += l | ||
| total_w += w | ||
| total_c += c | ||
|
|
||
| results.append((l,w,c, file)) | ||
|
|
||
| # print per-file results (GNU-aligned formatting) | ||
| for l, w, c, file in results: | ||
|
|
||
| parts = [] | ||
| if show_l: | ||
| parts.append(f"{l:3}") | ||
| if show_w: | ||
| parts.append(f"{w:4}") | ||
| if show_c: | ||
| parts.append(f"{c:4}") | ||
|
|
||
|
|
||
| print("".join(parts) + " " + file) | ||
|
|
||
| # print total if multiple files | ||
| if len(results) > 1: | ||
| parts = [] | ||
|
|
||
| if show_l: | ||
| parts.append(f"{total_l:3}") | ||
| if show_w: | ||
| parts.append(f"{total_w:4}") | ||
| if show_c: | ||
| parts.append(f"{total_c:4}") | ||
|
|
||
|
|
||
| print("".join(parts) + " total") | ||
|
|
||
| if had_error: | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.