Skip to content

Commit

Permalink
refactor: use more-itertools
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed May 12, 2024
1 parent f2bf733 commit a6b19d1
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 59 deletions.
12 changes: 3 additions & 9 deletions plugin/rules/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@
from pathlib import Path
from typing import Any, Pattern, TypeVar, final

from more_itertools import first_true

from ..cache import clearable_lru_cache
from ..constants import PLUGIN_NAME, ST_PLATFORM
from ..snapshot import ViewSnapshot
from ..types import Optimizable, ST_ConstraintRule
from ..utils import (
camel_to_snake,
compile_regex,
first_true,
list_all_subclasses,
merge_regexes,
parse_regex_flags,
remove_suffix,
)
from ..utils import camel_to_snake, compile_regex, list_all_subclasses, merge_regexes, parse_regex_flags, remove_suffix

T = TypeVar("T")

Expand Down
4 changes: 3 additions & 1 deletion plugin/rules/constraints/contains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from typing import Any, final

from more_itertools import nth

from ...snapshot import ViewSnapshot
from ...utils import nth, str_finditer
from ...utils import str_finditer
from ..constraint import AbstractConstraint


Expand Down
3 changes: 2 additions & 1 deletion plugin/rules/constraints/contains_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from typing import Any, final

from more_itertools import nth

from ...snapshot import ViewSnapshot
from ...utils import nth
from ..constraint import AbstractConstraint


Expand Down
4 changes: 3 additions & 1 deletion plugin/rules/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from dataclasses import dataclass, field
from typing import Any, Union, final

from more_itertools import first_true

from ..cache import clearable_lru_cache
from ..snapshot import ViewSnapshot
from ..types import Optimizable, ST_MatchRule
from ..utils import camel_to_snake, first_true, list_all_subclasses, remove_suffix
from ..utils import camel_to_snake, list_all_subclasses, remove_suffix
from .constraint import ConstraintRule


Expand Down
3 changes: 2 additions & 1 deletion plugin/rules/matches/ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from typing import Any, final

from more_itertools import nth

from ...snapshot import ViewSnapshot
from ...utils import nth
from ..match import AbstractMatch, MatchableRule


Expand Down
3 changes: 2 additions & 1 deletion plugin/rules/matches/some.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from typing import Any, final

from more_itertools import nth

from ...snapshot import ViewSnapshot
from ...utils import nth
from ..match import AbstractMatch, MatchableRule


Expand Down
3 changes: 2 additions & 1 deletion plugin/rules/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from dataclasses import dataclass

import sublime
from more_itertools import first_true

from ..constants import VERSION
from ..snapshot import ViewSnapshot
from ..types import ListenerEvent, Optimizable, ST_SyntaxRule
from ..utils import find_syntax_by_syntax_likes, first_true
from ..utils import find_syntax_by_syntax_likes
from .match import MatchRule


Expand Down
46 changes: 2 additions & 44 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
import threading
from collections.abc import Generator, Iterable, Mapping
from functools import cmp_to_key, lru_cache, reduce, wraps
from itertools import islice
from pathlib import Path
from typing import Any, Callable, Pattern, TypeVar, Union, cast, overload
from typing import Any, Callable, Pattern, TypeVar, Union, cast

import sublime
from more_itertools import first_true

from .cache import clearable_lru_cache
from .libs.trie import TrieNode
from .types import SyntaxLike

_T = TypeVar("_T")
_U = TypeVar("_U")

_T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any])
_T_ExpandableVar = TypeVar("_T_ExpandableVar", bound=Union[None, bool, int, float, str, dict, list, tuple])
Expand Down Expand Up @@ -147,34 +146,6 @@ def call_function() -> Any:
return decorator


@overload
def first_true(
items: Iterable[_T],
default: _U,
pred: Callable[[_T], bool] | None = None,
) -> _T | _U: ...


@overload
def first_true(
items: Iterable[_T],
*,
pred: Callable[[_T], bool] | None = None,
) -> _T | None: ...


def first_true(
items: Iterable[_T],
default: _U | None = None,
pred: Callable[[_T], bool] | None = None,
) -> _T | _U | None:
"""
Gets the first item which satisfies the `pred`. Otherwise, `default`.
If `pred` is not given or `None`, the first truthy item will be returned.
"""
return next(filter(pred, items), default)


def list_all_subclasses(
root: type[_T],
skip_abstract: bool = False,
Expand All @@ -187,19 +158,6 @@ def list_all_subclasses(
yield from list_all_subclasses(leaf, skip_self=False, skip_abstract=skip_abstract)


@overload
def nth(items: Iterable[_T], n: int) -> _T | None: ...


@overload
def nth(items: Iterable[_T], n: int, default: _U) -> _T | _U: ...


def nth(items: Iterable[_T], n: int, default: _U | None = None) -> _T | _U | None:
"""Gets the `n`th item (started from 0th) in `items`. Returns `default` if no such item."""
return next(islice(iter(items), n, None), default)


def stable_unique(items: Iterable[_T], *, key: Callable[[_T], Any] | None = None) -> Generator[_T, None, None]:
"""Lists unique items from the iterable, in their original order."""
key = key or (lambda x: x)
Expand Down

0 comments on commit a6b19d1

Please sign in to comment.