def expression(expr):
    """
    Returns a function that treats the keys of a dictionary as locals, and
    returns the expression, e.g.:

    >>> expression("foo")({'foo': 'bar'})
    'bar'
    >>> expression("bar + 1")({'bar': 2})
    3
    >>> expression("get('foo bar') == 'baz'")({'foo bar': 'baz'})
    True
    """
    if callable(expr):
        return expr

    if len(expr) == 0:
        return lambda d: None

    def _expr(data, **kwargs):
        context = locals()
        if type(data) is dict:
            context["get"] = data.get
        else:
            context["get"] = lambda attr: getattr(data, attr)
        context.update(data)
        context.update(kwargs)
        return eval(expr, globals(), context)
    _expr.__doc__ = expr
    return _expr
Returns a function that treats the keys of a dictionary as locals, and
    returns the expression, e.g.:

    >>> expression("foo")({'foo': 'bar'})
    'bar'
    >>> expression("bar + 1")({'bar': 2})
    3
    >>> expression("get('foo bar') == 'baz'")({'foo bar': 'baz'})
    True