pyracmon.util API documentation
Description: Utility types and functions for internal use.

# Module `pyracmon.util`

Utility types and functions for internal use.

Expand source code

    """
    Utility types and functions for internal use.
    """
    from collections.abc import Mapping, Sequence, Callable
    from typing import Any, Union, TypeVar
    try:
        from typing import TypeAlias
    except:
        from typing_extensions import TypeAlias

    T = TypeVar('T')

    #----------------------------------------------------------------
    # Utility Types
    #----------------------------------------------------------------
    Qualifier: TypeAlias = Callable[[str], str]

    PKS = Union[Any, dict[str, Any]]
    """Primary key(s)."""

    #----------------------------------------------------------------
    # Utility Functions
    #----------------------------------------------------------------
    def key_to_index(values: Mapping[str, T], ordered_keys: Sequence[str]) -> dict[int, T]:
        """
        Replace keys of a `dict` with its index in ordered list.

        Args:
            values: A dictionary.
            ordered_keys: Ordered keys.
        Returns:
            New dictionary where keys are replaced.
        """
        def index(k):
            if isinstance(k, int):
                return k
            else:
                return ordered_keys.index(k)

        return {index(k):v for k, v in values.items()}

## Global variables

`var PKS`

  Primary key(s).

## Functions

`def key_to_index(values: collections.abc.Mapping[str, ~T], ordered_keys: collections.abc.Sequence[str]) ‑> dict[int, ~T]`

  Replace keys of a `dict` with its index in ordered list.

  ## Args

  `values`
    A dictionary.
  `ordered_keys`
    Ordered keys.

  ## Returns

  New dictionary where keys are replaced.

  Expand source code

      def key_to_index(values: Mapping[str, T], ordered_keys: Sequence[str]) -> dict[int, T]:
          """
          Replace keys of a `dict` with its index in ordered list.

          Args:
              values: A dictionary.
              ordered_keys: Ordered keys.
          Returns:
              New dictionary where keys are replaced.
          """
          def index(k):
              if isinstance(k, int):
                  return k
              else:
                  return ordered_keys.index(k)

          return {index(k):v for k, v in values.items()}