mirror of
https://github.com/rzmk/czv.git
synced 2025-12-19 08:09:24 +00:00
refactor: use czv in czv-python
- Add `file_path` for czv-python count operations - Refactor tests based on file path instead of data - Use thiserror in czv - Update examples to reflect changes
This commit is contained in:
parent
9799ab694b
commit
ce260e9491
14 changed files with 79 additions and 55 deletions
|
|
@ -1,33 +1,21 @@
|
|||
use crate::Result;
|
||||
use csv::ReaderBuilder;
|
||||
use pyo3::pyfunction;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Returns a count of the total number of rows.
|
||||
///
|
||||
/// ## Arguments
|
||||
///
|
||||
/// * `file_data` - CSV file data.
|
||||
/// * `include_header_row` - Specify whether to include the header row (first row) in the row count.
|
||||
#[pyfunction]
|
||||
pub fn row_count(file_data: String, include_header_row: Option<bool>) -> Result<usize> {
|
||||
let mut rdr = ReaderBuilder::new();
|
||||
|
||||
rdr.has_headers(!include_header_row.unwrap_or(false));
|
||||
return Ok(rdr.from_reader(file_data.as_bytes()).records().count());
|
||||
pub fn row_count(
|
||||
file_path: Option<PathBuf>,
|
||||
file_data: Option<String>,
|
||||
include_header_row: Option<bool>,
|
||||
) -> Result<usize> {
|
||||
Ok(czv::count::row_count(
|
||||
file_path,
|
||||
file_data,
|
||||
include_header_row.unwrap_or(false),
|
||||
)?)
|
||||
}
|
||||
|
||||
/// Returns a count of the total number of columns (fields).
|
||||
///
|
||||
/// ## Arguments
|
||||
///
|
||||
/// * `file_data` - CSV file data.
|
||||
#[pyfunction]
|
||||
pub fn column_count(file_data: Option<String>) -> Result<usize> {
|
||||
let rdr = ReaderBuilder::new();
|
||||
|
||||
if let Some(file_data) = file_data {
|
||||
return Ok(rdr.from_reader(file_data.as_bytes()).headers()?.len());
|
||||
} else {
|
||||
bail!("Could not determine a file path or file data for column_count_builder.");
|
||||
}
|
||||
pub fn column_count(file_path: Option<PathBuf>, file_data: Option<String>) -> Result<usize> {
|
||||
Ok(czv::count::column_count(file_path, file_data)?)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue