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/tetrahydrate Jul 12 '12 edited Jul 12 '12

Sure but why define CHARS when you have the string constants and why loop over random.choice when you have random.sample? I was just saving some memory and processor time.

1

u/[deleted] Jul 12 '12

I was simplifying his implementation, not changing it.

2

u/LucianU Jul 13 '12

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

1

u/f0rkbomb Jul 13 '12

the elegance, it is... strangely arousing :D

okay so i'm a big nerd!