Solution 1

import string
import random


class SimplePasswordGenerator(object):

    def __init__(self, available_chars=None, length=8):
        if not available_chars:
            available_chars = list(
                string.ascii_lowercase + string.digits + string.punctuation)

        self.available_chars = available_chars
        self.length = length

    def __iter__(self):
        return self

    def next(self):  # use __next__ in Python 3.x
        return ''.join([
            random.choice(self.available_chars) for _ in range(self.length)])

    __next__ = next

Write a simple random password generator using iterators. The generator can accept two parameters:
available_chars: The characters to use to generate the password. OPTIONAL. Default: A set of lowercase characters, digits and symbols. (hint, check the string module)
length: The length of the password. OPTIONAL. Default: 8

pass_generator = SimplePasswordGenerator()
next(pass_generator)  # %58a$d8a (length=8)

pass_generator = SimplePasswordGenerator()
next(pass_generator)  # '%58a$d8a' (length=8)

pass_generator = SimplePasswordGenerator(available_chars=['a', 'b'], length=4)
next(pass_generator)  # 'abba'

Note: Make sure you implement the iterator in a way compatible with both Python 2 and Python 3

Test Cases

test respects passed length - Run Test

def test_respects_passed_length():
    length_2 = SimplePasswordGenerator(length=2)
    length_2_it = iter(length_2)
    assert len(next(length_2_it)) == 2

    length_4 = SimplePasswordGenerator(length=4)
    length_4_it = iter(length_4)
    assert len(next(length_4_it)) == 4

    length_16 = SimplePasswordGenerator(length=16)
    length_16_it = iter(length_16)
    assert len(next(length_16_it)) == 16

test uses available chars - Run Test

def test_uses_available_chars():
    pass_gen = SimplePasswordGenerator(available_chars=['a', 'b'], length=2)
    it = iter(pass_gen)
    password = next(it)

    assert (
        set(password) == set(['a', 'b']) or
        set(password) == set(['a']) or
        set(password) == set(['b'])
    )
class SimplePasswordGenerator(object): def __iter__(self): pass def next(self): # use __next__ in Python 3.x pass