Matthew Schinckel avatar Matthew Schinckel committed 85129ca

refactor.

Comments (0)

Files changed (1)

wordlist/helpers.py

 import gzip
+import os
 import random
 
-MALE_NAMES = gzip.open('data/male-names.gz').read().strip().split('\n')
-FEMALE_NAMES = gzip.open('data/female-names.gz').read().strip().split('\n')
-LAST_NAMES = gzip.open('data/Family-names.gz').read().strip().split('\n')
+data = os.path.join(os.path.dirname(__file__), 'data')
 
-WORDS = gzip.open('data/Unabr.dict.gz').read().strip().split('\n')
+def read(filename):
+    return gzip.open(os.path.join(data, filename)).read().strip().split('\n')
+
+MALE_NAMES = read('male-names.gz')
+FEMALE_NAMES = read('female-names.gz')
+LAST_NAMES = read('Family-names.gz')
+
+WORDS = read('Unabr.dict.gz')
 
 WORD_TYPES = {
-    'animal': gzip.open('data/animals.gz').read().strip().split('\n'),
-    'plant': gzip.open('data/plants.gz').read().strip().split('\n'),
-    'colour': gzip.open('data/colours.gz').read().strip().split('\n'),
-    'shape': gzip.open('data/shapes.gz').read().strip().split('\n'),
-    'verb': gzip.open('data/verbs.gz').read().strip().split('\n'),
+    'animal': read('animals.gz'),
+    'plant': read('plants.gz'),
+    'colour': read('colours.gz'),
+    'shape': read('shapes.gz'),
+    'verb': read('verbs.gz'),
 }
 
 FIRST_NAMES = {
     'female': FEMALE_NAMES
 }
 
+_WORDS = {}
+
 def digits(length=3):
     result = []
     for i in range(length):
 def generate_full_name(gender=None):
     return first_name(gender) + ' ' + last_name()
 
-def generate_passphrase(words=4):
+def generate_passphrase(words=4, length=None):
+    if length:
+        if length not in _WORDS:
+            _WORDS[length] = [x for x in WORDS if len(x) <= length]
+        source = _WORDS[length]
+    else:
+        source = WORDS
     result = []
     for i in range(words):
-        result.append(random.choice(WORDS))
+        result.append(random.choice(source))
     return " ".join(result)
 
 def generate_nice_passphrase(*terms):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.