pymantic.parsers

class pymantic.parsers.base.BaseParser(environment=None)[source]

Common base class for all parsers

Provides shared utilities for creating RDF objects, handling IRIs, and tracking parser state.

pymantic.parsers.jsonld

Parse RDF serialized as jsonld

Usage:

from pymantic.parsers.jsonld import jsonld_parser
graph = jsonld_parser.parse_json(json.load(io.open('file.jsonld', mode='rt')))
class pymantic.parsers.jsonld.PyLDLoader(*args, **kwargs)[source]

lark parsers

pymantic.parsers.lark.base

class pymantic.parsers.lark.base.LarkParser(lark)[source]

Provide a consistent interface for parsing serialized RDF using one of the lark parsers.

line_by_line_parser(stream)[source]
parse(string_or_stream, graph=None)[source]

Parse a string or file-like object into RDF primitives and add them to either the provided graph or a new graph.

parse_string(string_or_bytes, graph=None)[source]

Parse a string, decoding it from bytes to UTF-8 if necessary.

pymantic.parsers.lark.ntriples

Parse RDF serialized as ntriples files.

Usage:

from pymantic.parsers.lark import ntriples_parser
graph = ntriples_parser.parse(io.open('a_file.nt', mode='rt'))
graph2 = ntriples_parser.parse("<http://a.example/s> <http://a.example/p> <http://a.example/o> .")

If .parse() is called with a file-like object implementing readline, it will efficiently parse line by line rather than parsing the entire file.

class pymantic.parsers.lark.ntriples.NTriplesTransformer(environment=None)[source]

Transform the tokenized ntriples into RDF primitives.

blank_node_label(children)[source]
iriref(children)[source]
literal(children)[source]
triple(children)[source]
triples_start(children)[source]

pymantic.parsers.lark.nquads

Parse RDF serialized as nquads files.

Usage:

from pymantic.parsers.lark import nquads_parser
graph = nquads_parser.parse(io.open('a_file.nq', mode='rt'))
graph2 = nquads_parser.parse("<http://a.example/s> <http://a.example/p> <http://a.example/o> <http://a.example/g> .")

If .parse() is called with a file-like object implementing readline, it will efficiently parse line by line rather than parsing the entire file.

class pymantic.parsers.lark.nquads.NQuadsTransformer(environment=None)[source]

Transform the tokenized nquads into RDF primitives.

quad(children)[source]
quads_start(children)[source]

pymantic.parsers.lark.turtle

Parse RDF serialized as turtle files.

Usage:

from pymantic.parsers.lark import turtle_parser
graph = turtle_parser.parse(io.open('a_file.ttl', mode='rt'))
graph2 = turtle_parser.parse("""@prefix p: <http://a.example/s>.
p: <http://a.example/p> <http://a.example/o> .""")

Unlike pymantic.parsers.lark.ntriples, this parser cannot efficiently parse turtle line by line. If a file-like object is provided, the entire file will be read into memory and parsed there.

class pymantic.parsers.lark.turtle.TurtleTransformer(base_iri=u'')[source]
base(children)[source]
blank_node(children)[source]
blank_node_property_list(children)[source]
boolean_literal(children)[source]
collection(children)[source]
decode_iriref(iriref)[source]
iri(children)[source]
numeric_literal(children)[source]
predicate_object_list(children)[source]
prefix_id(children)[source]
prefixed_name(children)[source]
rdf_literal(children)[source]
sparql_base(children)[source]
sparql_prefix(children)[source]
string(children)[source]
triples(children)[source]
turtle_doc(children)[source]
pymantic.parsers.lark.turtle.parse(string_or_stream, graph=None, base=u'')[source]
pymantic.parsers.lark.turtle.parse_string(string_or_bytes, graph=None, base=u'')[source]
pymantic.parsers.lark.turtle.unpack_predicate_object_list(subject, pol)[source]
pymantic.parsers.lark.turtle.validate_iri(iri)[source]