polars_python/expr/
binary.rs

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