python bintols
cargo devel CI / cargo CI (push) Successful in 3m40s Details

This commit is contained in:
Christoph J. Scherr 2024-03-01 20:44:14 +01:00
parent 10ec77aef0
commit 6d4e82b0b7
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,56 @@
use pyo3::prelude::*;
use libpt::bintols as origin;
mod display {
use pyo3::prelude::*;
use libpt::bintols::display as origin;
#[pyfunction]
pub fn bytes_to_bin(data: &[u8]) -> String {
origin::bytes_to_bin(data)
}
#[pyfunction]
pub fn byte_bit_display(data: usize) -> String {
origin::byte_bit_display(data)
}
#[pyfunction]
pub fn humanbytes(total: u128) -> String {
origin::humanbytes(total)
}
/// implement a python module in Rust
pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> {
let module = PyModule::new(py, "display")?;
module.add_function(wrap_pyfunction!(bytes_to_bin, module)?)?;
module.add_function(wrap_pyfunction!(byte_bit_display, module)?)?;
module.add_function(wrap_pyfunction!(humanbytes, module)?)?;
parent.add_submodule(module)?;
Ok(())
}
}
/// implement a python module in Rust
pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> {
let module = PyModule::new(py, "bintols")?;
// binary constants
module.add("KIBI", origin::KIBI)?;
module.add("MEBI", origin::MEBI)?;
module.add("GIBI", origin::GIBI)?;
module.add("TEBI", origin::TEBI)?;
module.add("PEBI", origin::PEBI)?;
module.add("EXBI", origin::EXBI)?;
module.add("ZEBI", origin::ZEBI)?;
module.add("YOBI", origin::YOBI)?;
display::submodule(py, module)?;
parent.add_submodule(module)?;
Ok(())
}

View File

@ -4,6 +4,8 @@
mod core;
#[cfg(feature = "log")]
mod log;
#[cfg(feature = "bintols")]
mod bintols;
use pyo3::prelude::*;
@ -20,6 +22,9 @@ fn libpt_py(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(version, m)?)?;
#[cfg(feature = "core")]
core::submodule(py, m)?;
#[cfg(feature = "log")]
log::submodule(py, m)?;
#[cfg(feature = "bintols")]
bintols::submodule(py, m)?;
Ok(())
}