def get_tags_from_namespace(tag_keys_and_values, namespace):
    """Extract the tags from namespace using tag_keys_and_values.

    Arguments:
    tag_keys_and_values -- Dictionary mapping tag keys with their valid values.
                           This  dictonary is of the form:
                           {
                               'n': [0, 1, 2],
                               'foo': ['bar'],
                           }
                           Which says that the tag 'n' can have the possible values of 0, 1 and 2.
    namespace           -- A Namespace object containing the tags passed.

    Returns:
    A dictionary containing the tags extracted from the namespace object.
    """
    tags = {}
    for tag_key in tag_keys_and_values:
        cli_tag_value = getattr(namespace, tag_key, None)
        if cli_tag_value is None:
            continue
        if cli_tag_value not in tag_keys_and_values[tag_key]:
            raise ValueError(
                'Invalid value: {value} provided for the tag: {key}'.format(key=tag_key, value=cli_tag_value))
        tags[tag_key] = cli_tag_value
    return tags