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:
rzmk 2024-06-20 01:03:54 -04:00
parent 9799ab694b
commit ce260e9491
14 changed files with 79 additions and 55 deletions

View file

@ -12,5 +12,6 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
anyhow = "1.0.86"
csv = "1.3.0"
czv = { path = "../czv" }
pyo3 = { version = "0.21.2", features = ["extension-module"] }
thiserror = "1.0.61"

View file

@ -1,24 +1,26 @@
# czv-python
Python library for [czv](https://github.com/rzmk/czv). czv is a library of utility functions for CSV-related data engineering and analysis tasks.
Python library for [czv](https://github.com/rzmk/czv). czv is a library of CSV-related operations for data engineering and analysis tasks.
- For a Rust library see [czv](https://github.com/rzmk/czv/tree/main/czv).
- For a WebAssembly (JavaScript, TypeScript) library see [czv-wasm](https://github.com/rzmk/czv/tree/main/czv-wasm).
## Installation and example
To install `czv`, run:
```bash
pip install czv
```
Let's say we want to print the total number of rows in a 4x3 CSV file `fruits.csv` including the header row:
```python
import czv
data = """fruits,price
apple,2.50
banana,3.00
strawberry,1.50"""
output = czv.row_count(file_path="fruits.csv", include_header_row=True)
output = czv.row_count(data, False)
print(output)
print(output) # 4
```
## Development

View file

@ -14,12 +14,7 @@ pip install czv
```python
from czv import row_count
data = \"""fruits,price
apple,2.50
banana,3.00
strawberry,1.50\"""
output = row_count(data, False)
output = row_count(file_path="fruits.csv")
print(output)
```
@ -27,20 +22,23 @@ print(output)
"""
from typing import Optional
from pathlib import Path
def row_count(file_data: str, include_header_row: Optional[bool]) -> int:
def row_count(file_path: Optional[Path], file_data: Optional[str], include_header_row: Optional[bool]) -> int:
"""Returns a count of the total number of rows.
## Arguments
* `file_path` - CSV file path.
* `file_data` - CSV file data.
* `include_header_row` - Specify whether to include the header row (first row) in the row count. Default is false.
"""
def column_count(file_data: str) -> int:
def column_count(file_path: Optional[Path], file_data: Optional[str]) -> int:
"""Returns a count of the total number of columns (fields).
## Arguments
* `file_path` - CSV file path.
* `file_data` - CSV file data.
"""

View file

@ -5,6 +5,6 @@ apple,2.50
banana,3.00
strawberry,1.50"""
output = czv.row_count(data, False)
output = czv.row_count(file_data=data, include_header_row=True)
print(output)
print(output) # 4

View file

@ -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)?)
}

View file

@ -1,3 +1,4 @@
use ::czv::CzvError as OGError;
use pyo3::prelude::*;
// Error-handling helpers
@ -5,6 +6,12 @@ use pyo3::prelude::*;
#[error("{0}")]
pub struct CzvError(anyhow::Error);
impl From<OGError> for CzvError {
fn from(value: OGError) -> Self {
value.into()
}
}
impl From<pyo3::PyErr> for CzvError {
fn from(value: pyo3::PyErr) -> Self {
value.into()

View file

@ -10,7 +10,7 @@ class TestCountFunc:
def test_count(self, file_name, expected):
"""Count the total number of non-header rows."""
result = czv.row_count(test_data[file_name].read_text())
result = czv.row_count(file_path=test_data[file_name])
assert result == expected
@pytest.mark.parametrize(
@ -20,5 +20,5 @@ class TestCountFunc:
def test_include_header_row(self, file_name, expected):
"""Count the total number of rows including the header row."""
result = czv.row_count(test_data[file_name].read_text(), include_header_row=True)
result = czv.row_count(file_path=test_data[file_name], include_header_row=True)
assert result == expected