Static Protocols

#code #python #datastructure

from typing import Protocol, Any

class SupportsLessThan(Protocol):
    def __lt__(self, other: Any) -> bool: ...

from collections.abc import Iterable
from typing import TypeVar

from comparable import SupportsLessThan

LT = TypeVar('LT', bound=SupportsLessThan)

def top(series: Iterable[LT], length: int) -> list[LT]:
    ordered = sorted(series, reverse=True)
    return ordered[:length]
  • Example above shows that the bound need to implement the special method __lt__

Eu com algumas figuras que representa análise de dados/ciência
#code #python #datastructure