r/Python • • Jul 12 '12

random imgur downloader. learning Python and wrote this to practice, any feedback on technique/style is appreciated

https://gist.github.com/3088128
45 Upvotes

72 comments sorted by

View all comments

-1

u/tetrahydrate Jul 12 '12 edited Jul 13 '12

I'm a big fan of one-liners so here's how I'd do that random string function:

rand_string = lambda l: ''.join(random.sample(string.letters+string.digits, l))

edit: I'm wrong! I was just using random.sample for something else and it turns out it returns a sample of unique elements, which isn't a requirement of the imgur URLs.

2

u/[deleted] Jul 12 '12

''.join([random.choice(CHARS) for x in range(string_length)])

?

2

u/LucianU Jul 13 '12

''.join() accepts a generator expression, so the square brackets aren't needed.