From e8e2b3fc646adccc1a8ccc749ac47ca0ae8cedc3 Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Mon, 6 Jan 2014 13:47:46 -0700 Subject: [PATCH] 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. --- r2/r2/lib/utils/utils.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/r2/r2/lib/utils/utils.py b/r2/r2/lib/utils/utils.py index 76beb9bdd..61c9d1ce6 100644 --- a/r2/r2/lib/utils/utils.py +++ b/r2/r2/lib/utils/utils.py @@ -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): """