changeset 41:d4522af2c6e5

refactor
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 28 Dec 2013 12:56:06 -0800
parents cb1d21648588
children 871c040668c0
files bitsyauth/dictionary.py example/persona.html
diffstat 2 files changed, 33 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/bitsyauth/dictionary.py
@@ -0,0 +1,29 @@
+import random
+
+class Dictionary(object):
+    def __init__(self, dictionary_file='/usr/share/dict/american-english'):
+        self.dictionary_file = dictionary_file
+        with open(dictionary_file) as f:
+            self.wordlist = []
+            for line in f:
+                line = line.strip()
+                if not line or not line.isalpha():
+                    continue
+                line = line.lower()
+                self.wordlist.append(line)
+            self.wordlist = tuple(self.wordlist)
+        self._min_lengths = {}
+
+    def random_word(self, min_length=5):
+        """
+        choose random word
+
+        min_length -- minimum word length
+        """
+
+        if min_length not in self._min_lengths:
+            # cache
+            self._min_lengths[min_lengths] = [i for i in self.wordlist
+                                              if len(i) > min_length]
+        return random.Random().choice(self._min_lengths[min_length])
+
--- a/example/persona.html
+++ b/example/persona.html
@@ -42,21 +42,23 @@ navigator.id.watch({
       type: 'POST',
       url: '/auth/logout', // This is a URL on your website.
       success: function(res, status, xhr) { window.location.reload(); },
       error: function(xhr, status, err) { alert("Logout failure: " + err); }
     });
   }
 });
 
+
 var signinLink = document.getElementById('signin');
 if (signinLink) {
-signinLink.onclick = function() { navigator.id.request();
+signinLink.onclick = function() {
+navigator.id.request();
 nclicks += 1;
-$('clickcounter').append('<li>This is click ' + nclicks + '</li>');
+$('#clickcounter').append('<li>This is click ' + nclicks + '</li>');
 };
 }
 
 var signoutLink = document.getElementById('signout');
 if (signoutLink) {
 signoutLink.onclick = function() { navigator.id.logout(); };
 }
 });