generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
London | 25-SDC-July | Priscilla Emebo | Sprint 4 | Python Implementation #162
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
Priscilla-EM
wants to merge
1
commit into
CodeYourFuture:main
Choose a base branch
from
Priscilla-EM:python-implementation
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
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,39 @@ | ||
import argparse | ||
import glob | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="mycat", | ||
description="Outputs the content of the given file(s), like the cat command", | ||
) | ||
|
||
parser.add_argument("-n", help="Number all lines", action="store_true") | ||
parser.add_argument("-b", help="Number all non-blank lines", action="store_true") | ||
parser.add_argument("path", help="The file to display(allowing wildcards)", nargs="+") | ||
|
||
args = parser.parse_args() | ||
|
||
file_list = [] | ||
for path in args.path: | ||
file_list.extend(glob.glob(path)) | ||
|
||
line_number = 1 | ||
|
||
for filename in file_list: | ||
try: | ||
with open(filename, "r") as f: | ||
for line in f: | ||
stripped = line.rstrip("\n") | ||
|
||
if args.b: | ||
if stripped: | ||
print(f"{line_number:>6}\t{stripped}") | ||
line_number += 1 | ||
else: | ||
print("") | ||
elif args.n: | ||
print(f"{line_number:>6}\t{stripped}") | ||
line_number += 1 | ||
else: | ||
print(line, end="") | ||
except FileNotFoundError: | ||
print(f"mycat: {filename}: No such file or directory") |
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,32 @@ | ||
import argparse | ||
import os | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="myls", | ||
description="List directory contents like the ls command" | ||
) | ||
|
||
parser.add_argument("-1", dest="one_per_line", action="store_true", | ||
help="List one file per line") | ||
parser.add_argument("-a", dest="all_files", action="store_true", | ||
help="Include hidden files starting with '.'") | ||
parser.add_argument("path", nargs="?", default=".", help="Directory to list (default: current directory)") | ||
|
||
args = parser.parse_args() | ||
|
||
try: | ||
entries = os.listdir(args.path) | ||
except FileNotFoundError: | ||
print(f"my_ls: cannot access '{args.path}': No such file or directory") | ||
exit(1) | ||
|
||
if not args.all_files: | ||
entries = [e for e in entries if not e.startswith('.')] | ||
|
||
entries.sort() | ||
|
||
if args.one_per_line: | ||
for entry in entries: | ||
print(entry) | ||
else: | ||
print(" ".join(entries)) |
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 @@ | ||
import argparse | ||
import glob | ||
import os | ||
|
||
def count_file(file_path, count_lines=False, count_words=False, count_bytes=False): | ||
lines = words = bytes_count = 0 | ||
with open(file_path, "r", encoding="utf-8", errors="ignore") as f: | ||
for line in f: | ||
lines += 1 | ||
words += len(line.split()) | ||
bytes_count = os.path.getsize(file_path) | ||
|
||
if not (count_lines or count_words or count_bytes): | ||
return lines, words, bytes_count | ||
|
||
return ( | ||
lines if count_lines else None, | ||
words if count_words else None, | ||
bytes_count if count_bytes else None | ||
) | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="count lines, words, and bytes like the wc command") | ||
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="Count bytes") | ||
parser.add_argument("path", nargs="+", help="Files to count") | ||
|
||
args = parser.parse_args() | ||
|
||
total_lines = total_words = total_bytes = 0 | ||
file_list = [] | ||
for pattern in args.path: | ||
file_list.extend(glob.glob(pattern)) | ||
|
||
for file_path in file_list: | ||
counts = count_file(file_path, args.l, args.w, args.c) | ||
|
||
if not (args.l or args.w or args.c): | ||
lines, words, bytes_count = counts | ||
print(f"{lines:>7} {words:>7} {bytes_count:>7} {file_path}") | ||
total_lines += lines | ||
total_words += words | ||
total_bytes += bytes_count | ||
else: | ||
count_strings = [] | ||
if counts[0] is not None: | ||
count_strings.append(f"{counts[0]:>7}") | ||
total_lines += counts[0] | ||
if counts[1] is not None: | ||
count_strings.append(f"{counts[1]:>7}") | ||
total_words += counts[1] | ||
if counts[2] is not None: | ||
count_strings.append(f"{counts[2]:>7}") | ||
total_bytes += counts[2] | ||
print(" ".join(count_strings), file_path) | ||
|
||
if len(file_list) > 1: | ||
if not (args.l or args.w or args.c): | ||
print(f"{total_lines:>7} {total_words:>7} {total_bytes:>7} total") | ||
else: | ||
total_strings = [] | ||
if args.l: | ||
total_strings.append(f"{total_lines:>7}") | ||
if args.w: | ||
total_strings.append(f"{total_words:>7}") | ||
if args.c: | ||
total_strings.append(f"{total_bytes:>7}") | ||
print(" ".join(total_strings), "total") | ||
|
||
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.
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.
There is some duplication of code in this file. Can you spot where it is and find a way to reduce this?