Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-python/src/expr/binary.rs
7889 views
1
use pyo3::prelude::*;
2
3
use super::datatype::PyDataTypeExpr;
4
use crate::PyExpr;
5
6
#[pymethods]
7
impl 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
69
fn bin_slice(&self, offset: PyExpr, length: PyExpr) -> Self {
70
self.inner
71
.clone()
72
.binary()
73
.slice(offset.inner, length.inner)
74
.into()
75
}
76
77
fn bin_head(&self, n: PyExpr) -> Self {
78
self.inner.clone().binary().head(n.inner).into()
79
}
80
81
fn bin_tail(&self, n: PyExpr) -> Self {
82
self.inner.clone().binary().tail(n.inner).into()
83
}
84
}
85
86