Python: uses some recent PyO3 syntactic sugars

pull/428/head
Tpt 2 years ago committed by Thomas Tanon
parent 23e47bcc5e
commit e553b6374a
  1. 12
      python/src/io.rs
  2. 84
      python/src/model.rs
  3. 18
      python/src/sparql.rs
  4. 9
      python/src/store.rs

@ -130,7 +130,7 @@ pub fn serialize(input: &PyAny, output: PyObject, mime_type: &str, py: Python<'_
.map_err(map_io_err)?; .map_err(map_io_err)?;
for i in input.iter()? { for i in input.iter()? {
writer writer
.write(&*i?.downcast::<PyCell<PyTriple>>()?.borrow()) .write(&*i?.extract::<PyRef<PyTriple>>()?)
.map_err(map_io_err)?; .map_err(map_io_err)?;
} }
writer.finish().map_err(map_io_err)?; writer.finish().map_err(map_io_err)?;
@ -141,7 +141,7 @@ pub fn serialize(input: &PyAny, output: PyObject, mime_type: &str, py: Python<'_
.map_err(map_io_err)?; .map_err(map_io_err)?;
for i in input.iter()? { for i in input.iter()? {
writer writer
.write(&*i?.downcast::<PyCell<PyQuad>>()?.borrow()) .write(&*i?.extract::<PyRef<PyQuad>>()?)
.map_err(map_io_err)?; .map_err(map_io_err)?;
} }
writer.finish().map_err(map_io_err)?; writer.finish().map_err(map_io_err)?;
@ -160,8 +160,8 @@ pub struct PyTripleReader {
#[pymethods] #[pymethods]
impl PyTripleReader { impl PyTripleReader {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self, py: Python<'_>) -> PyResult<Option<PyTriple>> { fn __next__(&mut self, py: Python<'_>) -> PyResult<Option<PyTriple>> {
@ -181,8 +181,8 @@ pub struct PyQuadReader {
#[pymethods] #[pymethods]
impl PyQuadReader { impl PyQuadReader {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self, py: Python<'_>) -> PyResult<Option<PyQuad>> { fn __next__(&mut self, py: Python<'_>) -> PyResult<Option<PyQuad>> {

@ -96,8 +96,8 @@ impl PyNamedNode {
} }
fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> { fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> {
if let Ok(other) = other.downcast::<PyCell<Self>>() { if let Ok(other) = other.extract::<PyRef<Self>>() {
Ok(op.matches(self.cmp(&other.borrow()))) Ok(op.matches(self.cmp(&other)))
} else if PyBlankNode::is_type_of(other) } else if PyBlankNode::is_type_of(other)
|| PyLiteral::is_type_of(other) || PyLiteral::is_type_of(other)
|| PyDefaultGraph::is_type_of(other) || PyDefaultGraph::is_type_of(other)
@ -202,8 +202,8 @@ impl PyBlankNode {
} }
fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> { fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> {
if let Ok(other) = other.downcast::<PyCell<Self>>() { if let Ok(other) = other.extract::<PyRef<Self>>() {
eq_compare(self, &other.borrow(), op) eq_compare(self, &other, op)
} else if PyNamedNode::is_type_of(other) } else if PyNamedNode::is_type_of(other)
|| PyLiteral::is_type_of(other) || PyLiteral::is_type_of(other)
|| PyDefaultGraph::is_type_of(other) || PyDefaultGraph::is_type_of(other)
@ -338,8 +338,8 @@ impl PyLiteral {
} }
fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> { fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> {
if let Ok(other) = other.downcast::<PyCell<Self>>() { if let Ok(other) = other.extract::<PyRef<Self>>() {
eq_compare(self, &other.borrow(), op) eq_compare(self, &other, op)
} else if PyNamedNode::is_type_of(other) } else if PyNamedNode::is_type_of(other)
|| PyBlankNode::is_type_of(other) || PyBlankNode::is_type_of(other)
|| PyDefaultGraph::is_type_of(other) || PyDefaultGraph::is_type_of(other)
@ -392,8 +392,8 @@ impl PyDefaultGraph {
} }
fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> { fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> {
if let Ok(other) = other.downcast::<PyCell<Self>>() { if let Ok(other) = other.extract::<PyRef<Self>>() {
eq_compare(self, &other.borrow(), op) eq_compare(self, &other, op)
} else if PyNamedNode::is_type_of(other) } else if PyNamedNode::is_type_of(other)
|| PyBlankNode::is_type_of(other) || PyBlankNode::is_type_of(other)
|| PyLiteral::is_type_of(other) || PyLiteral::is_type_of(other)
@ -846,13 +846,13 @@ impl PyQuad {
} }
} }
fn __iter__(slf: PyRef<'_, Self>) -> QuadComponentsIter { fn __iter__(&self) -> QuadComponentsIter {
QuadComponentsIter { QuadComponentsIter {
inner: vec![ inner: vec![
Some(slf.inner.subject.clone().into()), Some(self.inner.subject.clone().into()),
Some(slf.inner.predicate.clone().into()), Some(self.inner.predicate.clone().into()),
Some(slf.inner.object.clone()), Some(self.inner.object.clone()),
match slf.inner.graph_name.clone() { match self.inner.graph_name.clone() {
GraphName::NamedNode(node) => Some(node.into()), GraphName::NamedNode(node) => Some(node.into()),
GraphName::BlankNode(node) => Some(node.into()), GraphName::BlankNode(node) => Some(node.into()),
GraphName::DefaultGraph => None, GraphName::DefaultGraph => None,
@ -946,8 +946,8 @@ impl<'a> TryFrom<&'a PyAny> for PyNamedNodeRef<'a> {
type Error = PyErr; type Error = PyErr;
fn try_from(value: &'a PyAny) -> PyResult<Self> { fn try_from(value: &'a PyAny) -> PyResult<Self> {
if let Ok(node) = value.downcast::<PyCell<PyNamedNode>>() { if let Ok(node) = value.extract::<PyRef<PyNamedNode>>() {
Ok(Self(node.borrow())) Ok(Self(node))
} else { } else {
Err(PyTypeError::new_err(format!( Err(PyTypeError::new_err(format!(
"{} is not an RDF named node", "{} is not an RDF named node",
@ -975,10 +975,10 @@ impl<'a> TryFrom<&'a PyAny> for PyNamedOrBlankNodeRef<'a> {
type Error = PyErr; type Error = PyErr;
fn try_from(value: &'a PyAny) -> PyResult<Self> { fn try_from(value: &'a PyAny) -> PyResult<Self> {
if let Ok(node) = value.downcast::<PyCell<PyNamedNode>>() { if let Ok(node) = value.extract::<PyRef<PyNamedNode>>() {
Ok(Self::NamedNode(node.borrow())) Ok(Self::NamedNode(node))
} else if let Ok(node) = value.downcast::<PyCell<PyBlankNode>>() { } else if let Ok(node) = value.extract::<PyRef<PyBlankNode>>() {
Ok(Self::BlankNode(node.borrow())) Ok(Self::BlankNode(node))
} else { } else {
Err(PyTypeError::new_err(format!( Err(PyTypeError::new_err(format!(
"{} is not an RDF named or blank node", "{} is not an RDF named or blank node",
@ -1008,12 +1008,12 @@ impl<'a> TryFrom<&'a PyAny> for PySubjectRef<'a> {
type Error = PyErr; type Error = PyErr;
fn try_from(value: &'a PyAny) -> PyResult<Self> { fn try_from(value: &'a PyAny) -> PyResult<Self> {
if let Ok(node) = value.downcast::<PyCell<PyNamedNode>>() { if let Ok(node) = value.extract::<PyRef<PyNamedNode>>() {
Ok(Self::NamedNode(node.borrow())) Ok(Self::NamedNode(node))
} else if let Ok(node) = value.downcast::<PyCell<PyBlankNode>>() { } else if let Ok(node) = value.extract::<PyRef<PyBlankNode>>() {
Ok(Self::BlankNode(node.borrow())) Ok(Self::BlankNode(node))
} else if let Ok(node) = value.downcast::<PyCell<PyTriple>>() { } else if let Ok(node) = value.extract::<PyRef<PyTriple>>() {
Ok(Self::Triple(node.borrow())) Ok(Self::Triple(node))
} else { } else {
Err(PyTypeError::new_err(format!( Err(PyTypeError::new_err(format!(
"{} is not an RDF named or blank node", "{} is not an RDF named or blank node",
@ -1051,14 +1051,14 @@ impl<'a> TryFrom<&'a PyAny> for PyTermRef<'a> {
type Error = PyErr; type Error = PyErr;
fn try_from(value: &'a PyAny) -> PyResult<Self> { fn try_from(value: &'a PyAny) -> PyResult<Self> {
if let Ok(node) = value.downcast::<PyCell<PyNamedNode>>() { if let Ok(node) = value.extract::<PyRef<PyNamedNode>>() {
Ok(Self::NamedNode(node.borrow())) Ok(Self::NamedNode(node))
} else if let Ok(node) = value.downcast::<PyCell<PyBlankNode>>() { } else if let Ok(node) = value.extract::<PyRef<PyBlankNode>>() {
Ok(Self::BlankNode(node.borrow())) Ok(Self::BlankNode(node))
} else if let Ok(node) = value.downcast::<PyCell<PyLiteral>>() { } else if let Ok(node) = value.extract::<PyRef<PyLiteral>>() {
Ok(Self::Literal(node.borrow())) Ok(Self::Literal(node))
} else if let Ok(node) = value.downcast::<PyCell<PyTriple>>() { } else if let Ok(node) = value.extract::<PyRef<PyTriple>>() {
Ok(Self::Triple(node.borrow())) Ok(Self::Triple(node))
} else { } else {
Err(PyTypeError::new_err(format!( Err(PyTypeError::new_err(format!(
"{} is not an RDF term", "{} is not an RDF term",
@ -1094,11 +1094,11 @@ impl<'a> TryFrom<&'a PyAny> for PyGraphNameRef<'a> {
type Error = PyErr; type Error = PyErr;
fn try_from(value: &'a PyAny) -> PyResult<Self> { fn try_from(value: &'a PyAny) -> PyResult<Self> {
if let Ok(node) = value.downcast::<PyCell<PyNamedNode>>() { if let Ok(node) = value.extract::<PyRef<PyNamedNode>>() {
Ok(Self::NamedNode(node.borrow())) Ok(Self::NamedNode(node))
} else if let Ok(node) = value.downcast::<PyCell<PyBlankNode>>() { } else if let Ok(node) = value.extract::<PyRef<PyBlankNode>>() {
Ok(Self::BlankNode(node.borrow())) Ok(Self::BlankNode(node))
} else if value.downcast::<PyCell<PyDefaultGraph>>().is_ok() { } else if value.extract::<PyRef<PyDefaultGraph>>().is_ok() {
Ok(Self::DefaultGraph) Ok(Self::DefaultGraph)
} else { } else {
Err(PyTypeError::new_err(format!( Err(PyTypeError::new_err(format!(
@ -1194,8 +1194,8 @@ pub struct TripleComponentsIter {
#[pymethods] #[pymethods]
impl TripleComponentsIter { impl TripleComponentsIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self) -> Option<PyTerm> { fn __next__(&mut self) -> Option<PyTerm> {
@ -1210,8 +1210,8 @@ pub struct QuadComponentsIter {
#[pymethods] #[pymethods]
impl QuadComponentsIter { impl QuadComponentsIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self, py: Python<'_>) -> Option<PyObject> { fn __next__(&mut self, py: Python<'_>) -> Option<PyObject> {

@ -5,7 +5,6 @@ use oxigraph::model::Term;
use oxigraph::sparql::*; use oxigraph::sparql::*;
use pyo3::exceptions::{PyRuntimeError, PySyntaxError, PyTypeError, PyValueError}; use pyo3::exceptions::{PyRuntimeError, PySyntaxError, PyTypeError, PyValueError};
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::{Py, PyRef};
use std::vec::IntoIter; use std::vec::IntoIter;
pub fn parse_query( pub fn parse_query(
@ -114,11 +113,10 @@ impl PyQuerySolution {
Ok(self.inner.get(key).map(|term| PyTerm::from(term.clone()))) Ok(self.inner.get(key).map(|term| PyTerm::from(term.clone())))
} else if let Ok(key) = <&str>::extract(input) { } else if let Ok(key) = <&str>::extract(input) {
Ok(self.inner.get(key).map(|term| PyTerm::from(term.clone()))) Ok(self.inner.get(key).map(|term| PyTerm::from(term.clone())))
} else if let Ok(key) = input.downcast::<PyCell<PyVariable>>() { } else if let Ok(key) = input.extract::<PyRef<PyVariable>>() {
let key = &*key.borrow();
Ok(self Ok(self
.inner .inner
.get(<&Variable>::from(key)) .get(<&Variable>::from(&*key))
.map(|term| PyTerm::from(term.clone()))) .map(|term| PyTerm::from(term.clone())))
} else { } else {
Err(PyTypeError::new_err(format!( Err(PyTypeError::new_err(format!(
@ -143,8 +141,8 @@ pub struct SolutionValueIter {
#[pymethods] #[pymethods]
impl SolutionValueIter { impl SolutionValueIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self) -> Option<Option<PyTerm>> { fn __next__(&mut self) -> Option<Option<PyTerm>> {
@ -180,8 +178,8 @@ impl PyQuerySolutions {
.collect() .collect()
} }
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self) -> PyResult<Option<PyQuerySolution>> { fn __next__(&mut self) -> PyResult<Option<PyQuerySolution>> {
@ -205,8 +203,8 @@ pub struct PyQueryTriples {
#[pymethods] #[pymethods]
impl PyQueryTriples { impl PyQueryTriples {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self) -> PyResult<Option<PyTriple>> { fn __next__(&mut self) -> PyResult<Option<PyTriple>> {

@ -9,7 +9,6 @@ use oxigraph::sparql::Update;
use oxigraph::store::{self, LoaderError, SerializerError, StorageError, Store}; use oxigraph::store::{self, LoaderError, SerializerError, StorageError, Store};
use pyo3::exceptions::{PyIOError, PyRuntimeError, PyValueError}; use pyo3::exceptions::{PyIOError, PyRuntimeError, PyValueError};
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::{Py, PyRef};
/// RDF store. /// RDF store.
/// ///
@ -728,8 +727,8 @@ pub struct QuadIter {
#[pymethods] #[pymethods]
impl QuadIter { impl QuadIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self) -> PyResult<Option<PyQuad>> { fn __next__(&mut self) -> PyResult<Option<PyQuad>> {
@ -747,8 +746,8 @@ pub struct GraphNameIter {
#[pymethods] #[pymethods]
impl GraphNameIter { impl GraphNameIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> { fn __iter__(slf: PyRef<'_, Self>) -> PyRef<Self> {
slf.into() slf
} }
fn __next__(&mut self) -> PyResult<Option<PyNamedOrBlankNode>> { fn __next__(&mut self) -> PyResult<Option<PyNamedOrBlankNode>> {

Loading…
Cancel
Save