|
| 1 | +"""Deviations from `polars`. |
| 2 | +
|
| 3 | +- A `Selector` corresponds to a `nw.selectors` function |
| 4 | +- Binary ops are represented as a subtype of `BinaryExpr` |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import re |
| 10 | +from typing import TYPE_CHECKING, Iterable |
| 11 | + |
| 12 | +from narwhals._plan.common import Immutable, is_iterable_reject |
| 13 | +from narwhals.utils import _parse_time_unit_and_time_zone |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from datetime import timezone |
| 17 | + from typing import Iterator, TypeVar |
| 18 | + |
| 19 | + from narwhals._plan.dummy import DummySelector |
| 20 | + from narwhals._plan.expr import SelectorIR |
| 21 | + from narwhals.dtypes import DType |
| 22 | + from narwhals.typing import TimeUnit |
| 23 | + |
| 24 | + T = TypeVar("T") |
| 25 | + |
| 26 | + |
| 27 | +class Selector(Immutable): |
| 28 | + def to_selector(self) -> SelectorIR: |
| 29 | + from narwhals._plan.expr import SelectorIR |
| 30 | + |
| 31 | + return SelectorIR(selector=self) |
| 32 | + |
| 33 | + |
| 34 | +class All(Selector): ... |
| 35 | + |
| 36 | + |
| 37 | +class ByDType(Selector): |
| 38 | + __slots__ = ("dtypes",) |
| 39 | + |
| 40 | + dtypes: frozenset[DType | type[DType]] |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def from_dtypes( |
| 44 | + *dtypes: DType | type[DType] | Iterable[DType | type[DType]], |
| 45 | + ) -> ByDType: |
| 46 | + return ByDType(dtypes=frozenset(_flatten_hash_safe(dtypes))) |
| 47 | + |
| 48 | + |
| 49 | +class Boolean(Selector): ... |
| 50 | + |
| 51 | + |
| 52 | +class Categorical(Selector): ... |
| 53 | + |
| 54 | + |
| 55 | +class Datetime(Selector): |
| 56 | + """Should swallow the [`utils` functions]. |
| 57 | +
|
| 58 | + Just re-wrapping them for now, since `CompliantSelectorNamespace` is still using them. |
| 59 | +
|
| 60 | + [`utils` functions]: https://github.com/narwhals-dev/narwhals/blob/6d524ba04fca6fe2d6d25bdd69f75fabf1d79039/narwhals/utils.py#L1565-L1596 |
| 61 | + """ |
| 62 | + |
| 63 | + __slots__ = ("time_units", "time_zones") |
| 64 | + |
| 65 | + time_units: frozenset[TimeUnit] |
| 66 | + time_zones: frozenset[str | None] |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def from_time_unit_and_time_zone( |
| 70 | + time_unit: TimeUnit | Iterable[TimeUnit] | None, |
| 71 | + time_zone: str | timezone | Iterable[str | timezone | None] | None, |
| 72 | + /, |
| 73 | + ) -> Datetime: |
| 74 | + units, zones = _parse_time_unit_and_time_zone(time_unit, time_zone) |
| 75 | + return Datetime(time_units=frozenset(units), time_zones=frozenset(zones)) |
| 76 | + |
| 77 | + |
| 78 | +class Matches(Selector): |
| 79 | + __slots__ = ("pattern",) |
| 80 | + |
| 81 | + pattern: re.Pattern[str] |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def from_string(pattern: str, /) -> Matches: |
| 85 | + return Matches(pattern=re.compile(pattern)) |
| 86 | + |
| 87 | + |
| 88 | +class Numeric(Selector): ... |
| 89 | + |
| 90 | + |
| 91 | +class String(Selector): ... |
| 92 | + |
| 93 | + |
| 94 | +def all() -> DummySelector: |
| 95 | + return All().to_selector().to_narwhals() |
| 96 | + |
| 97 | + |
| 98 | +def by_dtype( |
| 99 | + *dtypes: DType | type[DType] | Iterable[DType | type[DType]], |
| 100 | +) -> DummySelector: |
| 101 | + return ByDType.from_dtypes(*dtypes).to_selector().to_narwhals() |
| 102 | + |
| 103 | + |
| 104 | +def boolean() -> DummySelector: |
| 105 | + return Boolean().to_selector().to_narwhals() |
| 106 | + |
| 107 | + |
| 108 | +def categorical() -> DummySelector: |
| 109 | + return Categorical().to_selector().to_narwhals() |
| 110 | + |
| 111 | + |
| 112 | +def datetime( |
| 113 | + time_unit: TimeUnit | Iterable[TimeUnit] | None = None, |
| 114 | + time_zone: str | timezone | Iterable[str | timezone | None] | None = ("*", None), |
| 115 | +) -> DummySelector: |
| 116 | + return ( |
| 117 | + Datetime.from_time_unit_and_time_zone(time_unit, time_zone) |
| 118 | + .to_selector() |
| 119 | + .to_narwhals() |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +def matches(pattern: str) -> DummySelector: |
| 124 | + return Matches.from_string(pattern).to_selector().to_narwhals() |
| 125 | + |
| 126 | + |
| 127 | +def numeric() -> DummySelector: |
| 128 | + return Numeric().to_selector().to_narwhals() |
| 129 | + |
| 130 | + |
| 131 | +def string() -> DummySelector: |
| 132 | + return String().to_selector().to_narwhals() |
| 133 | + |
| 134 | + |
| 135 | +def _flatten_hash_safe(iterable: Iterable[T | Iterable[T]], /) -> Iterator[T]: |
| 136 | + """Fully unwrap all levels of nesting. |
| 137 | +
|
| 138 | + Aiming to reduce the chances of passing an unhashable argument. |
| 139 | + """ |
| 140 | + for element in iterable: |
| 141 | + if isinstance(element, Iterable) and not is_iterable_reject(element): |
| 142 | + yield from _flatten_hash_safe(element) |
| 143 | + else: |
| 144 | + yield element # type: ignore[misc] |
0 commit comments