def as_bool(x):
    """Convert value (possibly a string) into a boolean.
    """
    if x is True or x is False:
        return x
    if isinstance(x, int):
        return bool(x)
    retval = None
    if isinstance(x, str):
        retval = {'yes': True, 'no': False, 'on': True, 'off': False, '1': 
            True, '0': False, 'true': True, 'false': False}.get(x.lower(), None
            )
    if retval is None:
        raise ValueError('Cannot convert to bool: %s' % x)
    return retval
