Adds __repr__ implementation to Python QuerySolution

pull/43/head
Tpt 4 years ago
parent f74bc12a18
commit 4ec7caa146
  1. 12
      python/README.md
  2. 15
      python/src/store_utils.rs
  3. 3
      python/tests/test_store.py

@ -187,16 +187,14 @@ assert store.query('ASK { ?s ?s ?s }')
The `SELECT` queries return an iterator of query solutions that could be indexed by variable name or position in the `SELECT` clause:
```python
solutions = list(store.query('SELECT ?s WHERE { ?s ?p ?o }'))
assert solutions[0][0] == s
assert solutions[0]['s'] == s
for solution in store.query('SELECT ?s WHERE { ?s ?p ?o }'):
assert solution[0] == solution['s']
```
The `CONSTRUCT` and `DESCRIBE` queries return an iterator of query solutions that could be indexed by variable name or position in the `SELECT` clause:
The `CONSTRUCT` and `DESCRIBE` queries return an iterator of triples:
```python
solutions = list(store.query('SELECT ?s WHERE { ?s ?p ?o }'))
assert solutions[0][0] == s
assert solutions[0]['s'] == s
for (s, p, o) in store.query('CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }'):
print(s)
```
### `load`

@ -4,7 +4,8 @@ use oxigraph::sparql::{QueryResult, QuerySolution};
use oxigraph::Result;
use pyo3::exceptions::TypeError;
use pyo3::prelude::*;
use pyo3::{create_exception, PyIterProtocol, PyMappingProtocol, PyNativeType};
use pyo3::{create_exception, PyIterProtocol, PyMappingProtocol, PyNativeType, PyObjectProtocol};
use std::fmt::Write;
use std::vec::IntoIter;
create_exception!(oxigraph, ParseError, pyo3::exceptions::Exception);
@ -77,6 +78,18 @@ pub struct PyQuerySolution {
inner: QuerySolution,
}
#[pyproto]
impl PyObjectProtocol for PyQuerySolution {
fn __repr__(&self) -> String {
let mut buffer = "<QuerySolution".to_owned();
for (k, v) in self.inner.iter() {
write!(&mut buffer, " {}={}", k.as_str(), v).unwrap();
}
buffer.push('>');
buffer
}
}
#[pyproto]
impl PyMappingProtocol for PyQuerySolution {
fn __len__(&self) -> usize {

@ -66,8 +66,7 @@ class TestAbstractStore(unittest.TestCase, ABC):
[(foo, bar, baz, DefaultGraph()), (foo, bar, baz, graph)],
)
self.assertEqual(
list(store.match(None, None, None, graph)),
[(foo, bar, baz, graph)],
list(store.match(None, None, None, graph)), [(foo, bar, baz, graph)],
)
self.assertEqual(
list(store.match(foo, None, None, DefaultGraph())),

Loading…
Cancel
Save