Skip to content

Commit

Permalink
Use a sorted str instead of namedtuple as key
Browse files Browse the repository at this point in the history
  • Loading branch information
Kushagra-0801 committed Sep 14, 2019
1 parent 20c8173 commit 10a01b6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 43 deletions.
6 changes: 2 additions & 4 deletions extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import Counter
import pickle
import requests
from helpers import LetterFreq, BASE_DICT, get_anagram_hub_id, get_questions
from helpers import get_anagram_hub_id, get_questions

parser = argparse.ArgumentParser()
parser.add_argument("--name", help="Extension name", dest="name")
Expand All @@ -24,9 +24,7 @@
auth_header = {"Authorization": args.auth_token}
hub_id = get_anagram_hub_id(args.api_url, auth_header)
for question in get_questions(args.api_url, auth_header, hub_id):
word = Counter(question)
word.update(BASE_DICT)
word = LetterFreq(**word)
word = "".join(sorted(question))
possible_answers = WORD_DICT[word]
for answer in possible_answers:
requests.post(
Expand Down
43 changes: 4 additions & 39 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,6 @@

WORD_LIST = "wordlist.txt"

LetterFreq = namedtuple(
"LetterFreq", "a b c d e f g h i j k l m n o p q r s t u v w x y z"
)

BASE_DICT = {
"a": 0,
"b": 0,
"c": 0,
"d": 0,
"e": 0,
"f": 0,
"g": 0,
"h": 0,
"i": 0,
"j": 0,
"k": 0,
"l": 0,
"m": 0,
"n": 0,
"o": 0,
"p": 0,
"q": 0,
"r": 0,
"s": 0,
"t": 0,
"u": 0,
"v": 0,
"w": 0,
"x": 0,
"y": 0,
"z": 0,
}


def get_anagram_hub_id(api_url: str, auth_header: dict) -> int:
"""Return the seesion_id for anagram game hub."""
Expand Down Expand Up @@ -89,16 +56,14 @@ def make_dict(word_list: str) -> None:
Open word_list and create/overwite word_dict.pickle with the contents of processed list.
"""
word_dict = {}
with open(word_dict) as f:
with open(word_list) as f:
for word in f:
word = word.strip()
word_hash = Counter(word)
word_hash.update(BASE_DICT)
word_hash = LetterFreq(**word_hash)
word_key = "".join(sorted(word))
try:
word_dict[word_hash].append(word)
word_dict[word_key].append(word)
except KeyError:
word_dict[word_hash] = [word]
word_dict[word_key] = [word]

with open("word_dict.pickle", "bw+") as f:
pickle.dump(word_dict, f)
Expand Down
Binary file modified word_dict.pickle
Binary file not shown.

0 comments on commit 10a01b6

Please sign in to comment.