generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Glasgow | 25-SDC-Nov | Nataliia Volkova | Sprint 4 | Implement shell tools (cat, ls, wc) in Python #264
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
Nataliia74
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
Nataliia74:implement-shell-tools-withPython
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.
+140
−0
Open
Glasgow | 25-SDC-Nov | Nataliia Volkova | Sprint 4 | Implement shell tools (cat, ls, wc) in Python #264
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f3b6b52
shell commands written by python
Nataliia74 87ed47e
correct numbering lines
Nataliia74 e3aafe0
Changes in wc.py according to the Readme requirements
Nataliia74 0027636
added -1 flag, used ruff format command
Nataliia74 558abe6
com ruff format for rest files
Nataliia74 9bc06ec
refactoring
Nataliia74 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,30 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat-command", description="cat shell command in python " | ||
| ) | ||
|
|
||
| parser.add_argument("-n", action="store_true", help="Display all lines numbers") | ||
| parser.add_argument("-b", action="store_true", help="Display numbers non-empty lines") | ||
| parser.add_argument("path", nargs="+", help="The file to search") | ||
|
|
||
| args = parser.parse_args() | ||
| number = 1 | ||
|
|
||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.readlines() | ||
|
|
||
| if args.n: | ||
| for line in content: | ||
| print(f"{number}\t{line.strip()}") | ||
| number += 1 | ||
| elif args.b: | ||
| for line in content: | ||
| if line.strip() != "": | ||
| print(f"{number}\t{line.strip()}") | ||
| number += 1 | ||
| else: | ||
| print("") | ||
| else: | ||
| print("".join(content)) | ||
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,57 @@ | ||
| import argparse | ||
Nataliia74 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import os | ||
| import stat | ||
| import pwd | ||
| import grp | ||
| import time | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls-command", description="ls shell command on python" | ||
| ) | ||
|
|
||
| parser.add_argument("-1", action="store_true", dest="one", help="One file per line") | ||
| parser.add_argument( | ||
| "-l", action="store_true", help="Display long format description files" | ||
| ) | ||
| parser.add_argument( | ||
| "-a", action="store_true", help="Display hidden files along with visible" | ||
| ) | ||
| parser.add_argument("path", nargs="*", default=["."], help="The file to search") | ||
|
|
||
|
|
||
| args = parser.parse_args() | ||
|
|
||
|
|
||
| def long_format(path, file): | ||
| info = os.stat(path) | ||
| permissions = stat.filemode(info.st_mode) | ||
| size_file = info.st_size | ||
| owner = pwd.getpwuid(info.st_uid).pw_name | ||
| group = grp.getgrgid(info.st_gid).gr_name | ||
| mtime = time.strftime("%b %d %H:%M", time.localtime(info.st_mtime)) | ||
| print(permissions, size_file, owner, group, mtime, file) | ||
|
|
||
|
|
||
| for path in args.path: | ||
| if os.path.isfile(path): | ||
| file = os.path.basename(path) | ||
| if args.l: | ||
| long_format(path, file) | ||
| else: | ||
| print(file) | ||
| elif os.path.isdir(path): | ||
| files = os.listdir(path) | ||
|
|
||
| if not args.a: | ||
| files = [file for file in files if not file.startswith(".")] | ||
|
|
||
| for file in files: | ||
| full_file_path = os.path.join(path, file) | ||
|
|
||
| if args.l: | ||
| long_format(full_file_path, file) | ||
| else: | ||
| if args.one: | ||
| print(file) | ||
| else: | ||
| print(file, "\n") | ||
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,53 @@ | ||
| import argparse | ||
Nataliia74 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="wc-count words, lines, characters", description="wc shell command on python" | ||
| ) | ||
|
|
||
| parser.add_argument("-l", action="store_true", help="Count lines") | ||
| parser.add_argument("-w", action="store_true", help="Count words") | ||
| parser.add_argument("-c", action="store_true", help="Counts bytes") | ||
| parser.add_argument("path", nargs="+", help="The file to search") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| def calculate_counts(content): | ||
|
|
||
| count_lines = content.count("\n") | ||
| count_words = len(content.split()) | ||
| count_bytes = len(content.encode("utf-8")) | ||
|
|
||
| return count_lines, count_words, count_bytes | ||
|
|
||
| def format_output(count_lines, count_words, count_bytes, file_path): | ||
| if not (args.l or args.w or args.c): | ||
Nataliia74 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print(count_lines, count_words, count_bytes, file_path) | ||
| else: | ||
| line = "" | ||
| if args.l: | ||
| line += f"{count_lines} " | ||
| if args.w: | ||
| line += f"{count_words} " | ||
| if args.c: | ||
| line += f"{count_bytes} " | ||
| line += f"{file_path}" | ||
| print(line) | ||
|
|
||
| count_total_lines = 0 | ||
| count_total_words = 0 | ||
| count_total_bytes = 0 | ||
|
|
||
| for file_path in args.path: | ||
| with open(file_path, "r") as file: | ||
| content = file.read() | ||
|
|
||
| count_lines, count_words, count_bytes = calculate_counts(content) | ||
|
|
||
| count_total_lines += count_lines | ||
| count_total_words += count_words | ||
| count_total_bytes += count_bytes | ||
|
|
||
| format_output(count_lines, count_words, count_bytes, file_path) | ||
|
|
||
| if len(args.path) > 1: | ||
| format_output(count_total_lines, count_total_words, count_total_bytes, " total") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.