-
-
Notifications
You must be signed in to change notification settings - Fork 108
LONDON | 26-SDC-JUly | Boshra Mahmoudi | Sprint 4 | Implement shell tools in python #678
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 all commits
db7b0f0
721d271
e4bcf76
c48dcce
8157633
0d520d4
9844783
f8a25cc
19015e5
3e44d1c
ad6bc8e
b6324e4
31364b4
ebca147
c6723d0
60d49b2
d193496
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,30 @@ | ||
| import argparse | ||
|
|
||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat implemnet by Python" | ||
| ) | ||
|
|
||
| parser.add_argument("-n", action="store_true") | ||
| parser.add_argument("-b", action="store_true") | ||
| parser.add_argument("files", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| line_number = 1 | ||
|
|
||
| for filename in args.files: | ||
|
|
||
| with open(filename) as file: | ||
| for line in file: | ||
| line = line.rstrip("\n") | ||
|
|
||
| if args.b and line == "": | ||
| print() | ||
| continue | ||
|
|
||
| if args.n or args.b: | ||
| print(f"{line_number} {line}") | ||
| line_number += 1 | ||
| else: | ||
| print(line) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls implemnet by Python" | ||
| ) | ||
|
|
||
| parser.add_argument("-1", action="store_true", dest="one_per_line") | ||
| parser.add_argument("-a", action="store_true", dest="show_all") | ||
| parser.add_argument("paths", nargs="*") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not args.paths: | ||
| args.paths = ["."] | ||
|
|
||
| for path in args.paths: | ||
| if os.path.isdir(path): | ||
| files = sorted(os.listdir(path)) | ||
|
|
||
| if not args.show_all: | ||
| files = [file for file in files if not file.startswith(".")] | ||
|
|
||
| if args.one_per_line: | ||
| for file in files: | ||
| print(file) | ||
| else: | ||
| for file in files: | ||
| print(file, end=" ") | ||
| print() | ||
|
|
||
| else: | ||
|
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 happens if we try like Your current condition checks if it's a directory but what if it is a file. Shall we just print or do something with that condition? And what if it's neither of dir nor file? How do we handle that? |
||
| print(path) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import argparse | ||
|
|
||
|
|
||
| parser = argparse.ArgumentParser() | ||
|
|
||
| parser.add_argument("-l", action="store_true") | ||
| parser.add_argument("-w", action="store_true") | ||
| parser.add_argument("-c", action="store_true") | ||
| parser.add_argument("files", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
|
|
||
| def add_count(output, value): | ||
| output.append(str(value)) | ||
|
|
||
|
|
||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_bytes = 0 | ||
|
|
||
| for filename in args.files: | ||
|
|
||
| with open(filename, "rb") as file: | ||
| content = file.read() | ||
|
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. Reading the whole file with .read() is putting that data into RAM. What if you point this at a 50GB log file? Check out this code for learning a better approach here |
||
|
|
||
| lines = content.count(b"\n") | ||
| words = len(content.split()) | ||
| bytes_count = len(content) | ||
|
|
||
| total_lines += lines | ||
| total_words += words | ||
| total_bytes += bytes_count | ||
|
|
||
| if not args.l and not args.w and not args.c: | ||
| print(f"{lines:8} {words:8} {bytes_count:8} {filename}") | ||
|
|
||
| else: | ||
| output = [] | ||
|
|
||
| if args.l: | ||
| add_count(output, lines) | ||
|
|
||
| if args.w: | ||
| add_count(output, words) | ||
|
|
||
| if args.c: | ||
| add_count(output, bytes_count) | ||
|
|
||
| print(f"{' '.join(output):>8} {filename}") | ||
|
|
||
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.
What happens if filename doesn't exist? You might try with error handling for FileNotFoundError.