src/rdflib/__init__.py
1"""A pure Python package providing the core RDF constructs.
2
3The packages is intended to provide the core RDF types and interfaces
4for working with RDF. The package defines a plugin interface for
5parsers, stores, and serializers that other packages can use to
6implement parsers, stores, and serializers that will plug into the
7rdflib package.
8
9The primary interface `rdflib` exposes to work with RDF is
10[`rdflib.graph.Graph`][rdflib.graph.Graph].
11
12A tiny example:
13
14```python
15>>> from rdflib import Graph, URIRef, Literal
16
17>>> g = Graph()
18>>> result = g.parse("http://www.w3.org/2000/10/swap/test/meet/blue.rdf")
19
20>>> print("graph has %s statements." % len(g))
21graph has 4 statements.
22>>>
23>>> for s, p, o in g:
24... if (s, p, o) not in g:
25... raise Exception("It better be!")
26
27>>> s = g.serialize(format='nt')
28>>>
29>>> sorted(g) == [
30... (URIRef("http://meetings.example.com/cal#m1"),
31... URIRef("http://www.example.org/meeting_organization#homePage"),
32... URIRef("http://meetings.example.com/m1/hp")),
33... (URIRef("http://www.example.org/people#fred"),
34... URIRef("http://www.example.org/meeting_organization#attending"),
35... URIRef("http://meetings.example.com/cal#m1")),
36... (URIRef("http://www.example.org/people#fred"),
37... URIRef("http://www.example.org/personal_details#GivenName"),
38... Literal("Fred")),
39... (URIRef("http://www.example.org/people#fred"),
40... URIRef("http://www.example.org/personal_details#hasEmail"),
41... URIRef("mailto:fred@example.com"))
42... ]
43True
44
45```
46"""
47
48import logging
49import sys
50from importlib import metadata
51
52_DISTRIBUTION_METADATA = metadata.metadata("rdflib")
53
54__docformat__ = "restructuredtext en"
55
56__version__: str = _DISTRIBUTION_METADATA["Version"]
57__date__ = "2026-02-13"
58
59__all__ = [
60 "URIRef",
61 "BNode",
62 "IdentifiedNode",
63 "Literal",
64 "Node",
65 "Variable",
66 "Namespace",
67 "Dataset",
68 "Graph",
69 "ConjunctiveGraph",
70 "BRICK",
71 "CSVW",
72 "DC",
73 "DCAT",
74 "DCMITYPE",
75 "DCTERMS",
76 "DOAP",
77 "FOAF",
78 "ODRL2",
79 "ORG",
80 "OWL",
81 "PROF",
82 "PROV",
83 "QB",
84 "RDF",
85 "RDFS",
86 "SDO",
87 "SH",
88 "SKOS",
89 "SOSA",
90 "SSN",
91 "TIME",
92 "VANN",
93 "VOID",
94 "XMLNS",
95 "XSD",
96 "util",
97 "plugin",
98 "query",
99 "NORMALIZE_LITERALS",
100]
101
102logger = logging.getLogger(__name__)
103
104try:
105 import __main__
106
107 if (
108 not hasattr(__main__, "__file__")
109 and sys.stdout is not None
110 and hasattr(sys.stderr, "isatty")
111 and sys.stderr.isatty()
112 ):
113 # show log messages in interactive mode
114 logger.setLevel(logging.INFO)
115 logger.addHandler(logging.StreamHandler())
116 del __main__
117except ImportError:
118 # Main already imported from elsewhere
119 import warnings
120
121 warnings.warn("__main__ already imported", ImportWarning)
122 del warnings
123
124del sys
125
126
127NORMALIZE_LITERALS = True
128"""
129If True - Literals lexical forms are normalized when created.
130I.e. the lexical forms is parsed according to data-type, then the
131stored lexical form is the re-serialized value that was parsed.
132
133Illegal values for a datatype are simply kept. The normalized keyword
134for Literal.__new__ can override this.
135
136For example:
137
138```python
139>>> from rdflib import Literal,XSD
140>>> Literal("01", datatype=XSD.int)
141rdflib.term.Literal("1", datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#integer"))
142
143```
144
145This flag may be changed at any time, but will only affect literals
146created after that time, previously created literals will remain
147(un)normalized.
148
149"""
150
151
152DAWG_LITERAL_COLLATION = False
153"""DAWG_LITERAL_COLLATION determines how literals are ordered or compared
154to each other.
155
156In SPARQL, applying the >,<,>=,<= operators to literals of
157incompatible data-types is an error, i.e:
158
159`Literal(2)>Literal('cake')` is neither true nor false, but an error.
160
161This is a problem in PY3, where lists of Literals of incompatible
162types can no longer be sorted.
163
164Setting this flag to True gives you strict DAWG/SPARQL compliance,
165setting it to False will order Literals with incompatible datatypes by
166datatype URI
167
168In particular, this determines how the rich comparison operators for
169Literal work, eq, `__neq__`, `__lt__`, etc.
170"""
171
172
173from rdflib.graph import ConjunctiveGraph, Dataset, Graph
174from rdflib.namespace import (
175 BRICK,
176 CSVW,
177 DC,
178 DCAT,
179 DCMITYPE,
180 DCTERMS,
181 DOAP,
182 FOAF,
183 ODRL2,
184 ORG,
185 OWL,
186 PROF,
187 PROV,
188 QB,
189 RDF,
190 RDFS,
191 SDO,
192 SH,
193 SKOS,
194 SOSA,
195 SSN,
196 TIME,
197 VANN,
198 VOID,
199 XMLNS,
200 XSD,
201 Namespace,
202)
203from rdflib.term import BNode, IdentifiedNode, Literal, Node, URIRef, Variable
204
205from rdflib import plugin, query, util # isort:skip
206from rdflib.container import * # isort:skip # noqa: F403