|
Revision 1, 1.2 KB
(checked in by alex, 4 years ago)
|
- initial djangoquest import
|
| Line | |
|---|
| 1 | |
|---|
| 2 | def nicepass(alpha=6,numeric=2): |
|---|
| 3 | """ |
|---|
| 4 | returns a human-readble password (say rol86din instead of |
|---|
| 5 | a difficult to remember K8Yn9muL ) |
|---|
| 6 | |
|---|
| 7 | From: http://code.activestate.com/recipes/410076/ |
|---|
| 8 | """ |
|---|
| 9 | import string |
|---|
| 10 | import random |
|---|
| 11 | vowels = ['a','e','i','o','u'] |
|---|
| 12 | consonants = [a for a in string.ascii_lowercase if a not in vowels] |
|---|
| 13 | digits = string.digits |
|---|
| 14 | |
|---|
| 15 | ####utility functions |
|---|
| 16 | def a_part(slen): |
|---|
| 17 | ret = '' |
|---|
| 18 | for i in range(slen): |
|---|
| 19 | if i%2 ==0: |
|---|
| 20 | randid = random.randint(0,20) #number of consonants |
|---|
| 21 | ret += consonants[randid] |
|---|
| 22 | else: |
|---|
| 23 | randid = random.randint(0,4) #number of vowels |
|---|
| 24 | ret += vowels[randid] |
|---|
| 25 | return ret |
|---|
| 26 | |
|---|
| 27 | def n_part(slen): |
|---|
| 28 | ret = '' |
|---|
| 29 | for i in range(slen): |
|---|
| 30 | randid = random.randint(0,9) #number of digits |
|---|
| 31 | ret += digits[randid] |
|---|
| 32 | return ret |
|---|
| 33 | |
|---|
| 34 | #### |
|---|
| 35 | fpl = alpha/2 |
|---|
| 36 | if alpha % 2 : |
|---|
| 37 | fpl = int(alpha/2) + 1 |
|---|
| 38 | lpl = alpha - fpl |
|---|
| 39 | |
|---|
| 40 | start = a_part(fpl) |
|---|
| 41 | mid = n_part(numeric) |
|---|
| 42 | end = a_part(lpl) |
|---|
| 43 | |
|---|
| 44 | return "%s%s%s" % (start,mid,end) |
|---|
| 45 | |
|---|