Skip to content

Commit a14c320

Browse files
committed
Refactor count_sourcelines.py for clarity and style
Removed redundant comments, simplified ignore_set initialization, and updated string formatting to use .format(). Minor code style improvements for readability.
1 parent 51bc1e5 commit a14c320

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

count_sourcelines.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
# prints recursive count of lines of python source code from current directory
2-
# includes an ignore_list. also prints total sloc
3-
41
import os
2+
53
cur_path = os.getcwd()
6-
ignore_set = set(["__init__.py", "count_sourcelines.py"])
4+
ignore_set = {"__init__.py", "count_sourcelines.py"}
75

86
loc_list = []
97

@@ -12,14 +10,14 @@
1210
if py_file.endswith(".py") and py_file not in ignore_set:
1311
total_path = os.path.join(py_dir, py_file)
1412
try:
15-
# Open the file with UTF-8 encoding
16-
with open(total_path, "r", encoding="utf-8") as file:
17-
loc_list.append((len(file.read().splitlines()),
18-
total_path.split(cur_path)[1]))
13+
with open(total_path, encoding="utf-8") as file:
14+
loc_list.append(
15+
(len(file.read().splitlines()), total_path.split(cur_path)[1])
16+
)
1917
except UnicodeDecodeError as e:
2018
print(f"Skipping file {total_path} due to encoding error: {e}")
2119

2220
for line_number_count, filename in loc_list:
2321
print("%05d lines in %s" % (line_number_count, filename))
2422

25-
print("\nTotal: %s lines (%s)" % (sum([x[0] for x in loc_list]), cur_path))
23+
print("\nTotal: {} lines ({})".format(sum([x[0] for x in loc_list]), cur_path))

0 commit comments

Comments
 (0)