Python Password List Generator

Posted on by

Solution: A Better Password Generator. Write a python program that takes a string and shuffles the characters in the string into a random order. Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password.

Matt: Thanks a lot for the comments. I did cut short the code a lot to achieve almost the same result. • I changed the urandom function. Now it uses choice from random. (thanks for the tip about platform independance.) • Removed the unnecessary variables. • Also the string appending operation was expensive.

Python Password List Generator

Hot Cpu Tester Pro 4.4.1+ Serial. Now it uses an array. The design choice of not wanting 2 consecutive characters was a conscious one. But, now I randomly choose a character set to generate a character from instead of using a predictable flow of iterating over all the character sets sequentially.

Maybe there is more room for improvement?

Python Password List Generator

John's answer is good (that list comprehensions are better when you want to iterate over something multiple times). However, it's also worth noting that you should use a list if you want to use any of the list methods. For example, the following code won't work: def gen(): return (something for something in get_some_stuff()) print gen()[:2] # generators don't support indexing or slicing print [5,6] + gen() # generators can't be added to lists Basically, use a generator expression if all you're doing is iterating once. If you want to store and use the generated results, then you're probably better off with a list comprehension. Since performance is the most common reason to choose one over the other, my advice is to not worry about it and just pick one; if you find that your program is running too slowly, then and only then should you go back and worry about tuning your code. The important point is that the list comprehension creates a new list.

The generator creates a an iterable object that will 'filter' the source material on-the-fly as you consume the bits. Imagine you have a 2TB log file called 'hugefile.txt', and you want the content and length for all the lines that start with the word 'ENTRY'. So you try starting out by writing a list comprehension: logfile = open('hugefile.txt','r') entry_lines = [(line,len(line)) for line in logfile if line.startswith('ENTRY')] This slurps up the whole file, processes each line, and stores the matching lines in your array. This array could therefore contain up to 2TB of content. That's a lot of RAM, and probably not practical for your purposes. So instead we can use a generator to apply a 'filter' to our content.