本文整理汇总了Python中rdflib.ConjunctiveGraph.quads方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.quads方法的具体用法?Python ConjunctiveGraph.quads怎么用?Python ConjunctiveGraph.quads使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.quads方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_quad_contexts
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def test_quad_contexts():
g = ConjunctiveGraph()
a = URIRef('urn:a')
b = URIRef('urn:b')
g.get_context(a).add((a, a, a))
g.addN([(b, b, b, b)])
assert set(g) == set([(a, a, a), (b, b, b)])
for q in g.quads():
assert isinstance(q[3], Graph)
示例2: addTrig
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def addTrig(graph, url, timeout=2):
t1 = time.time()
# workaround for some reason my ipv6 names don't resolve
for name, addr in ipv6Addresses.iteritems():
url = url.replace('/' + name + ':', '/[' + addr + ']:')
log.debug(' fetching %r', url)
response = yield treq.get(url, headers={'accept': ['application/trig']}, timeout=timeout)
if response.code != 200:
raise ValueError("status %s from %s" % (response.code, url))
g = ConjunctiveGraph()
g.parse(StringInputSource((yield response.content())), format='trig')
fetchTime = time.time() - t1
log.debug(' %r done in %.04f sec', url, fetchTime)
graph.addN(g.quads())
returnValue(fetchTime)
示例3: __init__
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def __init__(self, location, repository, inmemory=False):
super(RDFLibStore, self).__init__(location, repository)
self.inmemory = inmemory
self.closed = False
graphid = URIRef("file://" + self.repository)
g = ConjunctiveGraph(store=self._storeid(), identifier=graphid)
if os.path.exists(self.location):
g.open(self.location, create=False)
else:
g.open(self.location, create=True)
l = logging.getLogger(__name__)
if inmemory:
l.debug("Loading store into memory")
ig = ConjunctiveGraph(identifier=graphid)
ig.addN(g.quads())
g.close()
self.graph = ig
else:
l.debug("Using on-disk store")
self.graph = g
示例4: store
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def store(self, cur_g, base_dir, base_iri, context_path, tmp_dir=None,
override=False, already_processed={}, store_now=True):
self.repok.new_article()
self.reperr.new_article()
if len(cur_g) > 0:
cur_subject = set(cur_g.subjects(None, None)).pop()
cur_dir_path, cur_file_path = find_paths(
str(cur_subject), base_dir, base_iri, self.dir_split, self.n_file_item)
try:
if not os.path.exists(cur_dir_path):
os.makedirs(cur_dir_path)
final_g = ConjunctiveGraph()
final_g.addN([item + (cur_g.identifier,) for item in list(cur_g)])
# Merging the data
if not override:
if cur_file_path in already_processed:
stored_g = already_processed[cur_file_path]
stored_g.addN(final_g.quads((None, None, None, None)))
final_g = stored_g
elif os.path.exists(cur_file_path):
# This is a conjunctive graps that contains all the triples (and graphs)
# the file is actually defining - they could be more than those using
# 'cur_subject' as subject.
final_g = self.load(cur_file_path, cur_g, tmp_dir)
already_processed[cur_file_path] = final_g
if store_now:
self.__store_in_file(final_g, cur_file_path, context_path)
return already_processed
except Exception as e:
self.reperr.add_sentence("[5] It was impossible to store the RDF statements in %s. %s" %
(cur_file_path, str(e)))
return None
示例5: ConjunctiveGraph
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
from rdflib import URIRef,Literal, Namespace, Graph, ConjunctiveGraph, RDF
import rdflib
pkg_resources.require("rdfextras>=0.1")
from rdfextras import *
file_store=rdflib.plugin.get('Sleepycat',rdflib.store.Store)()
code = file_store.open("test.db", create=True)
if code != rdflib.store.VALID_STORE:
print "something went wrong"
sys.exit(-1)
all_graphs = ConjunctiveGraph(store = file_store)
print "things already in the store"
for s,p,o,m in all_graphs.quads((None, None, None)):
print s,p,o,m
file_store.bind('dc', Namespace("http://purl.org/dc/elements/1.1/"))
dc = Namespace("http://purl.org/dc/elements/1.1/")
a_graph = Graph(store=file_store, identifier=URIRef("http://example.com/graph1"))
a_graph.add( (URIRef("http://example.com/i1"), dc["creator"], Literal("Author "+str(random.randint(1,1000))) ) )
print "things in a_graph"
for s,p,o in a_graph:
print s,p,o
b_graph = Graph(store=file_store, identifier=URIRef("http://example.com/graph2"))
示例6: __init__
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
class RDFCrawler:
logger = logging.getLogger(__name__)
def __init__(self, uri, domains=set()):
"""
:param uri: root URI to start crawling .
:param domains: list of permits domains to crawl.
"""
self.root = uri
self.graph_route = 'graph_store_%s' % hash(self.root)
self.graph = ConjunctiveGraph('Sleepycat')
self.graph.open(self.graph_route, create=True)
self._filter_domains = domains
self._filter_domains.add(uri)
self.last_process_time = 0.0
self.lock = RLock()
def filter_uris(self, uri_list):
"""
:param uri_list: list of URIs to be filtered.
:return: filtered list of URIs.
"""
return [uri for uri in uri_list for match in self._filter_domains
if match in str(uri)]
def _has_context(self, graph, subject):
"""
:param subject: the URIRef or URI to check if it has current context.
:return: True if subject has a current context.
"""
return len(graph.get_context(self._get_context_id(subject))) > 1
@staticmethod
def _get_context_id(subject):
"""
:param subject: URIRef or URI from which the get context id.
:return: context id of the resource.
Example:
subject -> http://www.example.org/#fragment
context_id -> http://www.example.org/
"""
return str(subject).split('#')[0]
def start(self):
"""
start method for crawling.
"""
self.lock.acquire(True)
# Erase old graph
for q in self.graph.quads():
self.graph.remove(q)
# Crawl for data
logging.info('Start crawling: %s' % self.root)
start_time = time.time()
self._crawl([self.root])
end_time = time.time()
self.last_process_time = end_time - start_time
logging.info('Crawling complete after: %s seconds with %s predicates.'
% (self.last_process_time, len(self.graph)))
self.lock.release()
def _crawl(self, uri_list):
"""
Recursive method that crawl RDF objects
:param uri_list: list of URIs to crawl
"""
if len(uri_list) > 0:
for uri in uri_list:
try:
# A few considerations about parsing params.
# publicID = uri due to redirection issues
# Format = None due to default params use 'XML'
self.graph.parse(uri, publicID=uri, format=None)
logging.info('[OK]: %s' % uri)
except Exception as e:
logging.info('[Error]: %s: %s' % (uri, e))
# Check that there are context that remains without parsing
objects = set([self._get_context_id(o)
for o in set(self.graph.objects(None, None))
if isinstance(o, URIRef) and
not self._has_context(self.graph, o)])
self._crawl(self.filter_uris(objects))
示例7: testPerfectAddAllowsExistingStmtInNewContext
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def testPerfectAddAllowsExistingStmtInNewContext(self):
g = ConjunctiveGraph()
patchQuads(g, [], [stmt1])
patchQuads(g, [], [stmt2], perfect=True)
self.assertEqual(len(list(g.quads((None,None,None)))), 2)
示例8: testDeleteRunsBeforeAdd
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def testDeleteRunsBeforeAdd(self):
g = ConjunctiveGraph()
patchQuads(g, [stmt1], [stmt1])
quads = list(g.quads((None,None,None)))
self.assertEqual(quads, [(A, B, C, Graph(identifier=CTX1))])
示例9: testDeletes
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def testDeletes(self):
g = ConjunctiveGraph()
patchQuads(g, [], [stmt1])
patchQuads(g, [stmt1], [])
quads = list(g.quads((None,None,None)))
self.assertEqual(quads, [])
示例10: testAddsToNewContext
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import quads [as 别名]
def testAddsToNewContext(self):
g = ConjunctiveGraph()
patchQuads(g, [], [stmt1])
self.assert_(len(g), 1)
quads = list(g.quads((None,None,None)))
self.assertEqual(quads, [(A, B, C, Graph(identifier=CTX1))])