Skip to content

Commit 84192ba

Browse files
committed
refactored use of Path functions and removed os library in place of Path
1 parent 36b5584 commit 84192ba

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

  • implement-shell-tools/wc

‎implement-shell-tools/wc/wc.py‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import argparse
2-
import os
32
from pathlib import Path
43

5-
64
# Argument Handling
75
parser = argparse.ArgumentParser(
86
prog="wc",
@@ -20,15 +18,16 @@
2018
metrics = ["line_count", "word_count", "byte_size"]
2119
displayed_metrics = []
2220

21+
2322
# ===== wc Procedure =====
2423
def wc(args):
25-
cwd = os.getcwd()
2624
file_names = args.files
2725

2826
execute_flags()
29-
all_files_data = add_totals(extract_files_data(file_names, cwd))
27+
all_files_data = add_totals(extract_files_data(file_names))
3028
print_output(all_files_data)
3129

30+
3231
# ===== Flag Handling =====
3332
def execute_flags():
3433
if args.w:
@@ -38,44 +37,50 @@ def execute_flags():
3837
if args.c:
3938
displayed_metrics.append("byte_size")
4039

40+
4141
# ===== Extracting Files Data =====
42-
def extract_files_data(file_names, cwd):
42+
def extract_files_data(file_names):
4343
all_files_data = []
4444

4545
for file_name in file_names:
4646
file_data = {}
4747
file_data["name"] = file_name
48-
file_data["text"] = read_file(file_name, cwd)
48+
file_data["text"] = read_file(file_name)
4949
file_data["line_count"] = calculate_line_count(file_data["text"])
5050
file_data["word_count"] = calculate_word_count(file_data["text"])
51-
file_data["byte_size"] = read_byte_size(file_name, cwd)
51+
file_data["byte_size"] = read_byte_size(file_name)
5252

5353
all_files_data.append(file_data)
5454

5555
return all_files_data
5656

57-
def read_file(file_name, cwd):
58-
file_path = Path(cwd) / file_name
59-
return file_path.read_text(encoding="utf-8").rstrip()
57+
58+
def read_file(file_name):
59+
file_path = Path(file_name)
60+
return file_path.read_text(encoding="utf-8").rstrip()
61+
6062

6163
def calculate_line_count(text):
62-
if not text:
63-
return 0
64-
return len(text.splitlines())
64+
if not text:
65+
return 0
66+
return len(text.splitlines())
67+
6568

6669
def calculate_word_count(text):
67-
return len(text.split())
70+
return len(text.split())
71+
72+
73+
def read_byte_size(file_name):
74+
file_path = Path(file_name)
75+
return Path(file_path).stat().st_size
6876

69-
def read_byte_size(file_name, cwd):
70-
file_path = os.path.join(cwd, file_name)
71-
return os.path.getsize(file_path)
7277

7378
def add_totals(all_files_data):
7479
if len(all_files_data) <= 1:
7580
return all_files_data
7681

7782
totals_data = {"name": "total"}
78-
83+
7984
for metric in metrics:
8085
metric_sum = 0
8186
for file in all_files_data:
@@ -85,6 +90,7 @@ def add_totals(all_files_data):
8590
all_files_data.append(totals_data)
8691
return all_files_data
8792

93+
8894
# ===== Outputting Data =====
8995
def print_output(output_data):
9096
for file in output_data:
@@ -97,5 +103,6 @@ def print_output(output_data):
97103
output_string += f" {file['name']}"
98104
print(output_string)
99105

106+
100107
# ===== Run wc =====
101-
wc(parser.parse_args())
108+
wc(parser.parse_args())

0 commit comments

Comments
 (0)