Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
.venv/
__pycache__/
*.pyc
48 changes: 48 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import argparse

parser = argparse.ArgumentParser(
prog="cat shell tool",
description="making cat tool with oython"
)

parser.add_argument(
"files",
nargs="+",
help="file to read"
)

parser.add_argument(
"-n",
"--number",
action="store_true",
help="add number to the lines"
)

parser.add_argument(
"-b",
"--number_nonblank",
action="store_true",
help="does not number the blank lines"
)

args = parser.parse_args()

number_line = 0
for file in args.files:
with open(file, "r") as f:
lines = f.readlines()

for line in lines:
if args.number_nonblank:
if line.strip():
number_line += 1
print(f"{number_line} {line}", end="")
elif args.number:
number_line += 1
print(f"{number_line} {line}", end="")
else:
print(line)




1 change: 1 addition & 0 deletions implement-shell-tools/cat/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

42 changes: 42 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import argparse
import os

parser = argparse.ArgumentParser(
prog="ls shell tool",
description="making ls tool with python"
)

parser.add_argument(
"-1",
dest="one_per_line",
action="store_true",
help="lists one per line"
)
parser.add_argument(
"-a",
dest="show_hidden",
action="store_true",
help="show files even hidden files"
)
parser.add_argument(
"items",
nargs="*", #defualt to current directory
help="items to list"
)

args = parser.parse_args()

paths = args.items if args.items else ['.']

for path in paths:
try:
entries = os.listdir(path)
if not args.show_hidden:
entries = [e for e in entries if not e.startswith(".")]
if args.one_per_line:
for entry in entries:
print(entry)
else:
print(" ".join(entries))
except:
print(f"ls can not access this directory: {path}")
82 changes: 82 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import argparse
import os

parser = argparse.ArgumentParser(
prog="wc shell tool",
description="making wc tool with python"
)

parser.add_argument(
"files",
nargs="+",
help="file or files to work on"
)
parser.add_argument(
"-w",
action="store_true",
help="counting words"
)
parser.add_argument(
"-l",
action="store_true",
help="counting lines"
)
parser.add_argument(
"-c",
action="store_true",
help="counting char"
)

args = parser.parse_args()

total_lines = 0
total_words = 0
total_chars = 0

for file in args.files:
try:
if os.path.isdir(file):
print(f"wc: {file}: read: Is a directory")
continue

with open(file, "rb") as f:
content = f.read()
word_count = len(content.decode('utf-8', errors='ignore').split())
line_count = content.count(b"\n")
char_count = len(content)

total_lines += line_count
total_words += word_count
total_chars += char_count

output_parts = []
if args.l:
output_parts.append(f"{line_count:>7}")
if args.w:
output_parts.append(f"{word_count:>7}")
if args.c:
output_parts.append(f"{char_count:>7}")

if not args.w and not args.l and not args.c:
print(f"{line_count:>7}{word_count:>7}{char_count:>7} {file}")
else:
output_parts.append(f" {file}")
print("".join(output_parts))

except FileNotFoundError:
print(f"wc: {file}: open: No such file or directory")

if len(args.files) > 1:
total_parts = []
if args.l:
total_parts.append(f"{total_lines:>7}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start. Some of this code is repeated quite a lot though, like all the printing, formatting. Can you find a way to generalise and reduce some of this duplication?

if args.w:
total_parts.append(f"{total_words:>7}")
if args.c:
total_parts.append(f"{total_chars:>7}")

if not args.w and not args.l and not args.c:
print(f"{total_lines:>7}{total_words:>7}{total_chars:>7} total")
else:
total_parts.append(" total")
print("".join(total_parts))