Skip to content

Commit

Permalink
Merge pull request #180 from promised-ai/feat/improve-python-converti…
Browse files Browse the repository at this point in the history
…on-errors

feat(python): Improved errors from type conversions.
schmidmt authored Feb 6, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 06fbf54 + 01d4ad3 commit d66cb65
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 10 additions & 7 deletions pylace/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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:?}`"
))),
}
}
@@ -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 {
@@ -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 {

0 comments on commit d66cb65

Please sign in to comment.