generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
London | 25-SDC-July | Andrei Filippov | Sprint 4 | Implement shell tools in Python #142
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
Droid-An
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
Droid-An:implement-shell-tools-in-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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f53628
wip: done -1 flag
Droid-An 66d8f5e
done ls
Droid-An 573074c
removed unnecessary comments and created wc.py file
Droid-An ace3d8e
done wc
Droid-An 0ddafe8
done cat
Droid-An 8544e56
done changes according to review
Droid-An 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,35 @@ | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="cat", | ||
description="Concatenate and print files", | ||
) | ||
|
||
parser.add_argument( | ||
"-n", action="store_true", help="Number the output lines, starting at 1" | ||
) | ||
parser.add_argument( | ||
"-b", action="store_true", help="Number the non-blank output lines, starting at 1" | ||
) | ||
parser.add_argument("path", nargs="+", help="The path to search") | ||
|
||
args = parser.parse_args() | ||
for path in args.path: | ||
with open(path) as file: | ||
lines = file.readlines() | ||
line_num = 1 | ||
for line in lines: | ||
lin = line.rstrip("\n") | ||
|
||
if args.b: | ||
if lin == "": | ||
print() | ||
else: | ||
print(line_num, lin) | ||
line_num += 1 | ||
elif args.n: | ||
print(line_num, lin) | ||
line_num += 1 | ||
|
||
else: | ||
print(lin) |
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,41 @@ | ||
import os | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="ls", | ||
description="list directory contents", | ||
) | ||
|
||
parser.add_argument("-1",dest="one",action='store_true', help="list one file per line") | ||
parser.add_argument("-a", action='store_true', help="Used to list all files, including hidden files, in the current directory") | ||
parser.add_argument("path", nargs="?", default=".", help="The path to search") | ||
|
||
args = parser.parse_args() | ||
|
||
def arguments_proceeding(files): | ||
data_to_proceed = files | ||
|
||
if args.a: | ||
data_to_proceed = [f for f in files] | ||
data_to_proceed.sort() | ||
data_to_proceed.insert(0, "..") | ||
data_to_proceed.insert(0, ".") | ||
else: | ||
data_to_proceed = [f for f in files if not f.startswith('.')] | ||
data_to_proceed.sort(key=str.lower) | ||
if args.one: | ||
output = [f for f in data_to_proceed] | ||
for f in output: | ||
print(f) | ||
else: | ||
print(" ".join(data_to_proceed)) | ||
|
||
|
||
def path_proceeding(path_argument): | ||
if os.path.isfile(path_argument): | ||
print(path_argument) | ||
elif os.path.isdir(path_argument): | ||
files = os.listdir(path_argument) | ||
arguments_proceeding(files) | ||
|
||
path_proceeding(args.path) |
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,54 @@ | ||
import os | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="wc", | ||
description="Counts words in a file that contain a particular character", | ||
) | ||
|
||
parser.add_argument("-l",action='store_true', help="The number of lines in each input file is written to the standard output.") | ||
parser.add_argument("-w", action='store_true', help="The number of words in each input file is written to the standard output.") | ||
parser.add_argument("-c", action='store_true', help="The number of bytes in each input file is written to the standard output.") | ||
parser.add_argument("path", nargs="+", help="The path to search") | ||
|
||
args = parser.parse_args() | ||
|
||
if (not args.w and not args.c and not args.l) : | ||
args.w = args.c = args.l = True | ||
total = [] | ||
for path in args.path: | ||
output = [] | ||
if args.l or args.w: | ||
with open(path) as file: | ||
lines = file.readlines() | ||
# lines count | ||
if args.l: | ||
num_lines = len(lines) | ||
output.append(num_lines) | ||
# word count | ||
if args.w: | ||
word_count = 0 | ||
for line in lines: | ||
lin = line.rstrip() | ||
wds = lin.split() | ||
word_count += len(wds) | ||
|
||
output.append(word_count) | ||
|
||
|
||
if args.c: | ||
file_size = os.path.getsize(path) | ||
output.append(file_size) | ||
|
||
if len(args.path) > 1: | ||
total.append(output.copy()) | ||
output.append(path) | ||
string_list = map(str, output) | ||
print(" ".join(string_list)) | ||
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. Can you see any problem that might happen with the displayed spacing of the output? 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. honestly, no |
||
if len(args.path) > 1: | ||
result = [sum(i) for i in zip(*total)] | ||
string_result_list = map(str, result) | ||
print(" ".join(string_result_list), " total") | ||
|
||
|
||
|
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.
When printing out the line numbers, compare how the original cat application works to yours. Do you see any discrepancies?
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.
The only discrepancies I see are in spacing. Is that acceptable, provided the output is correct?