[redland-dev] Redland in Python questions

Dave Beckett dave at dajobe.org
Mon Jul 10 04:59:28 BST 2006


Uldis Bojars wrote:
> Hi, All!
> 
> Could you help with a couple of questions about using Redland's Python
> bindings?
> 
> 1) The 'handler' callback in RDF.Parser
> 
> There is a 'handler' callback that can be supplied to Parser methods
> parse_into_model and parse_string_into_model.
> 
> This callback is good for logging, but a strange thing happens - these
> parser methods always return True (success) if the 'handler' callback
> is used when their return value should be indicating if the parsing
> failed or not.
> 
> Is this supposed to work this way? Is there some way to use the
> handler to throw an exception (a simple 'raise Exception' does not do
> anything) or to indicate what return value (true/false) should these
> parsing methods return?

Yes it is meant to work this way.  Raising exceptions from C+SWIG to
python is rather tricky and has generated very wierd responses when I've
experimented with it previously.  Since you can run whatever code you
like in the parser, you can track it yourself when parsing fails this
way and check your own failure flag after the parse method exits.


> 2) Model.contains_statement()
> 
> Is there a way to check if the model contains one or more statements
> matching a particular statement "template"?

No.

> E.g., to use model.contains_statement( RDF.Statement( None,
> rdfs.seeAlso, None) ) to find if there are statements with a predicate
> rdfs.seeAlso.
> 
> There is a method find_statements(), but it would perform more work
> than necessary - there is no need to find all statements in this case.

This is asking to do multiple triple matches at once, which handily is
what SPARQL/RDQL can let you do.  That might be slightly more efficient
for you, I'm not sure.  You can use a SPARQL ASK statment for this:

ASK WHERE
{
  :a :b :c .
  :d :e :f
}

and it'll return true/false if all those triples/statements are contained.

Dave


More information about the redland-dev mailing list