forked from PySimpleGUI/PySimpleGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode Counter.py
More file actions
250 lines (206 loc) · 8.54 KB
/
Code Counter.py
File metadata and controls
250 lines (206 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Code Counter
A program that counts the lines of code and code characters in a code-base
Author : Israel Dryer
Modified : 2019-11-01
You can find the original repository with the latest updates here:
https://github.com/israel-dryer/Code-Counter
"""
import PySimpleGUI as sg
import statistics as stats
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
WINDOW_SIZE = (1280, 720)
def clean_data(window):
""" clean and parse the raw data """
raw = window.AllKeysDict['INPUT'].DefaultText.split('\n')
# remove whitespace
data = [row.strip() for row in raw if row.strip()]
# remove hash comments
stage1 = []
for row in data:
if row.find('#') != -1:
stage1.append(row[:row.find('#')])
else:
stage1.append(row)
# remove " multiline comments
stage2 = []
ml_flag = False # multiline comment flag
for row in stage1:
if row.count(r'"""') == 0 and not ml_flag: # not a comment line
stage2.append(row)
elif row.count(r'"""') == 1 and not ml_flag: # starting comment line
ml_flag = True
stage2.append(row[:row.find('"""')])
elif row.count(r'"""') == 1 and ml_flag: # ending comment line
ml_flag = False
stage2.append(row[row.find('"""') + 1:])
else:
continue
# remove ' multiline comments
stage3 = []
ml_flag = False # multiline comment flag
for row in stage2:
if row.count(r"'''") == 0 and not ml_flag: # not a comment line
stage3.append(row)
elif row.count(r"'''") == 1 and not ml_flag: # starting comment line
ml_flag = True
stage3.append(row[:row.find("'''")])
elif row.count(r"'''") == 1 and ml_flag: # ending comment line
ml_flag = False
stage3.append(row[row.find("'''") + 1:])
else:
continue
clean_code = [row for row in stage3 if row not in ('', "''", '""')]
# row and character rounds / for calc stats, histogram, charts
char_cnt = [len(row) for row in clean_code]
# statistics
if len(clean_code) == 0:
char_per_line = 1
else:
char_per_line = sum(char_cnt) // len(clean_code)
code_stats = {
'lines': len(clean_code), 'char_per_line': char_per_line,
'count': sum(char_cnt), 'mean': stats.mean(char_cnt), 'median': stats.median(char_cnt),
'pstdev': stats.pstdev(char_cnt), 'min': min(char_cnt), 'max': max(char_cnt)}
return clean_code, char_cnt, code_stats
def process_data(window):
""" clean and save data ... previous executed manually with submit button """
# try:
clean_code, char_cnt, code_stats = clean_data(window)
save_data(clean_code, code_stats, window)
display_charts(char_cnt, window)
display_stats(code_stats, window)
window['T2'].select()
def save_data(clean_code, code_stats, window):
window['OUTPUT'].update('\n'.join([row for row in clean_code]))
return
""" save clean code and stats to file """
with open('output.txt', 'w') as f:
for row in clean_code:
f.write(row + '\n')
# update display
with open('output.txt', 'r') as f:
window['OUTPUT'].update(f.read())
def display_charts(char_cnt, window):
""" create charts to display in window """
def draw_figure(canvas, figure, loc=(0, 0)):
""" matplotlib helper function """
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack()
return figure_canvas_agg
figure = plt.figure(num=1, figsize=(4, 5))
# histogram
plt.subplot(211)
plt.hist(char_cnt)
plt.title('character count per line')
plt.ylabel('frequency')
plt.tight_layout()
# line plot
plt.subplot(212)
x = range(0, len(char_cnt))
y = char_cnt
plt.plot(y)
plt.fill_between(x, y)
plt.title('compressed code line counts')
plt.xlabel('code line number')
plt.ylabel('number of characters')
plt.tight_layout()
draw_figure(window['IMG'].TKCanvas, figure)
def display_stats(code_stats, window):
""" display code stats in the window """
window['LINES'].update('{:,d}'.format(code_stats['lines']))
window['CHARS'].update('{:,d}'.format(code_stats['count']))
window['CPL'].update('{:,d}'.format(code_stats['char_per_line']))
window['MEAN'].update('{:,.0f}'.format(code_stats['mean']))
window['MEDIAN'].update('{:,.0f}'.format(code_stats['median']))
window['PSTDEV'].update('{:,.0f}'.format(code_stats['pstdev']))
window['MAX'].update('{:,d}'.format(code_stats['max']))
window['MIN'].update('{:,d}'.format(code_stats['min']))
def click_file(window):
""" file button click event; open file and load to screen """
filename = sg.popup_get_file('Select a file containing Python code:', title='Code Counter')
if filename is None:
return
with open(filename) as f:
raw = f.read()
window['INPUT'].update(raw)
def click_clipboard(window):
""" get data from clipboard and paste to input """
try:
clip = window['INPUT'].Widget.clipboard_get()
window['INPUT'].update(clip)
except:
sg.popup_error('Clipboard is empty', no_titlebar=True)
def click_reset(window):
""" reset the windows and data fields """
window['INPUT'].update('')
window['OUTPUT'].update('')
reset_stats(window)
window['T1'].select()
def reset_stats(window):
""" clear the stats fields """
window['LINES'].update('{:,d}'.format(0))
window['CHARS'].update('{:,d}'.format(0))
window['CPL'].update('{:,d}'.format(0))
window['MEAN'].update('{:,.0f}'.format(0))
window['MEDIAN'].update('{:,.0f}'.format(0))
window['PSTDEV'].update('{:,.0f}'.format(0))
window['MAX'].update('{:,d}'.format(0))
window['MIN'].update('{:,d}'.format(0))
def btn(name, **kwargs):
""" create button with default settings """
return sg.Button(name, size=(16, 1), font=(sg.DEFAULT_FONT, 12), **kwargs)
def stat(text, width=10, relief=None, justification='left', key=None):
elem = sg.Text(text, size=(width, 1), relief=relief, justification=justification, key=key)
return elem
def main():
""" main program and GUI loop """
sg.ChangeLookAndFeel('BrownBlue')
tab1 = sg.Tab('Raw Code',
[[sg.Multiline(key='INPUT', pad=(0, 0), font=(sg.DEFAULT_FONT, 12))]],
background_color='gray', key='T1')
tab2 = sg.Tab('Clean Code',
[[sg.Multiline(key='OUTPUT', pad=(0, 0), font=(sg.DEFAULT_FONT, 12))]],
background_color='gray25', key='T2')
stat_col = sg.Column([
[stat('Lines of code'), stat(0, 8, 'sunken', 'right', 'LINES'),
stat('Total chars'), stat(0, 8, 'sunken', 'right', 'CHARS')],
[stat('Chars per line'), stat(0, 8, 'sunken', 'right', 'CPL'),
stat('Mean'), stat(0, 8, 'sunken', 'right', 'MEAN')],
[stat('Median'), stat(0, 8, 'sunken', 'right', 'MEDIAN'),
stat('PStDev'), stat(0, 8, 'sunken', 'right', 'PSTDEV')],
[stat('Max'), stat(0, 8, 'sunken', 'right', 'MAX'),
stat('Min'), stat(0, 8, 'sunken', 'right', 'MIN')]], pad=(5, 10), key='STATS')
lf_col = [
[btn('Load FILE'), btn('Clipboard'), btn('RESET')],
[sg.TabGroup([[tab1, tab2]], title_color='black', key='TABGROUP')]]
rt_col = [
[sg.Text('LOAD a file or PASTE code from Clipboard', pad=(5, 15))],
[sg.Text('Statistics', size=(20, 1), pad=((5, 5), (15, 5)),
font=(sg.DEFAULT_FONT, 14, 'bold'), justification='center')],
[stat_col],
[sg.Text('Visualization', size=(20, 1),
font=(sg.DEFAULT_FONT, 14, 'bold'), justification='center')],
[sg.Canvas(key='IMG')]]
layout = [[sg.Column(lf_col, element_justification='left', pad=(0, 10), key='LCOL'),
sg.Column(rt_col, element_justification='center', key='RCOL')]]
window = sg.Window('Code Counter', layout, resizable=True, size=WINDOW_SIZE, finalize=True)
for elem in ['INPUT', 'OUTPUT', 'LCOL', 'TABGROUP']:
window[elem].expand(expand_x=True, expand_y=True)
# main event loop
while True:
event, values = window.read()
if event is None:
break
if event == 'Load FILE':
click_file(window)
process_data(window)
if event == 'Clipboard':
click_clipboard(window)
process_data(window)
if event == 'RESET':
click_reset(window)
if __name__ == '__main__':
main()