Skip to content

Commit

Permalink
avoid private _utils import
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Mar 14, 2024
1 parent efef157 commit 406f9b7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
12 changes: 2 additions & 10 deletions polars_hash/polars_hash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
from typing import Iterable, Protocol, cast

import polars as pl
from packaging.version import Version
from polars.type_aliases import IntoExpr, PolarsDataType
from polars.utils.udfs import _get_shared_lib_location

from polars_hash._internal import __version__ as __version__

if Version(pl.__version__) >= Version("0.20.14"):
from polars._utils.parse_expr_input import parse_as_expression
from polars._utils.wrap import wrap_expr
else:
# old locations prior to https://github.com/pola-rs/polars/commit/b8d7a0f5492b662787d790c35712f0daabd01429
from polars.utils._parse_expr_input import parse_as_expression # type: ignore
from polars.utils._wrap import wrap_expr # type: ignore

from polars_hash.utils import parse_into_expr

lib = _get_shared_lib_location(__file__)

Expand Down Expand Up @@ -158,7 +150,7 @@ def to_coords(self) -> pl.Expr:

def from_coords(self, len: int | str | pl.Expr = 12) -> pl.Expr:
"""Takes Struct with latitude, longitude as input and returns utf8 hash using geohash."""
len_expr = wrap_expr(parse_as_expression(len))
len_expr = parse_into_expr(len)
return self._expr.register_plugin(
lib=lib,
args=[len_expr],
Expand Down
49 changes: 49 additions & 0 deletions polars_hash/polars_hash/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import polars as pl

if TYPE_CHECKING:
from polars.type_aliases import IntoExpr, PolarsDataType


def parse_into_expr(
expr: IntoExpr,
*,
str_as_lit: bool = False,
list_as_lit: bool = True,
dtype: PolarsDataType | None = None,
) -> pl.Expr:
"""
Parse a single input into an expression.
Parameters
----------
expr
The input to be parsed as an expression.
str_as_lit
Interpret string input as a string literal. If set to `False` (default),
strings are parsed as column names.
list_as_lit
Interpret list input as a lit literal, If set to `False`,
lists are parsed as `Series` literals.
dtype
If the input is expected to resolve to a literal with a known dtype, pass
this to the `lit` constructor.
Returns
-------
polars.Expr
"""
if isinstance(expr, pl.Expr):
pass
elif isinstance(expr, str) and not str_as_lit:
expr = pl.col(expr)
elif isinstance(expr, list) and not list_as_lit:
expr = pl.lit(pl.Series(expr), dtype=dtype)
else:
expr = pl.lit(expr, dtype=dtype)

return expr

0 comments on commit 406f9b7

Please sign in to comment.