def obtain_valid_painter(painter_class, **kwargs):
    """Returns a valid painter whose class is <painter_class>. You can try any
    argument you want ; only arguments existing in painter's __init__ method
    will be used.
    """
    try:
        painter = painter_class(**kwargs)
    except TypeError:
        painter = painter_class()
        args_okay = {}
        for arg in kwargs:
            if hasattr(painter, arg):
                args_okay[arg] = kwargs[arg]
        painter = painter_class(**args_okay)
    return painter