forked from AllenDowney/ThinkPython2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram_db.py
58 lines (39 loc) · 1.25 KB
/
anagram_db.py
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
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
import shelve
import sys
from anagram_sets import all_anagrams, signature
def store_anagrams(filename, anagram_map):
"""Stores the anagrams from a dictionary in a shelf.
filename: string file name of shelf
anagram_map: dictionary that maps strings to list of anagrams
"""
shelf = shelve.open(filename, 'c')
for word, word_list in anagram_map.items():
shelf[word] = word_list
shelf.close()
def read_anagrams(filename, word):
"""Looks up a word in a shelf and returns a list of its anagrams.
filename: string file name of shelf
word: word to look up
"""
shelf = shelve.open(filename)
sig = signature(word)
try:
return shelf[sig]
except KeyError:
return []
def main(script, command='make_db'):
if command == 'make_db':
anagram_map = all_anagrams('words.txt')
store_anagrams('anagrams.db', anagram_map)
else:
print(read_anagrams('anagrams.db', command))
if __name__ == '__main__':
main(*sys.argv)