Python rdflib.namespace.Namespace() Examples
The following are 17
code examples of rdflib.namespace.Namespace().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rdflib.namespace
, or try the search function
.
Example #1
Source File: models.py From django-rdf-io with Creative Commons Zero v1.0 Universal | 6 votes |
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")
Example #2
Source File: test_encoding.py From pycsvw with Apache License 2.0 | 6 votes |
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())
Example #3
Source File: test_datatypes.py From pycsvw with Apache License 2.0 | 6 votes |
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
Example #4
Source File: models.py From django-rdf-io with Creative Commons Zero v1.0 Universal | 5 votes |
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
Example #5
Source File: skosify.py From Skosify with MIT License | 5 votes |
def detect_namespace(rdf): """Try to automatically detect the URI namespace of the vocabulary. Return namespace as URIRef. """ # pick a concept conc = rdf.value(None, RDF.type, SKOS.Concept, any=True) if conc is None: logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) ln = localname(conc) ns = URIRef(conc.replace(ln, '')) if ns.strip() == '': logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) logging.info( "Namespace auto-detected to '%s' " "- you can override this with the --namespace option.", ns) return ns
Example #6
Source File: config.py From Skosify with MIT License | 5 votes |
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)
Example #7
Source File: test_formats.py From pycsvw with Apache License 2.0 | 5 votes |
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
Example #8
Source File: test_virtual_columns.py From pycsvw with Apache License 2.0 | 5 votes |
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
Example #9
Source File: test_virtual_columns.py From pycsvw with Apache License 2.0 | 5 votes |
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
Example #10
Source File: test_virtual_columns.py From pycsvw with Apache License 2.0 | 5 votes |
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
Example #11
Source File: test_new_lines_and_escapes.py From pycsvw with Apache License 2.0 | 5 votes |
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"
Example #12
Source File: test_new_lines_and_escapes.py From pycsvw with Apache License 2.0 | 5 votes |
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"
Example #13
Source File: models.py From django-rdf-io with Creative Commons Zero v1.0 Universal | 5 votes |
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)
Example #14
Source File: models.py From django-rdf-io with Creative Commons Zero v1.0 Universal | 5 votes |
def getNamespace( prefix) : try: return Namespace.objects.get(prefix = prefix) except: return None
Example #15
Source File: models.py From django-rdf-io with Creative Commons Zero v1.0 Universal | 5 votes |
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])
Example #16
Source File: validate_shacl.py From renku-python with Apache License 2.0 | 5 votes |
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)
Example #17
Source File: skosify.py From Skosify with MIT License | 4 votes |
def create_concept_scheme(rdf, ns, lname=''): """Create a skos:ConceptScheme in the model and return it.""" ont = None if not ns: # see if there's an owl:Ontology and use that to determine namespace onts = list(rdf.subjects(RDF.type, OWL.Ontology)) if len(onts) > 1: onts.sort() ont = onts[0] logging.warning( "Multiple owl:Ontology instances found. " "Creating concept scheme from %s.", ont) elif len(onts) == 1: ont = onts[0] else: ont = None if not ont: logging.info( "No skos:ConceptScheme or owl:Ontology found. " "Using namespace auto-detection for creating concept scheme.") ns = detect_namespace(rdf) elif ont.endswith('/') or ont.endswith('#') or ont.endswith(':'): ns = ont else: ns = ont + '/' NS = Namespace(ns) cs = NS[lname] rdf.add((cs, RDF.type, SKOS.ConceptScheme)) if ont is not None: rdf.remove((ont, RDF.type, OWL.Ontology)) # remove owl:imports declarations for o in rdf.objects(ont, OWL.imports): rdf.remove((ont, OWL.imports, o)) # remove protege specific properties for p, o in rdf.predicate_objects(ont): prot = URIRef( 'http://protege.stanford.edu/plugins/owl/protege#') if p.startswith(prot): rdf.remove((ont, p, o)) # move remaining properties (dc:title etc.) of the owl:Ontology into # the skos:ConceptScheme replace_uri(rdf, ont, cs) return cs