Simplifies some Python code

pull/71/head
Tpt 4 years ago
parent 5431a28cd3
commit f0c8f45a00
  1. 8
      python/src/io.rs
  2. 20
      python/src/memory_store.rs
  3. 17
      python/src/sled_store.rs
  4. 8
      python/tests/test_store.py

@ -48,12 +48,12 @@ pub fn add_to_module(module: &PyModule) -> PyResult<()> {
#[pyfunction] #[pyfunction]
#[text_signature = "(input, /, mime_type, *, base_iri = None)"] #[text_signature = "(input, /, mime_type, *, base_iri = None)"]
pub fn parse( pub fn parse(
input: &PyAny, input: PyObject,
mime_type: &str, mime_type: &str,
base_iri: Option<&str>, base_iri: Option<&str>,
py: Python<'_>, py: Python<'_>,
) -> PyResult<PyObject> { ) -> PyResult<PyObject> {
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) { if let Some(graph_format) = GraphFormat::from_media_type(mime_type) {
let mut parser = GraphParser::from_format(graph_format); let mut parser = GraphParser::from_format(graph_format);
if let Some(base_iri) = base_iri { if let Some(base_iri) = base_iri {
@ -113,8 +113,8 @@ pub fn parse(
/// b'<http://example.com> <http://example.com/p> "1" .\n' /// b'<http://example.com> <http://example.com/p> "1" .\n'
#[pyfunction] #[pyfunction]
#[text_signature = "(input, output, /, mime_type, *, base_iri = None)"] #[text_signature = "(input, output, /, mime_type, *, base_iri = None)"]
pub fn serialize(input: &PyAny, output: &PyAny, mime_type: &str, py: Python<'_>) -> PyResult<()> { pub fn serialize(input: &PyAny, output: PyObject, mime_type: &str) -> PyResult<()> {
let output = PyFileLike::new(output.to_object(py)); let output = PyFileLike::new(output);
if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { if let Some(graph_format) = GraphFormat::from_media_type(mime_type) {
let mut writer = GraphSerializer::from_format(graph_format) let mut writer = GraphSerializer::from_format(graph_format)
.triple_writer(output) .triple_writer(output)

@ -8,7 +8,6 @@ use pyo3::basic::CompareOp;
use pyo3::exceptions::{PyNotImplementedError, PyValueError}; use pyo3::exceptions::{PyNotImplementedError, PyValueError};
use pyo3::prelude::{ use pyo3::prelude::{
pyclass, pymethods, pyproto, Py, PyAny, PyCell, PyObject, PyRef, PyRefMut, PyResult, Python, pyclass, pymethods, pyproto, Py, PyAny, PyCell, PyObject, PyRef, PyRefMut, PyResult, Python,
ToPyObject,
}; };
use pyo3::{PyIterProtocol, PyObjectProtocol, PySequenceProtocol}; use pyo3::{PyIterProtocol, PyObjectProtocol, PySequenceProtocol};
use std::convert::TryFrom; use std::convert::TryFrom;
@ -51,7 +50,7 @@ impl PyMemoryStore {
/// [<Quad subject=<NamedNode value=http://example.com> predicate=<NamedNode value=http://example.com/p> object=<Literal value=1 datatype=<NamedNode value=http://www.w3.org/2001/XMLSchema#string>> graph_name=<NamedNode value=http://example.com/g>>] /// [<Quad subject=<NamedNode value=http://example.com> predicate=<NamedNode value=http://example.com/p> object=<Literal value=1 datatype=<NamedNode value=http://www.w3.org/2001/XMLSchema#string>> graph_name=<NamedNode value=http://example.com/g>>]
#[text_signature = "($self, quad)"] #[text_signature = "($self, quad)"]
fn add(&self, quad: PyQuad) { fn add(&self, quad: PyQuad) {
self.inner.insert(quad); self.inner.insert(quad)
} }
/// Removes a quad from the store /// Removes a quad from the store
@ -67,7 +66,7 @@ impl PyMemoryStore {
/// [] /// []
#[text_signature = "($self, quad)"] #[text_signature = "($self, quad)"]
fn remove(&self, quad: &PyQuad) { fn remove(&self, quad: &PyQuad) {
self.inner.remove(quad); self.inner.remove(quad)
} }
/// Looks for the quads matching a given pattern /// Looks for the quads matching a given pattern
@ -236,18 +235,17 @@ impl PyMemoryStore {
#[args(input, mime_type, "*", base_iri = "None", to_graph = "None")] #[args(input, mime_type, "*", base_iri = "None", to_graph = "None")]
fn load( fn load(
&self, &self,
input: &PyAny, input: PyObject,
mime_type: &str, mime_type: &str,
base_iri: Option<&str>, base_iri: Option<&str>,
to_graph: Option<&PyAny>, to_graph: Option<&PyAny>,
py: Python<'_>,
) -> PyResult<()> { ) -> PyResult<()> {
let to_graph_name = if let Some(graph_name) = to_graph { let to_graph_name = if let Some(graph_name) = to_graph {
Some(PyGraphNameRef::try_from(graph_name)?) Some(PyGraphNameRef::try_from(graph_name)?)
} else { } else {
None 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) { if let Some(graph_format) = GraphFormat::from_media_type(mime_type) {
self.inner self.inner
.load_graph( .load_graph(
@ -304,19 +302,13 @@ impl PyMemoryStore {
/// b'<http://example.com> <http://example.com/p> "1" .\n' /// b'<http://example.com> <http://example.com/p> "1" .\n'
#[text_signature = "($self, output, /, mime_type, *, from_graph = None)"] #[text_signature = "($self, output, /, mime_type, *, from_graph = None)"]
#[args(output, mime_type, "*", from_graph = "None")] #[args(output, mime_type, "*", from_graph = "None")]
fn dump( fn dump(&self, output: PyObject, mime_type: &str, from_graph: Option<&PyAny>) -> PyResult<()> {
&self,
output: &PyAny,
mime_type: &str,
from_graph: Option<&PyAny>,
py: Python<'_>,
) -> PyResult<()> {
let from_graph_name = if let Some(graph_name) = from_graph { let from_graph_name = if let Some(graph_name) = from_graph {
Some(PyGraphNameRef::try_from(graph_name)?) Some(PyGraphNameRef::try_from(graph_name)?)
} else { } else {
None 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) { if let Some(graph_format) = GraphFormat::from_media_type(mime_type) {
self.inner self.inner
.dump_graph( .dump_graph(

@ -6,7 +6,7 @@ use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::store::sled::*; use oxigraph::store::sled::*;
use pyo3::exceptions::PyValueError; use pyo3::exceptions::PyValueError;
use pyo3::prelude::{ 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 pyo3::{PyIterProtocol, PyObjectProtocol, PySequenceProtocol};
use std::convert::TryFrom; use std::convert::TryFrom;
@ -252,18 +252,17 @@ impl PySledStore {
#[args(input, mime_type, "*", base_iri = "None", to_graph = "None")] #[args(input, mime_type, "*", base_iri = "None", to_graph = "None")]
fn load( fn load(
&self, &self,
input: &PyAny, input: PyObject,
mime_type: &str, mime_type: &str,
base_iri: Option<&str>, base_iri: Option<&str>,
to_graph: Option<&PyAny>, to_graph: Option<&PyAny>,
py: Python<'_>,
) -> PyResult<()> { ) -> PyResult<()> {
let to_graph_name = if let Some(graph_name) = to_graph { let to_graph_name = if let Some(graph_name) = to_graph {
Some(PyGraphNameRef::try_from(graph_name)?) Some(PyGraphNameRef::try_from(graph_name)?)
} else { } else {
None 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) { if let Some(graph_format) = GraphFormat::from_media_type(mime_type) {
self.inner self.inner
.load_graph( .load_graph(
@ -321,19 +320,13 @@ impl PySledStore {
/// b'<http://example.com> <http://example.com/p> "1" .\n' /// b'<http://example.com> <http://example.com/p> "1" .\n'
#[text_signature = "($self, output, /, mime_type, *, from_graph = None)"] #[text_signature = "($self, output, /, mime_type, *, from_graph = None)"]
#[args(output, mime_type, "*", from_graph = "None")] #[args(output, mime_type, "*", from_graph = "None")]
fn dump( fn dump(&self, output: PyObject, mime_type: &str, from_graph: Option<&PyAny>) -> PyResult<()> {
&self,
output: &PyAny,
mime_type: &str,
from_graph: Option<&PyAny>,
py: Python<'_>,
) -> PyResult<()> {
let from_graph_name = if let Some(graph_name) = from_graph { let from_graph_name = if let Some(graph_name) = from_graph {
Some(PyGraphNameRef::try_from(graph_name)?) Some(PyGraphNameRef::try_from(graph_name)?)
} else { } else {
None 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) { if let Some(graph_format) = GraphFormat::from_media_type(mime_type) {
self.inner self.inner
.dump_graph( .dump_graph(

@ -232,6 +232,14 @@ class TestAbstractStore(unittest.TestCase, ABC):
b"<http://foo> <http://bar> <http://baz> <http://graph> .\n", b"<http://foo> <http://bar> <http://baz> <http://graph> .\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): class TestMemoryStore(TestAbstractStore):
def store(self): def store(self):

Loading…
Cancel
Save