Skip to content

utilities

Misc. utilities for the iam_bindings module.

Combinable

Bases: Protocol

Interface defining methods needed for a class to be passed to combine_and_maximize.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
44
45
46
47
48
49
50
51
52
53
54
@runtime_checkable
class Combinable(Protocol):
    """Interface defining methods needed for a class to be passed to combine_and_maximize."""

    def identifier(self) -> Comparable:
        """The "grouping variable", for example RoleIAMConfig.name to group roles by name."""
        ...

    def sorter(self) -> Comparable:
        """The "sorting variable", for example RoleIAMConfig.expiry to sort roles by expiry date."""
        ...

identifier()

The "grouping variable", for example RoleIAMConfig.name to group roles by name.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
48
49
50
def identifier(self) -> Comparable:
    """The "grouping variable", for example RoleIAMConfig.name to group roles by name."""
    ...

sorter()

The "sorting variable", for example RoleIAMConfig.expiry to sort roles by expiry date.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
52
53
54
def sorter(self) -> Comparable:
    """The "sorting variable", for example RoleIAMConfig.expiry to sort roles by expiry date."""
    ...

Comparable

Bases: Protocol

Interface for classes that support igroupby.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
15
16
17
18
19
20
21
22
23
24
25
@runtime_checkable
class Comparable(Protocol):
    """Interface for classes that support igroupby."""

    def __lt__(self, __o: Any) -> bool:
        """The less than magic method."""
        ...

    def __eq__(self, __o: Any) -> bool:
        """The equals magic method."""
        ...

__eq__(__o)

The equals magic method.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
23
24
25
def __eq__(self, __o: Any) -> bool:
    """The equals magic method."""
    ...

__lt__(__o)

The less than magic method.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
19
20
21
def __lt__(self, __o: Any) -> bool:
    """The less than magic method."""
    ...

combine_and_maximize(t_list)

Combine and choose the item with the max U.sorter value for items with the same U.identifier.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
60
61
62
63
64
65
66
67
@typeguard_ignore
def combine_and_maximize(t_list: Sequence[U]) -> List[U]:
    """Combine and choose the item with the max U.sorter value for items with the same U.identifier."""
    combined: List[U] = []
    grouped = igroupby(t_list, lambda t: t.identifier())
    for t_similar in grouped.values():
        combined.append(max(t_similar, key=lambda c: c.sorter()))
    return combined

igroupby(seq, key)

Helper function to bypass some annoyances with itertools.groupby.

  • groupby does not automatically sort the sequence given to it
  • groupby returns an iterator

This helper function sorts the sequence and returns a dictionary with list values.

Source code in dapla_team_cli/tf/iam_bindings/utilities.py
32
33
34
35
36
37
38
39
40
41
@typeguard_ignore
def igroupby(seq: Sequence[T], key: Callable[[T], G]) -> Dict[G, List[T]]:
    """Helper function to bypass some annoyances with itertools.groupby.

    - groupby does not automatically sort the sequence given to it
    - groupby returns an iterator

    This helper function sorts the sequence and returns a dictionary with list values.
    """
    return {k: list(v) for k, v in groupby(sorted(seq, key=key), key=key)}