Skip to content

Commit 877b80f

Browse files
committed
[NEEDS TO BE HEAVILY REVISED I DONT KNOW WHAT SHIT I HAVE DONE]
please revise the get_import_count function in visualwrapper please
1 parent 4ab372d commit 877b80f

File tree

1 file changed

+83
-36
lines changed

1 file changed

+83
-36
lines changed

PyStats.py

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
from rich.columns import Columns
2020
from rich.rule import Rule
2121
from rich.status import Status
22+
from rich.text import Text
23+
24+
console = Console()
25+
print = console.print
26+
2227

2328
install_traceback(show_locals=False)
2429

@@ -41,12 +46,14 @@
4146
parser.add_argument("-df", help="Input Absolute Path to Directory or File")
4247
# df is directory or file it will kinda figure out itself assuming theirs only 2 files in
4348
# the dir and u do -neglect
44-
parser.add_argument("-onefile", help="Input Absolute Path to File to Analyze")
49+
parser.add_argument(
50+
"-onefile", help="Input Absolute Path to File to Analyze")
4551
parser.add_argument(
4652
"-neglect", help="Input Absolute Path to File to Ignore ONLY IN DIRECTORY"
4753
)
48-
#Argument if to get how many variables
49-
parser.add_argument("--vars", help="Get how many variables are in the file")
54+
# Argument if to get how many variables
55+
parser.add_argument(
56+
"--vars", help="Get how many variables are in the file")
5057
# Debug argument to print out the file names
5158
path = parser.parse_args()
5259
args = parser.parse_args()
@@ -88,7 +95,8 @@
8895
for dir_path, _, filenames in os.walk(path.df):
8996
for f in filenames:
9097
if f.endswith(".py"):
91-
paths.append(os.path.abspath(os.path.join(dir_path, f)))
98+
paths.append(os.path.abspath(
99+
os.path.join(dir_path, f)))
92100
# remove neglect from paths
93101
if path.neglect is not None:
94102
for line in paths:
@@ -143,28 +151,32 @@ def add_from_imports_results(from_import_list, result_dictionary):
143151
def __scrape_imports(self, get_assets=True):
144152
result = {}
145153
if len(self.directory) > 1:
146-
with Status('[blue]Scraping imports from multiple files...'):
154+
with Status('[blue]Scraping imports from multiple files...'):
147155
# if get_assets:
148156
# print('[blue]Calculating assets ...\n')
149157
for file_path in self.directory:
150-
from_imp = from_imports(input_file=file_path, get_assets=get_assets)
158+
from_imp = from_imports(
159+
input_file=file_path, get_assets=get_assets)
151160
imp_ = import_imports(input_file=file_path)
152161

153162
self.add_from_imports_results(
154163
from_import_list=from_imp, result_dictionary=result
155164
)
156165

157-
self.add_imports_to_results(import_list=imp_, result_dictionary=result)
166+
self.add_imports_to_results(
167+
import_list=imp_, result_dictionary=result)
158168
else:
159169
with Status('[blue]Scraping imports from single file...'):
160-
from_imp = from_imports(input_file=self.directory[0], get_assets=get_assets)
170+
from_imp = from_imports(
171+
input_file=self.directory[0], get_assets=get_assets)
161172
imp_ = import_imports(input_file=self.directory[0])
162173

163174
self.add_from_imports_results(
164175
from_import_list=from_imp, result_dictionary=result
165176
)
166177

167-
self.add_imports_to_results(import_list=imp_, result_dictionary=result)
178+
self.add_imports_to_results(
179+
import_list=imp_, result_dictionary=result)
168180

169181
return result
170182

@@ -214,7 +226,8 @@ def line_count(self, exclude_empty_line: bool = False):
214226
count = sum(1 for _ in open_file if _.rstrip("\n"))
215227

216228
file_path = (
217-
file_path.split("../")[1] if ".." in file_path else file_path
229+
file_path.split(
230+
"../")[1] if ".." in file_path else file_path
218231
)
219232
line_count[file_path] = count
220233
else:
@@ -242,7 +255,7 @@ def most_used_variable(self, n_variables=None):
242255
reverse=True,
243256
)
244257
}
245-
258+
246259
if n_variables is not None:
247260
return {k: v for k, v in list(most_used_variable.items())[:n_variables]}
248261
else:
@@ -253,11 +266,11 @@ def get_import_names(self, import_type: str = "all"):
253266
imports = self.__scrape_imports(get_assets=True)
254267

255268
from_ = [k for k in imports.keys() if "from" in k]
256-
#sort by frequency of imports
269+
# sort by frequency of imports
257270
from_.sort()
258271

259272
import_ = [k for k in imports.keys() if "import" in k]
260-
#Sort by frequency of import
273+
# Sort by frequency of import
261274
import_.sort(key=lambda x: imports[x], reverse=True)
262275
import_.sort()
263276

@@ -271,7 +284,7 @@ def get_import_names(self, import_type: str = "all"):
271284
all_imports.extend(all_)
272285
all_imports = list(set(all_imports))
273286
all_imports.sort()
274-
287+
275288
return all_imports
276289
else:
277290
raise OutputNotSpecified(
@@ -298,16 +311,17 @@ def most_called_func(self):
298311
for line in lines:
299312
for each in func_names:
300313
if each in line:
301-
most_called_func[each] = most_called_func.get(each, 0) + 1
302-
303-
#Sort by frquency of use
314+
most_called_func[each] = most_called_func.get(
315+
each, 0) + 1
316+
317+
# Sort by frquency of use
304318
most_called_func = {
305319
k: v
306320
for k, v in sorted(
307321
most_called_func.items(), key=lambda item: item[1], reverse=True
308322
)
309323
}
310-
#if frequency is 1 then replace it with text 'Only Defined'
324+
# if frequency is 1 then replace it with text 'Only Defined'
311325
for key, value in most_called_func.items():
312326
if value == 1:
313327
most_called_func[key] = "[red]Defined Only[/]"
@@ -392,11 +406,11 @@ def get_line_count(self):
392406
def get_varible(self, n_variables=3):
393407
if args.vars:
394408
n_variables = int(args.vars)
395-
print(len(self.stat.most_used_variable(100000))) #Gets max number of varibles assuming their not more then 100000 cloud implement a fix to this astro but it will do for now
409+
# Gets max number of varibles assuming their not more then 100000 cloud implement a fix to this astro but it will do for now
410+
print(len(self.stat.most_used_variable(100000)))
396411
if int(n_variables) > int(len(self.stat.most_used_variable(100000))):
397412
n_variables = int(len(self.stat.most_used_variable(100000)))
398-
399-
413+
400414
if self.adhd_mode:
401415
coul = self.get_random_colour()
402416
else:
@@ -428,9 +442,8 @@ def get_varible(self, n_variables=3):
428442
# print(self.variable_panel)
429443
return self.variable_panel
430444

431-
def get_import_count(
432-
self,
433-
):
445+
def get_import_count(self):
446+
learn = 0
434447
if self.adhd_mode:
435448
coul = self.get_random_colour()
436449
else:
@@ -440,36 +453,67 @@ def get_import_count(
440453
else:
441454
coulv2 = "pale_turquoise1"
442455
imports = self.stat.import_count()
443-
#get only from stuff from imports
456+
# get only from stuff from imports
444457
all_imports = imports
445458
imports = {k: v for k, v in imports.items() if k.startswith("import")}
459+
len_import_imports = len(imports)
446460
# add \n after each element except after last element
447461
imports_md = "\n".join(
448462
[f"[{coulv2}]{k}[/]: [{coul}]{v}[/]" for k, v in imports.items()]
449463
)
464+
450465
imports_md = re.sub(r"(.*?): (\d+)", r"\1: [b]\2[/]", imports_md)
451466
self.import_md = f"""[pale_turquoise1]{imports_md}[/]"""
452467
self.import_panel = Panel(
453468
self.import_md,
454469
title="[black]Count of 'import' statements",
455470
title_align="left",
456471
border_style="blue",
472+
height=learn
457473
)
474+
#amount of elements in dict imports_md
458475

459-
imports = {k: v for k, v in all_imports.items() if k.startswith("from")}
476+
477+
imports_from = {k: v for k, v in all_imports.items()
478+
if k.startswith("from")}
479+
len_from_imports = len(imports_from)
460480
# add \n after each element except after last element
461-
imports_md = "\n".join(
462-
[f"[{coulv2}]{k}[/]: [{coul}]{v}[/]" for k, v in imports.items()]
481+
imports_md_from = "\n".join(
482+
[f"[{coulv2}]{k}[/]: [{coul}]{v}[/]" for k, v in imports_from.items()]
463483
)
464-
imports_md = re.sub(r"(.*?): (\d+)", r"\1: [b]\2[/]", imports_md)
465-
self.import_md = f"""[pale_turquoise1]{imports_md}[/]"""
484+
imports_md_from = re.sub(r"(.*?): (\d+)", r"\1: [b]\2[/]", imports_md_from)
485+
self.import_md_from = f"""[pale_turquoise1]{imports_md_from}[/]"""
486+
487+
466488
self.from_imports = Panel(
467-
self.import_md,
489+
self.import_md_from,
468490
title="[black]Count of 'from' statements",
469491
title_align="left",
470492
border_style="blue",
493+
height=learn
494+
)
495+
496+
if len_import_imports > len_from_imports:
497+
learn = len_import_imports
498+
elif len_import_imports < len_from_imports:
499+
learn = len_from_imports
500+
501+
self.import_panel = Panel(
502+
self.import_md,
503+
title="[black]Count of 'import' statements",
504+
title_align="left",
505+
border_style="blue",
506+
height=learn + 2
471507
)
472508

509+
self.from_imports = Panel(
510+
self.import_md_from,
511+
title="[black]Count of 'from' statements",
512+
title_align="left",
513+
border_style="blue",
514+
height=learn + 2
515+
)
516+
473517

474518
# print(self.import_panel)
475519
return self.import_panel, self.from_imports
@@ -487,7 +531,8 @@ def get_most_called_func(self):
487531
# add \n after each element except after last element
488532

489533
func_names_md = "\n".join(
490-
[f"[{coulv2}]{k}[/]: [{coul}]{v}[/]" for k, v in most_called_func.items()]
534+
[f"[{coulv2}]{k}[/]: [{coul}]{v}[/]" for k,
535+
v in most_called_func.items()]
491536
)
492537

493538
func_names_md = re.sub(r"(.*?): (\d+)", r"\1: [b]\2[/]", func_names_md)
@@ -503,8 +548,10 @@ def get_most_called_func(self):
503548
return self.func_panel
504549

505550
def get_all(self):
551+
imp_count = self.get_import_count()
506552
grp = Columns([self.get_line_count(), self.get_varible()])
507-
grp2 = Columns([self.get_import_count()[0], self.get_import_count()[1]], padding=(0, 1))
553+
grp2 = Columns([imp_count[0],
554+
imp_count[1]], padding=(0, 1))
508555
mygrp = Group(
509556
self.get_quickstat(),
510557
Rule('[black]Stats'),
@@ -513,13 +560,13 @@ def get_all(self):
513560
grp2,
514561
)
515562

516-
return Panel(mygrp, title="[black]All Stat", title_align="middle", width=None)
563+
return Panel(mygrp, title="[black]All Stats", title_align="middle", width=None)
517564

518565

519566
old_info = Stat(paths)
520-
info = VisualWrapper(paths)
567+
info = VisualWrapper(paths, adhd_mode=True)
521568

522-
print(info.get_all())
569+
print(info.get_all())
523570

524571
# print(info.get_quickstat())
525572
# print(info.get_line_count())

0 commit comments

Comments
 (0)