polars_python/expr/
binary.rs

1use polars::prelude::DataType;
2use pyo3::prelude::*;
3
4use crate::prelude::Wrap;
5use crate::PyExpr;
6
7#[pymethods]
8impl PyExpr {
9    fn bin_contains(&self, lit: PyExpr) -> Self {
10        self.inner
11            .clone()
12            .binary()
13            .contains_literal(lit.inner)
14            .into()
15    }
16
17    fn bin_ends_with(&self, sub: PyExpr) -> Self {
18        self.inner.clone().binary().ends_with(sub.inner).into()
19    }
20
21    fn bin_starts_with(&self, sub: PyExpr) -> Self {
22        self.inner.clone().binary().starts_with(sub.inner).into()
23    }
24
25    #[cfg(feature = "binary_encoding")]
26    fn bin_hex_decode(&self, strict: bool) -> Self {
27        self.inner.clone().binary().hex_decode(strict).into()
28    }
29
30    #[cfg(feature = "binary_encoding")]
31    fn bin_base64_decode(&self, strict: bool) -> Self {
32        self.inner.clone().binary().base64_decode(strict).into()
33    }
34
35    #[cfg(feature = "binary_encoding")]
36    fn bin_hex_encode(&self) -> Self {
37        self.inner.clone().binary().hex_encode().into()
38    }
39
40    #[cfg(feature = "binary_encoding")]
41    fn bin_base64_encode(&self) -> Self {
42        self.inner.clone().binary().base64_encode().into()
43    }
44
45    #[cfg(feature = "binary_encoding")]
46    #[allow(clippy::wrong_self_convention)]
47    fn from_buffer(&self, dtype: Wrap<DataType>, kind: &str) -> PyResult<Self> {
48        use pyo3::exceptions::PyValueError;
49
50        let is_little_endian = match kind.to_lowercase().as_str() {
51            "little" => true,
52            "big" => false,
53            _ => {
54                return Err(PyValueError::new_err(format!(
55                    "Invalid endianness: {kind}. Valid values are \"little\" or \"big\"."
56                )))
57            },
58        };
59        Ok(self
60            .inner
61            .clone()
62            .binary()
63            .from_buffer(dtype.0, is_little_endian)
64            .into())
65    }
66
67    fn bin_size_bytes(&self) -> Self {
68        self.inner.clone().binary().size_bytes().into()
69    }
70}