forked from tinygrad/tinygrad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_tokens.py
More file actions
executable file
·35 lines (28 loc) · 977 Bytes
/
count_tokens.py
File metadata and controls
executable file
·35 lines (28 loc) · 977 Bytes
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
#!/usr/bin/env python3
import os
import token
import tokenize
TOKEN_WHITELIST = [
token.OP,
token.NAME,
token.NUMBER,
token.STRING
]
if __name__ == "__main__":
count_by_file = {}
for path, subdirs, files in os.walk("tinygrad"):
for name in files:
if not name.endswith(".py"):
continue
filepath = os.path.join(path, name)
with tokenize.open(filepath) as file_:
tokens = tokenize.generate_tokens(file_.readline)
count_by_file[filepath] = len([t for t in tokens if t.type in TOKEN_WHITELIST])
count_by_file = dict(sorted(count_by_file.items(), key=lambda el: el[1], reverse=True))
max_length = max(len(k) for k in count_by_file.keys()) + 10
print(f"{'File':<{max_length}} {'Token count'}")
print('-' * (max_length + 14))
for key, value in count_by_file.items():
print(f"{key:<{max_length}} {value}")
print('-' * (max_length + 14))
print(f"{'Total':<{max_length}} {sum(count_by_file.values())}")