util
Some utility functions.
Miscellaneous utilities
- list2set
- first
- uniq
- more_than
Term characterisation and generation
- to_term
- from_n3
Date/time utilities
- date_time
- parse_date_time
Functions:
-
date_time–http://www.w3.org/TR/NOTE-datetime ex: 1997-07-16T19:20:30Z
-
find_roots–Find the roots in some sort of transitive hierarchy.
-
first–return the first element in a python sequence
-
from_n3–Creates the Identifier corresponding to the given n3 string.
-
get_tree–Return a nested list/tuple structure representing the tree
-
guess_format–Guess RDF serialization based on file suffix. Uses
-
list2set–Return a new list without duplicates.
-
more_than–Returns 1 if sequence has more items than number and 0 if not.
-
parse_date_time–always returns seconds in UTC
-
to_term–Creates and returns an Identifier of type corresponding
-
uniq–removes duplicate strings from the sequence.
FORMAT_MIMETYPE_MAP
module-attribute
FORMAT_MIMETYPE_MAP = {'xml': ['application/rdf+xml'], 'n3': ['text/n3'], 'turtle': ['text/turtle'], 'nt': ['application/n-triples'], 'trix': ['application/trix'], 'rdfa': ['text/html', 'application/xhtml+xml'], 'nquads': ['application/n-quads'], 'trig': ['application/trig'], 'json-ld': ['application/ld+json']}
RESPONSE_TABLE_FORMAT_MIMETYPE_MAP
module-attribute
RESPONSE_TABLE_FORMAT_MIMETYPE_MAP = {'xml': ['application/sparql-results+xml'], 'json': ['application/sparql-results+json'], 'csv': ['text/csv'], 'tsv': ['text/tab-separated-values']}
SUFFIX_FORMAT_MAP
module-attribute
SUFFIX_FORMAT_MAP = {'xml': 'xml', 'rdf': 'xml', 'owl': 'xml', 'n3': 'n3', 'ttl': 'turtle', 'nt': 'nt', 'trix': 'trix', 'xhtml': 'rdfa', 'html': 'rdfa', 'svg': 'rdfa', 'nq': 'nquads', 'nquads': 'nquads', 'trig': 'trig', 'json': 'json-ld', 'jsonld': 'json-ld', 'json-ld': 'json-ld'}
__all__
module-attribute
__all__ = ['list2set', 'first', 'uniq', 'more_than', 'to_term', 'from_n3', 'date_time', 'parse_date_time', 'guess_format', 'find_roots', 'get_tree', '_coalesce', '_iri2uri']
date_time
http://www.w3.org/TR/NOTE-datetime ex: 1997-07-16T19:20:30Z
>>> date_time(1126482850)
'2005-09-11T23:54:10Z'
@@ this will change depending on where it is run
#>>> date_time(1126482850, local_time_zone=True)
#'2005-09-11T19:54:10-04:00'
>>> date_time(1)
'1970-01-01T00:00:01Z'
>>> date_time(0)
'1970-01-01T00:00:00Z'
Source code in rdflib/util.py
find_roots
Find the roots in some sort of transitive hierarchy.
find_roots(graph, rdflib.RDFS.subClassOf) will return a set of all roots of the sub-class hierarchy
Assumes triple of the form (child, prop, parent), i.e. the direction of
RDFS.subClassOf or SKOS.broader
Source code in rdflib/util.py
first
return the first element in a python sequence for graphs, use graph.value instead
from_n3
from_n3(s: str, default: Optional[str] = None, backend: Optional[str] = None, nsm: Optional[NamespaceManager] = None) -> Optional[Union[Node, str]]
Creates the Identifier corresponding to the given n3 string.
>>> from rdflib.term import URIRef, Literal
>>> from rdflib.namespace import NamespaceManager
>>> from_n3('<http://ex.com/foo>') == URIRef('http://ex.com/foo')
True
>>> from_n3('"foo"@de') == Literal('foo', lang='de')
True
>>> from_n3('"""multi\nline\nstring"""@en') == Literal(
... 'multi\nline\nstring', lang='en')
True
>>> from_n3('42') == Literal(42)
True
>>> from_n3(Literal(42).n3()) == Literal(42)
True
>>> from_n3('"42"^^xsd:integer') == Literal(42)
True
>>> from rdflib import RDFS
>>> from_n3('rdfs:label') == RDFS['label']
True
>>> nsm = NamespaceManager(rdflib.graph.Graph())
>>> nsm.bind('dbpedia', 'http://dbpedia.org/resource/')
>>> berlin = URIRef('http://dbpedia.org/resource/Berlin')
>>> from_n3('dbpedia:Berlin', nsm=nsm) == berlin
True
Source code in rdflib/util.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
get_tree
get_tree(graph: Graph, root: Node, prop: URIRef, mapper: Callable[[Node], Node] = lambda x: x, sortkey: Optional[Callable[[Any], Any]] = None, done: Optional[Set[Node]] = None, dir: str = 'down') -> Optional[Tuple[Node, List[Any]]]
Return a nested list/tuple structure representing the tree built by the transitive property given, starting from the root given
i.e.
will return the structure for the subClassTree below person.
dir=’down’ assumes triple of the form (child, prop, parent), i.e. the direction of RDFS.subClassOf or SKOS.broader Any other dir traverses in the other direction
Source code in rdflib/util.py
guess_format
Guess RDF serialization based on file suffix. Uses
SUFFIX_FORMAT_MAP unless fmap is provided.
Example
>>> guess_format('path/to/file.rdf')
'xml'
>>> guess_format('path/to/file.owl')
'xml'
>>> guess_format('path/to/file.ttl')
'turtle'
>>> guess_format('path/to/file.json')
'json-ld'
>>> guess_format('path/to/file.xhtml')
'rdfa'
>>> guess_format('path/to/file.svg')
'rdfa'
>>> guess_format('path/to/file.xhtml', {'xhtml': 'grddl'})
'grddl'
This also works with just the suffixes, with or without leading dot, and regardless of letter case:
Source code in rdflib/util.py
list2set
Return a new list without duplicates. Preserves the order, unlike set(seq)
Source code in rdflib/util.py
more_than
Returns 1 if sequence has more items than number and 0 if not.
parse_date_time
always returns seconds in UTC
# tests are written like this to make any errors easier to understand
>>> parse_date_time('2005-09-11T23:54:10Z') - 1126482850.0
0.0
>>> parse_date_time('2005-09-11T16:54:10-07:00') - 1126482850.0
0.0
>>> parse_date_time('1970-01-01T00:00:01Z') - 1.0
0.0
>>> parse_date_time('1970-01-01T00:00:00Z') - 0.0
0.0
>>> parse_date_time("2005-09-05T10:42:00") - 1125916920.0
0.0
Source code in rdflib/util.py
to_term
to_term(s: Optional[str], default: Optional[Identifier] = None) -> Optional[Identifier]
Creates and returns an Identifier of type corresponding
to the pattern of the given positional argument string s:
’’ returns the default keyword argument value or None
‘’ returns URIRef(s) (i.e. without angle brackets)
‘“s”’ returns Literal(s) (i.e. without doublequotes)
’_s’ returns BNode(s) (i.e. without leading underscore)