Skip to content

Commit 466f036

Browse files
Count it!
1 parent c19cae0 commit 466f036

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

0x16-api_advanced/100-count.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
#!/usr/bin/python3
2-
"""parses the title of all hot articles, and prints a sorted count of given keywords"""
2+
"""
3+
parses the title of all hot articles,
4+
and prints a sorted count of given keywords
5+
"""
36

47
import requests
58
import re
69
from collections import defaultdict
710

11+
812
def count_words(subreddit, word_list, after=None, word_counts=None):
913
"""
10-
Recursively count the number of occurrences of each word from word_list in the hot articles of the given subreddit.
14+
Recursively count the number of occurrences of each word
15+
from word_list in the hot articles of the given subreddit.
1116
1217
Args:
1318
subreddit (str): The subreddit to search.
1419
word_list (list): A list of case-insensitive words to search for.
15-
after (str, optional): The fullname of a reddit thing. Used for pagination.
16-
word_counts (defaultdict, optional): A defaultdict to store the word counts.
20+
after (str, optional): The fullname of a reddit thing.
21+
Used for pagination.
22+
word_counts (defaultdict, optional):
23+
A defaultdict to store the word counts.
1724
1825
Returns:
1926
defaultdict: A defaultdict containing the word counts.
@@ -29,7 +36,8 @@ def count_words(subreddit, word_list, after=None, word_counts=None):
2936
response.raise_for_status()
3037
data = response.json()
3138
except (requests.exceptions.RequestException, ValueError, KeyError):
32-
return defaultdict(int) # Return an empty defaultdict if an error occurs
39+
return defaultdict(int)
40+
# Return an empty defaultdict if an error occurs
3341

3442
if word_counts is None:
3543
word_counts = defaultdict(int)
@@ -46,6 +54,7 @@ def count_words(subreddit, word_list, after=None, word_counts=None):
4654

4755
return word_counts
4856

57+
4958
if __name__ == "__main__":
5059
import sys
5160

0 commit comments

Comments
 (0)