I'm attempting to generate all the possible passwords with lengths between 1-5 characters, where the possible characters are the lowercase alphabet, uppercase alphabet and 10 digits.
For the sake of keeping things simple, I'm restricting the possible characters to just the lowercase alphabet and the ten digits, and the lengths of the passwords to just 3-5 characters.
import itertools
charMix = list("abcdefghijklmnopqrstuvwxyz1234567890")
mix = []
for length in range(3, 6):
temp = [''.join(p) for p in itertools.product(charMix, repeat=length)]
mix.append(temp)
However, I'm running into memory errors on the temp
assignment line and don't know how to overcome them :(
Is there a way to generate these passwords without memory errors?
Since you actually mention the term generate, consider a generator here if that will satisfy your use case:
from typing import Generator
import itertools
def make_pws(join="".join) -> Generator[str, None, None]:
charMix = "abcdefghijklmnopqrstuvwxyz1234567890"
for length in range(3, 6):
for p in itertools.product(charMix, repeat=length):
yield join(p)
You can iterate through this result like a sequence without placing the entire contents in memory:
>>> pws = make_pws()
>>> next(pws)
'aaa'
>>> next(pws)
'aab'
>>> next(pws)
'aac'
>>> next(pws)
'aad'
>>> for pw in make_pws():
... # process each one at a time