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    fn bin_reinterpret(&self, dtype: PyDataTypeExpr, kind: &str) -> PyResult<Self> {
46        use pyo3::exceptions::PyValueError;
47
48        let is_little_endian = match kind.to_lowercase().as_str() {
49            "little" => true,
50            "big" => false,
51            _ => {
52                return Err(PyValueError::new_err(format!(
53                    "Invalid endianness: {kind}. Valid values are \"little\" or \"big\"."
54                )));
55            },
56        };
57        Ok(self
58            .inner
59            .clone()
60            .binary()
61            .reinterpret(dtype.inner, is_little_endian)
62            .into())
63    }
64
65    fn bin_size_bytes(&self) -> Self {
66        self.inner.clone().binary().size_bytes().into()
67    }
68}