import re


def remove_chars(text, chars, replace_with=None):
    """
    Removes chars from text or replaces them with the input.
    Also removes any extra whitespaces which might have been
    introduced.

    : param text         : text upon which the processing is done
    : param chars        : chars to search for in text and remove
    : param replace_with : any chars found in text will be replaced
                           with replace_with input
    : type chars         : string of chars eg. ',.'{}#!'
    : return text        : processed text with removed or replaced chars
    """
    if replace_with is not None:
        remove_char_map = dict((ord(char), ord(replace_with)) for char in chars)
    else:
        remove_char_map = dict((ord(char), None) for char in chars)
    text = text.translate(remove_char_map)
    # remove extra whitespace created from above processing
    text = re.sub(" +", " ", text).strip(" ")
    return text