Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit ed8697c

Browse files
committed
summary file
1 parent 4b11b83 commit ed8697c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

summary.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
3+
def token_count(text):
4+
return len(text.strip().split())
5+
6+
def human_format(num):
7+
magnitude = 0
8+
while abs(num) >= 1000:
9+
magnitude += 1
10+
num /= 1000.0
11+
specific_magnitude = 0
12+
num2 = num
13+
while abs(num2) >= 1:
14+
specific_magnitude += 1
15+
num2 /= 10.0
16+
# add more suffixes if you need them
17+
return '%.{}f%s'.format(3-specific_magnitude) % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
18+
19+
row_length = None
20+
num_jokes_total = 0
21+
num_tokens_total = 0
22+
23+
ROW_FORMAT = "{:<17} | {:>5} jokes | {:>5} tokens"
24+
25+
for filename in sorted(["wocka.json", "stupidstuff.json", "reddit_jokes.json"]):
26+
with open(filename) as f:
27+
jokes = json.load(f)
28+
num_tokens = sum([token_count(x.get("body")) for x in jokes])
29+
row_str = ROW_FORMAT.format(filename, human_format(len(jokes)), human_format(num_tokens))
30+
num_jokes_total += len(jokes)
31+
num_tokens_total += num_tokens
32+
if row_length is None:
33+
row_length = len(row_str)
34+
print("-" * row_length)
35+
print(row_str)
36+
37+
print("-" * row_length)
38+
print(ROW_FORMAT.format("TOTAL", human_format(num_jokes_total), human_format(num_tokens_total)))
39+
print("-" * row_length)

0 commit comments

Comments
 (0)