From 4698663a85a6327fe8146df26f58a5d7f6b43b59 Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Sun, 10 Jun 2018 09:21:35 -0400 Subject: [PATCH] Rewrite generate_random_string() Rewrote generate_random_string() in a more Pythonic fashion. --- spotifyvis/views.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 1385371..12774d4 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -7,6 +7,7 @@ import os import urllib import json import pprint +import string from datetime import datetime from django.shortcuts import render, redirect @@ -32,11 +33,8 @@ def generate_random_string(length): Returns: A random string """ - rand_str = "" - possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" - - for _ in range(length): - rand_str += possible_chars[random.randint(0, len(possible_chars) - 1)] + all_chars = string.ascii_letters + string.digits + string.punctuation + rand_str = "".join(random.choice(all_chars) for _ in range(length)) return rand_str