from typing import Dict
from typing import Union
from typing import Tuple
from typing import Any


def get_item_attr(idmap: Dict, access: Union[Dict, Tuple, Any]) -> Any:
    """Access dictionary in different methods.

    Utility for accessing dict by different key types (for get).

    Args:
        idmap: A dictionary of identity map of SQLAlchemy objects.
        access: The access pattern which should either be basic data type, dictionary,
            or a tuple. If it is dictionary it should map to the names of the primary
            keys of the SQLAlchemy objects. If it is a tuple, it should be a set of
            keys to search for. If it is not a dict or a tuple, then the objects in
            question must have only one primary key of the type passed
            (such as a string, integer, etc.).

    Returns:
        An SQlAlchemy object that was requested.

    For example::
        >>> idmap = {(1,): 2}
        >>> get_item_attr(idmap, 1)
        2
        >>> idmap = {(1,): 2}
        >>> get_item_attr(idmap, {"pk": 1})
        2
        >>> get_item_attr(idmap, (1,))
        2
    """
    if isinstance(access, dict):
        keys = []
        for names in sorted(access):
            keys.append(access[names])
        return idmap.get(tuple(keys))
    elif isinstance(access, tuple):
        return idmap.get(access)
    else:
        return idmap.get((access,))