GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ThinkPython2/code/anagram_db.py at master · AllenDowney/ThinkPython2 Expand file tree/ Copy pathanagram_db.pyMore file actions 58 lines (39 loc) · 1.28 KB / Copy pathanagram_db.pyFile metadata and controls58 lines (39 loc) · 1.28 KB 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 https://greenteapress.com/wp/think-python-2e/ 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) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |