Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Improved errors from type conversions. #180

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Added parallelism to `Slice` row reassignment kernel. Run time is ~6x faster.
- (Python) Improved errors in type conversions.

### Fixed
- Initializing an engine with a codebook that has a different number of rows than the data will result in an error instead of printing a bunch on nonsense.
Expand Down
17 changes: 10 additions & 7 deletions pylace/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,11 @@ pub(crate) fn value_to_datum(val: &PyAny, ftype: FType) -> PyResult<Datum> {

match ftype {
FType::Continuous => {
let x: f64 = val.extract().unwrap();
let x: f64 = val.extract().map_err(|err| {
PyTypeError::new_err(format!(
"Expected real number, got `{val}` ({err})"
))
})?;
if x.is_nan() {
Ok(Datum::Missing)
} else {
Expand All @@ -543,10 +547,9 @@ pub(crate) fn value_to_datum(val: &PyAny, ftype: FType) -> PyResult<Datum> {
let x = pyany_to_category(val)?;
Ok(Datum::Categorical(x))
}
FType::Count => Ok(Datum::Count(val.extract().unwrap())),
ftype => Err(PyErr::new::<PyValueError, _>(format!(
"Unsupported ftype: {:?}",
ftype
FType::Count => Ok(Datum::Count(val.extract()?)),
ftype => Err(PyTypeError::new_err(format!(
"Unsupported ftype: `{ftype:?}`"
))),
}
}
Expand All @@ -556,7 +559,7 @@ pub(crate) fn value_to_name(
indexer: &Indexer,
) -> PyResult<String> {
val.extract::<String>().or_else(|_| {
let ix: usize = val.extract().unwrap();
let ix: usize = val.extract()?;
if let Some(name) = indexer.to_name.get(&ix) {
Ok(name.to_owned())
} else {
Expand All @@ -570,7 +573,7 @@ pub(crate) fn value_to_index(
indexer: &Indexer,
) -> PyResult<usize> {
val.extract::<usize>().or_else(|_| {
let s: &str = val.extract().unwrap();
let s: &str = val.extract()?;
if let Some(ix) = indexer.to_ix.get(s) {
Ok(*ix)
} else {
Expand Down
Loading