randstr: Allow specifying valid chars directly

Removes the "really_random" argument from the randstr util function, and
replaces it with an argument that allows specifying the character
choices instead of just having the two options available.
This commit is contained in:
Chad Birch
2014-01-06 13:47:46 -07:00
parent 79ddb15072
commit e8e2b3fc64

View File

@@ -54,16 +54,10 @@ from r2.lib.utils._utils import *
iters = (list, tuple, set)
def randstr(len, reallyrandom = False):
"""If reallyrandom = False, generates a random alphanumeric string
(base-36 compatible) of length len. If reallyrandom, add
uppercase and punctuation (which we'll call 'base-93' for the sake
of argument) and suitable for use as salt."""
alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
if reallyrandom:
alphabet += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&\()*+,-./:;<=>?@[\\]^_{|}~'
return ''.join(random.choice(alphabet)
for i in range(len))
def randstr(length,
alphabet='abcdefghijklmnopqrstuvwxyz0123456789'):
"""Return a string made up of random chars from alphabet."""
return ''.join(random.choice(alphabet) for _ in xrange(length))
class Storage(dict):
"""