本文整理汇总了Python中rdflib.ConjunctiveGraph.commit方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.commit方法的具体用法?Python ConjunctiveGraph.commit怎么用?Python ConjunctiveGraph.commit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.commit方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
def initialize(config_file):
print '[%s] Initializing...' % strftime("%a, %d %b %Y %H:%M:%S", localtime())
sys.stdout.flush()
config = __import__(config_file)
try:
g = ConjunctiveGraph(config.graph_store, config.graph_identifier)
g.open(config.db_configstring, create=True)
if config.input_file != None:
print '[%s] Parsing %s...' % (strftime("%a, %d %b %Y %H:%M:%S", localtime()), config.input_file)
sys.stdout.flush()
g.parse(config.input_file, format=config.input_format)
g.commit()
else:
dir_list = os.listdir(config.input_dir)
for file_name in dir_list:
print '[%s] Parsing %s...' % (strftime("%a, %d %b %Y %H:%M:%S", localtime()) ,file_name)
sys.stdout.flush()
g.parse(config.input_dir + '/' + file_name, format=config.input_format)
g.commit()
except Exception as e:
traceback.print_exc()
print e
print '"%s" not found, or incorrect RDF serialization.' % config.input_file
sys.stdout.flush()
exit(-1)
return g, config
示例2: ConvertToSQLLITE
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
def ConvertToSQLLITE (filename,destinationFileName):
_graph = ConjunctiveGraph()
_graph.parse(filename, format="nt")
sql = ConjunctiveGraph('SQLite')
sql.open(destinationFileName, create=True)
for t in _graph.triples((None,None,None)):
sql.add(t)
sql.commit()
sql.close()
示例3: DeepGraphStore
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
class DeepGraphStore():
store_name = 'SQLite'
def __init__(self, create=False, parse=None):
self.parse = parse
self.create = create
self.graph = None
def setUp(self):
self.path = "" + random_file_generating()
self.graph = Graph(store=self.store_name)
self.graph.open(self.path, create=self.create)
if self.create:
if not self.parse:
self.graph.parse("http://njh.me/foaf.rdf", format='xml')
else:
self.graph.parse(self.parse)
self.graph.commit()
def open(self, path):
self.graph = ConjunctiveGraph(self.store_name)
self.path = path
self.graph.open(self.path, create=False)
def query(self, sparql_query):
return self.graph.query(sparql_query)
def parse(self, path_to_file_):
self.graph.parse(path_to_file_)
def load(self, triples):
self.graph.load(triples)
def close(self):
self.graph.close()
def size(self):
size = self.graph.__len__()
size = len(self.graph)
# self.close()
return size
示例4: Command
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
class Command(BaseCommand):
args = "<path_to_skos_file path_to_skos_file>..."
help = "import skos ref in rdflib alchemy store"
def __init__(self):
super(Command, self).__init__()
self.ident = "jocondelab"
#'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
#'NAME': '', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
#'USER': '',
#'PASSWORD': '',
#'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
#'PORT': ''
db_settings = connections['default'].settings_dict
sa_db_settings = {
'engine': 'postgresql+psycopg2' if db_settings['ENGINE'] == "django.db.backends.postgresql_psycopg2" else db_settings['ENGINE'],
'user': db_settings['USER'],
'password': db_settings['PASSWORD'],
'port': db_settings['PORT'] if db_settings['PORT'] else "5432",
'host': db_settings['HOST'] if db_settings['HOST'] else "localhost",
'name': db_settings['NAME']
}
self.connect_config = "%(engine)s://%(user)s:%(password)[email protected]%(host)s:%(port)s/%(name)s"%sa_db_settings
self.store = plugin.get("SQLAlchemy", Store)(identifier=self.ident)
self.graph = ConjunctiveGraph(self.store, identifier=self.ident)
self.graph.open(self.connect_config, create=True)
def handle(self, *args, **options):
#import pydevd #@UnresolvedImport
#pydevd.settrace(suspend=True)
for skos_path, public_id in zip(args[::2],args[1::2]):
filepath = os.path.abspath(skos_path)
self.stdout.write("Importing %s" % filepath)
self.graph.parse(filepath, publicID=public_id, format='xml')
self.stdout.write("graph size %d" % len(self.graph))
self.graph.commit()
self.graph.close()
self.store = plugin.get("SQLAlchemy", Store)(identifier=self.ident)
self.graph = ConjunctiveGraph(self.store, identifier=self.ident)
self.graph.open(self.connect_config, create=False)
self.stdout.write("correct alt labels")
litteral_statements = self.store.tables['literal_statements']
with self.store.engine.connect() as connection:
q = litteral_statements.select().where(litteral_statements.c.predicate == "http://www.w3.org/2004/02/skos/core#altLabel")
for row in connection.execute(q):
if row['object'] and row['object'] != row['object'].strip():
u_q = litteral_statements.update().where(and_(
litteral_statements.c.subject == row['subject'],
litteral_statements.c.predicate == row['predicate'],
litteral_statements.c.object == row['object'],
litteral_statements.c.context == row['context'],
litteral_statements.c.termComb == row['termcomb'],
litteral_statements.c.objLanguage == row['objlanguage'],
litteral_statements.c.objDatatype == row['objdatatype']
)).values(object = row['object'].strip() )
#u_q_compiled = u_q.compile()
#self.stdout.write("UPDATE QUERY for %s : %s : %s - %s" % (row['subject'], row['object'], str(u_q_compiled), repr(u_q_compiled.params)))
connection.execute(u_q)
self.stdout.write("graph size %d" % len(self.graph))
self.stdout.write("graph contexts %s" % repr([g for g in self.graph.contexts()]))
示例5: ManifestHelper
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
class ManifestHelper(object):
def __init__(self, uri=None):
self.uri = None
if uri:
self.uri = uri
self.reset()
def reset(self):
self.g = None
if self.uri:
self.g = ConjunctiveGraph(identifier=self.uri)
else:
self.g = ConjunctiveGraph()
self.namespaces = {}
self.urihelper = URIHelper(self.namespaces)
#add defaults
for prefix, ns in NAMESPACES.iteritems():
self.add_namespace(prefix, ns)
def from_string(self, textfile, format="xml", encoding="utf-8"):
self.reset()
self.g.parse(textfile, format)
return
def triple_exists(self, s, p, o):
if not type(self.g).__name__ in ['ConjunctiveGraph', 'Graph']:
return False
if s == '*':
s = None
if p == '*':
p = None
if o == '*':
o = None
if not isinstance(s, URIRef) and not isinstance(s, BNode) and not s == None:
s = self.urihelper.get_uriref(s)
if not isinstance(p, URIRef) and not p == None:
p = self.urihelper.parse_uri(p)
if not isinstance(o, URIRef) and not isinstance(o, Literal) and not isinstance(o, BNode) and not o == None:
if not isinstance(o, basestring):
o = unicode(o)
o = self.urihelper.parse_uri(o, return_Literal_not_Exception=True)
count = 0
for ans_s, ans_p, ans_o in self.g.triples((s, p, o)):
count += 1
if count > 0:
return True
else:
return False
def list_objects(self, s, p):
objects = []
if not type(self.g).__name__ in ['ConjunctiveGraph', 'Graph']:
return objects
if s == '*':
s = None
if p == '*':
p = None
if not isinstance(s, URIRef) and not isinstance(s, BNode) and not s == None:
s = self.urihelper.get_uriref(s)
if not isinstance(p, URIRef) and not p == None:
p = self.urihelper.parse_uri(p)
for o in self.g.objects(s, p):
objects.append(o)
return objects
def add_triple(self, s, p, o):
if not isinstance(s, URIRef) and not isinstance(s, BNode):
s = self.urihelper.get_uriref(s)
if not isinstance(p, URIRef):
p = self.urihelper.parse_uri(p)
if not isinstance(o, URIRef) and not isinstance(o, Literal) and not isinstance(o, BNode):
if not isinstance(o, basestring):
o = unicode(o)
o = self.urihelper.parse_uri(o, return_Literal_not_Exception=True)
self.g.add((s, p, o))
self.g.commit()
return
def add_namespace(self, prefix, uri):
if not isinstance (prefix, basestring):
raise TypeError('Add namespace: prefix is not of type string or unicode')
if not isinstance(uri, (URIRef, Namespace)):
if not isinstance(uri, basestring):
raise TypeError('Add namespace: namespace is not of type string or unicode')
if not isinstance(prefix, unicode):
prefix = unicode(prefix)
if isinstance(uri, basestring) and not isinstance(uri, unicode):
#.........这里部分代码省略.........
示例6: ConjunctiveGraph
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
'''
from rdflib import ConjunctiveGraph, plugin
from rdflib.store import Store, VALID_STORE
from rdflib import URIRef
from api import BASE_GRAPH_URI, _get_mysql_config_string
store = plugin.get('MySQL', Store)(identifier='rdfstore')
rt = store.open(_get_mysql_config_string(), create=True)
assert rt == VALID_STORE,"The underlying store is corrupted"
citg = ConjunctiveGraph(store, identifier=URIRef(BASE_GRAPH_URI))
'''
citg = ConjunctiveGraph('MySQL', identifier=URIRef(BASE_GRAPH_URI))
rt = citg.open(_get_mysql_config_string(), create=True)
assert rt == VALID_STORE,"The underlying store is corrupted"
'''
citg.commit()
citg.close()
store.close()
print "Successfully initialized database"
示例7: export
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import commit [as 别名]
def export(self, rlist):
rset = set()
for rid in rlist:
rset.add(rid)
rtype = self.get_property(rid, RDF.type)
# Get resources (2-level for Collections)
if rtype == PIMO['Collection']:
for s, p, o in self.ask.get_triples(URIRef(rid)):
if type(o) == URIRef:
if o.startswith('vazaar'):
rset.add(o)
for s2, p2, o2 in self.ask.get_triples(URIRef(o)):
if type(o2) == URIRef:
if o2.startswith('vazaar'):
rset.add(o2)
else:
for s, p, o in self.ask.get_triples(URIRef(rid)):
if type(o) == URIRef:
if o.startswith('vazaar'):
rset.add(o)
# Add user info
user_owner_pim_uri = self.app.cfgmgr.get_value('PIMO', 'PIM')
user_owner_uri = self.ask.get_owner(user_owner_pim_uri)
user_owner_email_uri = self.ask.get_owner_email(user_owner_uri)
rset.add(user_owner_pim_uri)
rset.add(user_owner_uri)
rset.add(user_owner_email_uri)
# Get triples from selected resources
triples = []
for rid in rset:
triples += self.ask.get_triples(URIRef(rid))
# Build graph
g = ConjunctiveGraph()
#Bind namespaces to graph
for ns in NSBINDINGS:
g.bind(ns, NSBINDINGS[ns])
# Add triples from selected resources
for triple in triples:
g.add(triple)
g.commit()
# Build export environment
pid = self.ask.get_pim_id() # get PersonalInformationModel
backup_dir = LPATH['TMP']
now = datetime.now()
bdate = '%4d%02d%02d' % (now.year, now.month, now.day)
btime = '%02d%02d%02d' % (now.hour, now.minute, now.second)
temp_export_dir = LPATH['TMP'] + '/' + bdate + btime
relative_export_dir = bdate + btime
temp_metadata_dir = temp_export_dir + '/metadata'
temp_data_dir = temp_export_dir + '/data'
metadata_file = temp_metadata_dir + '/' + pid[9:] + '.rdf'
targz_file = backup_dir + '/' + bdate + btime + '.vzr'
command = 'cd %s; tar cfz %s %s' % (LPATH['TMP'], targz_file, relative_export_dir)
os.makedirs(temp_export_dir)
os.makedirs(temp_metadata_dir)
os.makedirs(temp_data_dir)
# Copy selected resources contents to temporal backup directory
for rid in rset:
rfile = rid[9:]
src = LPATH['RESOURCES'] + '/' + rfile
dst = temp_data_dir + '/' + rfile
try:
shutil.copy(src, dst)
except Exception, error:
pass #self.log.error('shutil: %s' % error)