From f0c8f45a0052b96c935707503767234149cc79e3 Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 1 Jan 2021 17:19:14 +0100 Subject: [PATCH] Simplifies some Python code --- python/src/io.rs | 8 ++++---- python/src/memory_store.rs | 20 ++++++-------------- python/src/sled_store.rs | 17 +++++------------ python/tests/test_store.py | 8 ++++++++ 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/python/src/io.rs b/python/src/io.rs index e1f3d38c..a351268e 100644 --- a/python/src/io.rs +++ b/python/src/io.rs @@ -48,12 +48,12 @@ pub fn add_to_module(module: &PyModule) -> PyResult<()> { #[pyfunction] #[text_signature = "(input, /, mime_type, *, base_iri = None)"] pub fn parse( - input: &PyAny, + input: PyObject, mime_type: &str, base_iri: Option<&str>, py: Python<'_>, ) -> PyResult { - let input = BufReader::new(PyFileLike::new(input.to_object(py))); + let input = BufReader::new(PyFileLike::new(input)); if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { let mut parser = GraphParser::from_format(graph_format); if let Some(base_iri) = base_iri { @@ -113,8 +113,8 @@ pub fn parse( /// b' "1" .\n' #[pyfunction] #[text_signature = "(input, output, /, mime_type, *, base_iri = None)"] -pub fn serialize(input: &PyAny, output: &PyAny, mime_type: &str, py: Python<'_>) -> PyResult<()> { - let output = PyFileLike::new(output.to_object(py)); +pub fn serialize(input: &PyAny, output: PyObject, mime_type: &str) -> PyResult<()> { + let output = PyFileLike::new(output); if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { let mut writer = GraphSerializer::from_format(graph_format) .triple_writer(output) diff --git a/python/src/memory_store.rs b/python/src/memory_store.rs index bebfaeeb..9c5f93df 100644 --- a/python/src/memory_store.rs +++ b/python/src/memory_store.rs @@ -8,7 +8,6 @@ use pyo3::basic::CompareOp; use pyo3::exceptions::{PyNotImplementedError, PyValueError}; use pyo3::prelude::{ pyclass, pymethods, pyproto, Py, PyAny, PyCell, PyObject, PyRef, PyRefMut, PyResult, Python, - ToPyObject, }; use pyo3::{PyIterProtocol, PyObjectProtocol, PySequenceProtocol}; use std::convert::TryFrom; @@ -51,7 +50,7 @@ impl PyMemoryStore { /// [ predicate= object=> graph_name=>] #[text_signature = "($self, quad)"] fn add(&self, quad: PyQuad) { - self.inner.insert(quad); + self.inner.insert(quad) } /// Removes a quad from the store @@ -67,7 +66,7 @@ impl PyMemoryStore { /// [] #[text_signature = "($self, quad)"] fn remove(&self, quad: &PyQuad) { - self.inner.remove(quad); + self.inner.remove(quad) } /// Looks for the quads matching a given pattern @@ -236,18 +235,17 @@ impl PyMemoryStore { #[args(input, mime_type, "*", base_iri = "None", to_graph = "None")] fn load( &self, - input: &PyAny, + input: PyObject, mime_type: &str, base_iri: Option<&str>, to_graph: Option<&PyAny>, - py: Python<'_>, ) -> PyResult<()> { let to_graph_name = if let Some(graph_name) = to_graph { Some(PyGraphNameRef::try_from(graph_name)?) } else { None }; - let input = BufReader::new(PyFileLike::new(input.to_object(py))); + let input = BufReader::new(PyFileLike::new(input)); if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.inner .load_graph( @@ -304,19 +302,13 @@ impl PyMemoryStore { /// b' "1" .\n' #[text_signature = "($self, output, /, mime_type, *, from_graph = None)"] #[args(output, mime_type, "*", from_graph = "None")] - fn dump( - &self, - output: &PyAny, - mime_type: &str, - from_graph: Option<&PyAny>, - py: Python<'_>, - ) -> PyResult<()> { + fn dump(&self, output: PyObject, mime_type: &str, from_graph: Option<&PyAny>) -> PyResult<()> { let from_graph_name = if let Some(graph_name) = from_graph { Some(PyGraphNameRef::try_from(graph_name)?) } else { None }; - let output = PyFileLike::new(output.to_object(py)); + let output = PyFileLike::new(output); if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.inner .dump_graph( diff --git a/python/src/sled_store.rs b/python/src/sled_store.rs index 6e26b2d5..93ce7aea 100644 --- a/python/src/sled_store.rs +++ b/python/src/sled_store.rs @@ -6,7 +6,7 @@ use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::store::sled::*; use pyo3::exceptions::PyValueError; use pyo3::prelude::{ - pyclass, pymethods, pyproto, Py, PyAny, PyObject, PyRef, PyRefMut, PyResult, Python, ToPyObject, + pyclass, pymethods, pyproto, Py, PyAny, PyObject, PyRef, PyRefMut, PyResult, Python, }; use pyo3::{PyIterProtocol, PyObjectProtocol, PySequenceProtocol}; use std::convert::TryFrom; @@ -252,18 +252,17 @@ impl PySledStore { #[args(input, mime_type, "*", base_iri = "None", to_graph = "None")] fn load( &self, - input: &PyAny, + input: PyObject, mime_type: &str, base_iri: Option<&str>, to_graph: Option<&PyAny>, - py: Python<'_>, ) -> PyResult<()> { let to_graph_name = if let Some(graph_name) = to_graph { Some(PyGraphNameRef::try_from(graph_name)?) } else { None }; - let input = BufReader::new(PyFileLike::new(input.to_object(py))); + let input = BufReader::new(PyFileLike::new(input)); if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.inner .load_graph( @@ -321,19 +320,13 @@ impl PySledStore { /// b' "1" .\n' #[text_signature = "($self, output, /, mime_type, *, from_graph = None)"] #[args(output, mime_type, "*", from_graph = "None")] - fn dump( - &self, - output: &PyAny, - mime_type: &str, - from_graph: Option<&PyAny>, - py: Python<'_>, - ) -> PyResult<()> { + fn dump(&self, output: PyObject, mime_type: &str, from_graph: Option<&PyAny>) -> PyResult<()> { let from_graph_name = if let Some(graph_name) = from_graph { Some(PyGraphNameRef::try_from(graph_name)?) } else { None }; - let output = PyFileLike::new(output.to_object(py)); + let output = PyFileLike::new(output); if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.inner .dump_graph( diff --git a/python/tests/test_store.py b/python/tests/test_store.py index 637689f1..8b2c7b59 100644 --- a/python/tests/test_store.py +++ b/python/tests/test_store.py @@ -232,6 +232,14 @@ class TestAbstractStore(unittest.TestCase, ABC): b" .\n", ) + def test_write_in_read(self): + store = self.store() + store.add(Quad(foo, bar, bar)) + store.add(Quad(foo, bar, baz)) + for triple in store: + store.add(Quad(triple.object, triple.predicate, triple.subject)) + self.assertEqual(len(store), 4) + class TestMemoryStore(TestAbstractStore): def store(self):