本文整理匯總了Python中rdflib.namespace.Namespace方法的典型用法代碼示例。如果您正苦於以下問題:Python namespace.Namespace方法的具體用法?Python namespace.Namespace怎麽用?Python namespace.Namespace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rdflib.namespace
的用法示例。
在下文中一共展示了namespace.Namespace方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: as_resource
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def as_resource(gr,curie) :
cleaned = dequote(curie)
if cleaned[0:4] == 'http' :
return URIRef(cleaned)
# this will raise error if not valid curie format
try:
(ns,value) = cleaned.split(":",2)
except:
return URIRef(cleaned) # just have to assume its not a problem - URNs are valid uri.s
# raise ValueError("value not value HTTP or CURIE format %s" % curie)
try :
nsuri = Namespace.getNamespace(ns)
if nsuri :
gr.namespace_manager.bind( str(ns), namespace.Namespace(nsuri.uri), override=False)
return URIRef("".join((nsuri.uri,value)))
else :
return URIRef(cleaned)
except:
raise ValueError("prefix " + ns + "not recognised")
示例2: test_time
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_time():
with CSVW(csv_path="tests/datatypes.time.csv",
metadata_path="tests/datatypes.time.csv-metadata.json") as csvw:
rdf_output = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_output, format="turtle")
NS = Namespace('https://www.example.org/')
time1_lit = Literal("19:30:00", datatype=XSD.time)
assert len(list(g.triples((NS['event/1'], NS['time1'], time1_lit)))) == 1
time2_lit = Literal("09:30:10.5", datatype=XSD.time)
assert len(list(g.triples((NS['event/1'], NS['time2'], time2_lit)))) == 1
time3_lit = Literal("10:30:10Z", datatype=XSD.time)
assert len(list(g.triples((NS['event/1'], NS['time3'], time3_lit)))) == 1
time4_lit = Literal("11:30:10-06:00", datatype=XSD.time)
assert len(list(g.triples((NS['event/1'], NS['time4'], time4_lit)))) == 1
time5_lit = Literal("04:30:10+04:00", datatype=XSD.time)
assert len(list(g.triples((NS['event/1'], NS['time5'], time5_lit)))) == 1
示例3: test_encoding_rdf
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_encoding_rdf():
# With encoding specified
encoding = "ISO-8859-1"
csvw = CSVW(csv_path="./tests/iso_encoding.csv",
metadata_path="./tests/iso_encoding.csv-metadata.json",
csv_encoding=encoding)
rdf_output = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_output, format="turtle")
units = Namespace('http://example.org/units/')
cars = Namespace('http://example.org/cars/')
meta = Namespace("http://example.org/properties/")
expected_unit = units[quote(u"\xb5100".encode('utf-8'))]
assert (cars['1'], meta['UnitOfMeasurement'], expected_unit) in g
assert expected_unit in list(g.objects())
示例4: _shacl_graph_to_string
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def _shacl_graph_to_string(graph):
"""Converts a shacl validation graph into human readable format."""
sh = Namespace('http://www.w3.org/ns/shacl#')
problems = []
for _, result in graph.subject_objects(sh.result):
path = graph.value(result, sh.resultPath)
res = graph.value(result, sh.resultMessage)
if res:
message = '{0}: {1}'.format(path, res)
else:
kind = graph.value(result, sh.sourceConstraintComponent)
focusNode = graph.value(result, sh.focusNode)
if isinstance(focusNode, BNode):
focusNode = '<Anonymous>'
message = '{0}: Type: {1}, Node ID: {2}'.format(
path, kind, focusNode
)
problems.append(message)
return '\n\t'.join(problems)
示例5: expand_curie
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def expand_curie(value):
try:
parts = value.split(":")
if len(parts) == 2 :
ns = Namespace.objects.get(prefix=parts[0])
return "".join((ns.uri,parts[1]))
except:
pass
return value
示例6: validate_urisyntax
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def validate_urisyntax(value):
if value[0:4] == 'http' :
URLValidator().__call__(value)
else :
parts = value.split(":")
if len(parts) != 2 :
raise ValidationError('invalid syntax - neither http URI or a valid CURIE')
# try:
# ns = Namespace.objects.get(prefix=parts[0])
# except Exception as e:
# raise ValidationError("Namespace not defined for prefix %s" % parts[0])
示例7: getNamespace
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def getNamespace( prefix) :
try:
return Namespace.objects.get(prefix = prefix)
except:
return None
示例8: save
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def save(self,*args,**kwargs):
if self.namespace :
self.uri = "".join((self.namespace.uri,self.propname))
else:
try:
(dummy, base, sep, term) = re.split('(.*)([/#])', self.uri)
ns = Namespace.objects.get(uri="".join((base,sep)))
if ns:
self.namespace = ns
self.propname = term
except:
pass
super(GenericMetaProp, self).save(*args,**kwargs)
示例9: test_literals_with_new_lines
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_literals_with_new_lines():
csv_path = "tests/parsing.quoted_newlines.csv"
metadata_path = "tests/parsing.quoted_newlines.csv-metadata.json"
csvw = CSVW(csv_path=csv_path,
metadata_path=metadata_path)
rdf_contents = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_contents, format="turtle")
ns = Namespace("http://example.org/expense/")
desc = URIRef("http://example.org/desc")
taxi_triples = list(g.triples((ns['taxi'], desc, None)))
assert len(taxi_triples) == 1
taxi_desc = taxi_triples[0][2]
assert isinstance(taxi_desc, Literal)
assert len(taxi_desc.value.splitlines()) == 2
flight = URIRef("http://example.org/expense/multi-hop%20flight")
flight_triples = list(g.triples((flight, desc, None)))
assert len(flight_triples) == 1
flight_desc = flight_triples[0][2]
assert isinstance(flight_desc, Literal)
assert len(flight_desc.value.splitlines()) == 4
dinner_triples = list(g.triples((ns['dinner'], desc, None)))
assert len(dinner_triples) == 1
dinner_desc = dinner_triples[0][2]
assert isinstance(dinner_desc, Literal)
assert u'\u2019' in dinner_desc, "Expected to read unicode characters"
assert u"('')" in dinner_desc, "Expected to read apostrophes"
示例10: test_literals_with_escaped_quotes
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_literals_with_escaped_quotes():
csv_path = "tests/parsing.escaped_quotes.csv"
metadata_path = "tests/parsing.escaped_quotes.csv-metadata.json"
csvw = CSVW(csv_path=csv_path,
metadata_path=metadata_path)
rdf_contents = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_contents, format="turtle")
ns = Namespace("http://example.org/expense/")
desc = URIRef("http://example.org/desc")
taxi_triples = list(g.triples((ns['taxi'], desc, None)))
assert len(taxi_triples) == 1
taxi_desc = taxi_triples[0][2]
assert isinstance(taxi_desc, Literal)
assert taxi_desc.value == "go from x to y"
quoted_expense_triples = list(g.triples((URIRef("http://example.org/expense/quoted%20expense"), desc, None)))
assert len(quoted_expense_triples) == 1
quoted_expense_desc = quoted_expense_triples[0][2]
assert isinstance(quoted_expense_desc, Literal)
assert quoted_expense_desc.value == "for some reason it came with quotes in it"
flight_triples = list(g.triples((ns['flight'], desc, None)))
assert len(flight_triples) == 1
flight_desc = flight_triples[0][2]
assert isinstance(flight_desc, Literal)
assert flight_desc.value == "had to fly \"escaped quotes business\" for this trip"
car_triples = list(g.triples((ns['car'], desc, None)))
assert len(car_triples) == 1
car_desc = car_triples[0][2]
assert isinstance(car_desc, Literal)
assert car_desc.value == " some \ in it to be escaped"
示例11: test_all_triples_with_row_numbers
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_all_triples_with_row_numbers():
csvw = CSVW(csv_path='tests/virtual1.csv',
metadata_path='tests/virtual1.csv-metadata.json')
rdf_output = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_output, format="turtle")
all_subjects = {x for x in g.subjects()}
assert len(all_subjects) == 4
ns = Namespace("http://example.org/")
assert ns['sub-1'] in all_subjects
assert ns['sub-2'] in all_subjects
assert len([g.triples((ns['sub-1'], ns['obj-1'], ns['pred-1']))]) == 1
assert len([g.triples((ns['sub-2'], ns['obj-2'], ns['pred-2']))]) == 1
示例12: test_default
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_default():
csvw = CSVW(csv_path='tests/virtual1.csv',
metadata_path='tests/virtual1.default.csv-metadata.json')
rdf_output = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_output, format="turtle")
all_subjects = {x for x in g.subjects()}
assert len(all_subjects) == 4
ns = Namespace("http://example.org/")
assert ns['sub-1'] in all_subjects
assert ns['sub-2'] in all_subjects
assert len([g.triples((ns['sub-1'], ns['obj-1'], ns['myvalue']))]) == 1
assert len([g.triples((ns['sub-2'], ns['obj-2'], ns['myvalue']))]) == 1
示例13: test_default_with_datatype
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def test_default_with_datatype():
csvw = CSVW(csv_path='tests/virtual1.csv',
metadata_path='tests/virtual1.default.datatype.csv-metadata.json')
rdf_output = csvw.to_rdf()
g = ConjunctiveGraph()
g.parse(data=rdf_output, format="turtle")
ns = Namespace("http://example.org/")
for x in [1, 2]:
active_vals = list(g.triples((ns['sub-{}'.format(x)], ns['active'], None)))
assert len(active_vals) == 1
active_val = active_vals[0][2]
assert isinstance(active_val, Literal)
assert active_val.datatype == XSD.boolean
assert active_val.value
string_vals = list(g.triples((ns['sub-{}'.format(x)], ns['stringprop1'], None)))
assert len(string_vals) == 1
string_val = string_vals[0][2]
assert isinstance(string_val, Literal)
assert string_val.value == "some string"
string_vals = list(g.triples((ns['sub-{}'.format(x)], ns['stringprop2'], None)))
assert len(string_vals) == 1
string_val = string_vals[0][2]
assert isinstance(string_val, Literal)
assert "%20" not in string_val.value
示例14: verify_rdf_contents
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def verify_rdf_contents(contents, fmt):
g = ConjunctiveGraph()
g.parse(data=contents, format=fmt)
books = Namespace('http://www.books.org/')
isbn = Namespace("http://www.books.org/isbn/")
# Check number of all triples
assert sum(1 for _ in g.triples((None, None, None))) == NUM_SUBJECTS * NUM_TRIPLES_PER_SUBJ
# Check number of subject
subjs = set(g.subjects())
expected_subjs = ["0062316095", "0374532508", "1610391845", "0374275637"]
assert len(subjs) == len(expected_subjs)
for s in expected_subjs:
assert isbn[s] in subjs
# Verify isbn number is positive integer
s_isbn = list(g.triples((isbn[s], books['isbnnumber'], None)))
assert len(s_isbn) == 1
s_isbn_val = s_isbn[0][2]
assert isinstance(s_isbn_val, Literal)
assert s_isbn_val.datatype == XSD.positiveInteger
# Verify pages is a unsignedShort
s_page = list(g.triples((isbn[s], books['pagecount'], None)))
assert len(s_page) == 1
s_page_val = s_page[0][2]
assert isinstance(s_page_val, Literal)
assert s_page_val.datatype == XSD.unsignedShort
# Verify hardcover is a boolean
s_hardcover = list(g.triples((isbn[s], books['hardcover'], None)))
assert len(s_hardcover) == 1
s_hardcover_val = s_hardcover[0][2]
assert isinstance(s_hardcover_val, Literal)
assert s_hardcover_val.datatype == XSD.boolean
# Verify price is a decimal
s_price = list(g.triples((isbn[s], books['price'], None)))
assert len(s_price) == 1
s_price_val = s_price[0][2]
assert isinstance(s_price_val, Literal)
assert s_price_val.datatype == XSD.decimal
示例15: parse_config
# 需要導入模塊: from rdflib import namespace [as 別名]
# 或者: from rdflib.namespace import Namespace [as 別名]
def parse_config(self, cfgparser):
# parse namespaces from configuration file
for prefix, uri in cfgparser.items('namespaces'):
self.namespaces[prefix] = Namespace(uri)
# parse types from configuration file
for key, val in cfgparser.items('types'):
self.types[expand_curielike(self.namespaces, key)] = \
expand_mapping_target(self.namespaces, val)
# parse literals from configuration file
for key, val in cfgparser.items('literals'):
self.literals[expand_curielike(self.namespaces, key)] = \
expand_mapping_target(self.namespaces, val)
# parse relations from configuration file
for key, val in cfgparser.items('relations'):
self.relations[expand_curielike(self.namespaces, key)] = \
expand_mapping_target(self.namespaces, val)
# parse options from configuration file
for opt, val in cfgparser.items('options'):
if not hasattr(self, opt) or opt in ['types', 'literals', 'relations', 'namespaces']:
logging.warning('Ignoring unknown configuration option: %s', opt)
continue
if getattr(self, opt) in (True, False): # is a Boolean option
setattr(self, opt, cfgparser.getboolean('options', opt))
else:
setattr(self, opt, val)