|
Last change
on this file was
6552,
checked in by Jeff Hammel, 14 years ago
|
- abstract wordchooser to utils.py
- stub for captchaauth. dont use yet, its not done
|
-
Property svn:executable set to
*
|
|
File size:
995 bytes
|
| Line | |
|---|
| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import random |
|---|
| 4 | import urllib |
|---|
| 5 | |
|---|
| 6 | class WordChooser(object): |
|---|
| 7 | """chose a random word given a dict file""" |
|---|
| 8 | wordlist = [] |
|---|
| 9 | def __init__(self, dictfile, min_len=5): |
|---|
| 10 | if not getattr(WordChooser, 'dictfile', '') == dictfile: |
|---|
| 11 | if dictfile.startswith("http://"): |
|---|
| 12 | f = urllib.urlopen(dictfile) |
|---|
| 13 | else: |
|---|
| 14 | f = open(dictfile, 'r') |
|---|
| 15 | _dict = f.read() |
|---|
| 16 | f.close() |
|---|
| 17 | _dict = _dict.lower().split() |
|---|
| 18 | _dict = [word for word in _dict if word.isalpha() and len(word) > min_len] |
|---|
| 19 | WordChooser.wordlist = _dict |
|---|
| 20 | WordChooser.dictfile = dictfile |
|---|
| 21 | |
|---|
| 22 | def __call__(self): |
|---|
| 23 | return random.Random().choice(self.wordlist) |
|---|
| 24 | |
|---|
| 25 | def random_word(dictfile): |
|---|
| 26 | chooser = WordChooser(dictfile) |
|---|
| 27 | return chooser() |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | if __name__ == '__main__': |
|---|
| 31 | foo = WordChooser('http://java.sun.com/docs/books/tutorial/collections/interfaces/examples/dictionary.txt') |
|---|
| 32 | print foo() |
|---|
| 33 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.