Skip to content

Commit 03f75ad

Browse files
authored
Merge pull request #12 from InnoFang/dev
⚡ 🎨 improve the performance of CountableFileIterator
2 parents 812acdf + f0077a9 commit 03f75ad

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,31 @@
1-
from collections.abc import Iterator
2-
import os
31
# !/usr/bin/env python3
42
# -*- coding: utf-8 -*-
53

4+
import os
65
from code_counter.core.countable.file import CountableFile
76
from code_counter.conf.config import Config
8-
from collections import deque
97

108

11-
class CountableFileIterator(Iterator):
12-
def __init__(self, path):
9+
class CountableFileIterator:
10+
def __init__(self):
1311
self._ignore = Config().ignore
1412
self._suffix = Config().suffix
15-
self._file_queue = deque()
16-
self.__search(path)
1713

18-
def __search(self, input_path):
14+
def iter(self, input_path):
1915
if os.path.isdir(input_path):
2016
files = os.listdir(input_path)
2117
for file in files:
2218
file_path = os.path.join(input_path, file)
2319
if os.path.isdir(file_path):
24-
if file_path in self._ignore:
20+
if file in self._ignore:
2521
continue
26-
self.__search(file_path)
22+
yield from self.iter(file_path)
2723
else:
2824
suffix = os.path.splitext(file)[1]
2925
if len(suffix) == 0 or suffix[1:] not in self._suffix:
3026
continue
31-
file_path = os.path.join(input_path, file)
32-
self._file_queue.append(CountableFile(file_path))
27+
yield CountableFile(file_path)
3328
elif os.path.isfile(input_path):
3429
suffix = os.path.splitext(input_path)[1]
3530
if len(suffix) > 0 and suffix[1:] in self._suffix:
36-
self._file_queue.append(CountableFile(input_path))
37-
38-
def __iter__(self):
39-
return self
40-
41-
def __next__(self) -> CountableFile:
42-
if self._file_queue:
43-
fc = self._file_queue.popleft()
44-
return fc
45-
else:
46-
raise StopIteration
31+
yield CountableFile(input_path)

code_counter/core/counter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def search(self):
5959

6060
for path in input_path:
6161
if os.path.exists(path):
62-
for cf in CountableFileIterator(path):
62+
for cf in CountableFileIterator().iter(path):
6363
cf.count()
6464
if self.search_args.verbose:
6565
print(cf, file=output_file)
@@ -175,6 +175,6 @@ def visualize(self):
175175
proptease.set_size('small')
176176
plt.setp(l_text2, fontproperties=proptease)
177177
plt.axis('equal')
178-
plt.title("Inner Pie: Code Files, Outer Pie: Code Type")
178+
plt.title("Inner Pie: Code Files, Outer Pie: Code Lines")
179179
plt.legend(list(self.result['code'].keys()), title="Abbreviation", loc='best', bbox_to_anchor=(1.05, 1))
180180
plt.show()

0 commit comments

Comments
 (0)