Generate real random string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# This snippet uses os.urandom, so it's pretty well random

import os
import string

def generate_random_string(length, stringset=string.ascii_letters+string.digits+string.punctuation):
    '''
    Returns a string with `length` characters chosen from `stringset`
    >>> len(generate_random_string(20) == 20 
    '''
    return ''.join([stringset[i%len(stringset)] \
        for i in [ord(x) for x in os.urandom(length)]])

More like this

  1. Generate Model Data. Lots of Options. by bl4th3rsk1t3 3 years, 1 month ago
  2. real_printf by zeeg 6 years ago
  3. Template tag to dump database query info by pryankster 6 years, 1 month ago
  4. Generate a dineromail form in python by grillermo 1 year, 2 months ago
  5. Model with random ID by jobs@flowgram.com 4 years, 11 months ago

Comments

(Forgotten your password?)