-
-
Notifications
You must be signed in to change notification settings - Fork 11
London | Elhadj Abdoul Diallo | Module-Tools | WEEK4 - Implement-shell-tools-python #38
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
acf2628
f8f1ca8
4b0c2dc
5050c26
59fb74a
4fa86d0
aa0ed1d
946e244
2b59edd
12ab72a
b61e4d1
b908688
2c493bf
f83037b
8c55ba3
92ce714
3969905
6e2cad2
80db8ec
4b7afa0
65c2653
f9f4f1b
c31cb36
3dd6a35
3fda7b2
78aff81
e6d6afe
1be9fbe
5d7f354
716ab40
af6a6a7
282e9cc
6fcf115
39b5783
d889bf4
f2c55db
d6c6577
b8d801b
b384884
cafe718
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
implement-cowsay/.venv. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="cat program implementation", | ||
description="Implements the cat program", | ||
) | ||
|
||
parser.add_argument("-n", "--number", action="store_true", help="Number the output lines, starting at 1.") | ||
parser.add_argument("-b", "--number2", action="store_true", help="Number the output of non-empty lines, starting at 1.") | ||
parser.add_argument("paths", nargs='+', help="The files to search") | ||
|
||
args = parser.parse_args() | ||
def read_file_content(file_path): | ||
with open(file_path, 'r') as f: | ||
return f.read() | ||
|
||
def extract_content_lines(content): | ||
lines = content.split('\n') | ||
if lines and lines[-1] == '': | ||
lines = lines[:-1] | ||
return lines | ||
|
||
def output_lines_with_numbers(lines): | ||
count = 0 | ||
number_lines = args.number | ||
number_non_empty_lines = args.number2 | ||
for line in lines: | ||
if number_lines: | ||
count += 1 | ||
print(count, line) | ||
elif(number_non_empty_lines) and line != '': | ||
count += 1 | ||
print(count, line) | ||
else: | ||
print(line) | ||
|
||
# Read contents of all files into a list | ||
all_lines = [] | ||
for path in args.paths: | ||
content = read_file_content(path) | ||
lines = extract_content_lines(content) | ||
all_lines.extend(lines) | ||
|
||
# Output lines with or without numbering | ||
output_lines_with_numbers(all_lines) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import argparse | ||
import os | ||
|
||
|
||
parser = argparse.ArgumentParser( | ||
prog="The ls program", | ||
description="Implements the ls program" | ||
) | ||
parser.add_argument('dir', nargs='?', type=str, help="Path to the directory to list", default='.') | ||
parser.add_argument("-1", "--one", action="store_true", help="list the directory files one per line") | ||
|
||
parser.add_argument("-a", "--hidden_files", action="store_true", help="list the directory files one per line") | ||
|
||
args = parser.parse_args() | ||
|
||
|
||
def list_directory_contents(): | ||
is_file_per_line_option = args.one | ||
is_hidden_option = args.hidden_files | ||
dir = args.dir | ||
files = os.listdir(dir) | ||
|
||
if is_hidden_option: | ||
files = ['.', '..'] + files | ||
|
||
for file in files: | ||
if is_hidden_option or not file.startswith('.'): | ||
if is_file_per_line_option: | ||
print(file) | ||
else: | ||
print(file, end=' ') | ||
if not is_file_per_line_option: | ||
print() | ||
|
||
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. would good be a good idea, rather than printing directly to console, to add console lines to an in memory list, this would make it easier to direct the output whereever you want it, and to test. |
||
list_directory_contents() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
import argparse | ||
|
||
|
||
parser = argparse.ArgumentParser( | ||
prog="wc program", | ||
description="Implements the cat program" | ||
) | ||
|
||
parser.add_argument("paths", nargs='+', type=str, help="The path to process") | ||
parser.add_argument('-l', '--lines', action="store_true", help='Ouput number of lines') | ||
parser.add_argument('-w', '--words', action="store_true", help='Ouput number of words') | ||
parser.add_argument('-c', '--chars', action="store_true", help='Ouput number of characters') | ||
|
||
args = parser.parse_args() | ||
is_lines_option = args.lines | ||
is_words_option = args.words | ||
is_chars_option = args.chars | ||
|
||
|
||
def extract_lines(content): | ||
lines = content.split('\n') | ||
if lines and lines[-1] == '': | ||
lines = lines[:-1] | ||
return lines | ||
|
||
def extract_words(content): | ||
return content.split(' ') | ||
|
||
def extract_chars(content): | ||
return list(content) | ||
|
||
def read_file_content(file_path): | ||
with open(file_path, 'r') as file: | ||
return file.read() | ||
|
||
|
||
def get_lines_words_chars_count(file_path): | ||
content = read_file_content(file_path) | ||
lines = extract_lines(content) | ||
words = extract_words(content) | ||
chars = extract_chars(content) | ||
return [len(lines), len(words), len(chars)] | ||
|
||
|
||
def print_selected_counts(file_path): | ||
lines_words_chars_count = get_lines_words_chars_count(file_path) | ||
|
||
lines_count = lines_words_chars_count[0] | ||
words_count = lines_words_chars_count[1] | ||
chars_count = lines_words_chars_count[2] | ||
|
||
if is_lines_option: print(lines_count, file_path) | ||
elif is_words_option: print(words_count, file_path) | ||
elif is_chars_option: print(chars_count, file_path) | ||
else: print(lines_count, words_count, chars_count, file_path) | ||
|
||
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. have a look at tuples rather than a plain array for the count results |
||
def print_total_lines(lines_words_chars_count, total): | ||
total_lines = sum(count[0] for count in lines_words_chars_count) | ||
total_words = sum(count[1] for count in lines_words_chars_count) | ||
total_chars = sum(count[2] for count in lines_words_chars_count) | ||
|
||
if is_lines_option: print(total_lines, total) | ||
elif is_words_option: print(total_words, total) | ||
elif is_chars_option: print(total_chars, total) | ||
else: print(total_lines, total_words, total_chars, total) | ||
|
||
def print_file_statistics(): | ||
lines_words_chars_count = [] | ||
if len(args.paths) == 1: | ||
print_selected_counts(args.paths[0]) | ||
else: | ||
for file_path in args.paths: | ||
result = get_lines_words_chars_count(file_path) | ||
lines_words_chars_count.append(result) | ||
print_selected_counts(file_path) | ||
print_total_lines(lines_words_chars_count, 'total') | ||
|
||
print_file_statistics() |
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.
possibly rename 'number1' and 'number2 ' to 'line_number_on' and filter_empty_lines'