human
Complete this python program:
def attribute_rule(allowed_attrs):
assistant
#Here is the Python code you requested.

    """Generator for functions that can be used as entries in Whitelister.element_rules.
    These functions accept a tag, and modify its attributes by looking each attribute
    up in the \allowed_attrs\ dict defined here:
    * if the lookup fails, drop the attribute
    * if the lookup returns a callable, replace the attribute with the result of calling
    it - e.g. {\title\: uppercase} will replace \title\ with the result of uppercasing
    the title. If the callable returns None, the attribute is dropped
    * if the lookup returns a truthy value, keep the attribute; if falsy, drop it
    """
    def fn(tag):
        for (attr, val) in list(tag.attrs.items()):
            rule = allowed_attrs.get(attr)
            if rule:
                if callable(rule):
                    new_val = rule(val)
                    if (new_val is None):
                        del tag[attr]
                    else:
                        tag[attr] = new_val
                else:
                    pass
            else:
                del tag[attr]
    return fn