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]
#[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<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) {
let mut parser = GraphParser::from_format(graph_format);
if let Some(base_iri) = base_iri {
@ -113,8 +113,8 @@ pub fn parse(
/// b'<http://example.com> <http://example.com/p> "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)

@ -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 {
/// [<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)"]
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'<http://example.com> <http://example.com/p> "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(

@ -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'<http://example.com> <http://example.com/p> "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(

@ -232,6 +232,14 @@ class TestAbstractStore(unittest.TestCase, ABC):
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):
def store(self):

Loading…
Cancel
Save