Skip to content

Commit 15f16e1

Browse files
committed
added map datatype
introduced in STAM v1.2
1 parent 64f558c commit 15f16e1

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "stam-python"
3-
version = "0.10.2"
3+
version = "0.11.0"
44
edition = "2021"
55
authors = ["Maarten van Gompel <proycon@anaproy.nl>"]
66
description = "STAM is a library for dealing with standoff annotations on text, this is the python binding."
@@ -19,8 +19,8 @@ crate-type = ["cdylib"]
1919
[dependencies]
2020
pyo3 = { version = "0.25.0", features = ["chrono"] }
2121
rayon = "1.10.0"
22-
stam = "0.16.6"
23-
stam-tools = "0.9.3"
22+
stam = "0.17.0"
23+
stam-tools = "0.11.0"
2424

2525
[features]
2626
default = ["pyo3/extension-module"]

src/annotationdata.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use pyo3::prelude::*;
33
use pyo3::pyclass::CompareOp;
44
use pyo3::types::*;
55
use std::borrow::Cow;
6+
use std::collections::BTreeMap;
67
use std::hash::{Hash, Hasher};
78
use std::ops::FnOnce;
89
use std::sync::{Arc, RwLock};
@@ -299,6 +300,14 @@ pub(crate) fn datavalue_from_py<'py>(value: Bound<'py, PyAny>) -> Result<DataVal
299300
list.push(pyitem);
300301
}
301302
return Ok(DataValue::List(list));
303+
} else if value.is_instance_of::<PyDict>() {
304+
let value: &Bound<'py, PyDict> = value.downcast().expect("downcast must succeed");
305+
let mut map: BTreeMap<String, DataValue> = BTreeMap::new();
306+
for (key, item) in value.iter() {
307+
let pyitem = datavalue_from_py(item)?;
308+
map.insert(key.to_string(), pyitem);
309+
}
310+
return Ok(DataValue::Map(map));
302311
}
303312
Err(StamError::OtherError(
304313
"Can't convert supplied Python object to a DataValue",
@@ -333,6 +342,14 @@ pub(crate) fn datavalue_into_py<'py>(
333342
}
334343
Ok(pylist.into_any())
335344
}
345+
DataValue::Map(v) => {
346+
let pydict = PyDict::new(py);
347+
for (key, value) in v.iter() {
348+
let pyvalue = datavalue_into_py(value, py)?;
349+
pydict.add((key, pyvalue)).expect("adding value to dict");
350+
}
351+
Ok(pydict.into_any())
352+
}
336353
}
337354
}
338355

0 commit comments

Comments
 (0)