import inspect
from enum import Enum


def find_object_key_with_value(
    obj:object, 
    needle:object,
    throw_exception=False
) -> str:
    """Given an  class or class instance, search the 
    attribute values of the object for the given "needle" and return its corresponding key.

    Note: If a class if given then it must be instantiable using a default constructor.
    
    Args:
        obj: Class or class instance
        needle: Class attribute value to find in class instance 
        throw_exception: If true, throw an exception if the needle is not found, return 'none' otherwise
    Return:
        Lowercase key of found attribute value or "none" if value is not found
    """
    
    if inspect.isclass(obj) and not issubclass(obj, Enum):
        obj = obj()
    
    for key in dir(obj):
        if getattr(obj, key) == needle:
            return key.lower()
        
    if throw_exception:
        raise ValueError(f'{needle} not found in {obj}')

    return 'none'