or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

dataset-named-graphs.mdgraph-operations.mdindex.mdnamespace-management.mdparsers-serializers.mdrdf-containers-collections.mdrdf-terms.mdsparql-queries-updates.mdutilities-helpers.md

index.mddocs/

0

# RDFLib

1

2

A comprehensive pure Python library for working with Resource Description Framework (RDF) data. RDFLib provides parsers and serializers for multiple RDF formats, a Graph interface that can be backed by various Store implementations, a complete SPARQL 1.1 implementation, and tools for namespace management and RDF data transformation.

3

4

## Package Information

5

6

- **Package Name**: rdflib

7

- **Language**: Python

8

- **Installation**: `pip install rdflib`

9

10

## Core Imports

11

12

```python

13

import rdflib

14

```

15

16

Common patterns for working with RDF:

17

18

```python

19

from rdflib import Graph, URIRef, Literal, BNode

20

from rdflib.namespace import RDF, RDFS, OWL, XSD

21

```

22

23

For SPARQL queries:

24

25

```python

26

from rdflib import Graph

27

from rdflib.plugins.sparql import prepareQuery

28

```

29

30

## Basic Usage

31

32

```python

33

from rdflib import Graph, URIRef, Literal, Namespace

34

from rdflib.namespace import RDF, FOAF

35

36

# Create a new graph

37

g = Graph()

38

39

# Define a namespace

40

EX = Namespace("http://example.org/")

41

42

# Add some triples

43

g.add((EX.person1, RDF.type, FOAF.Person))

44

g.add((EX.person1, FOAF.name, Literal("John Doe")))

45

g.add((EX.person1, FOAF.age, Literal(30)))

46

47

# Parse RDF from a file or URL

48

g.parse("http://example.org/data.rdf")

49

50

# Query the graph with SPARQL

51

query = """

52

SELECT ?name ?age WHERE {

53

?person foaf:name ?name .

54

?person foaf:age ?age .

55

FILTER(?age > 25)

56

}

57

"""

58

results = g.query(query)

59

for row in results:

60

print(f"Name: {row.name}, Age: {row.age}")

61

62

# Serialize the graph

63

turtle_data = g.serialize(format='turtle')

64

print(turtle_data)

65

```

66

67

## Architecture

68

69

RDFLib uses a modular architecture with several key components:

70

71

- **Terms**: Core RDF data types (URIRef, BNode, Literal, Variable) representing RDF nodes

72

- **Graph**: Container for RDF triples with query, update, and serialization capabilities

73

- **Store**: Pluggable storage backends (Memory, Berkeley DB, SPARQL endpoints)

74

- **Plugins**: Extensible parsers, serializers, and query processors for different RDF formats

75

- **Namespace Management**: Tools for managing URI prefixes and vocabulary definitions

76

77

This design enables RDFLib to serve as both a simple RDF manipulation library and a foundation for complex semantic web applications.

78

79

## Capabilities

80

81

### RDF Terms and Nodes

82

83

Core RDF data types representing the fundamental building blocks of RDF: URIs, blank nodes, literals, and variables. These classes provide the foundation for all RDF operations.

84

85

```python { .api }

86

class URIRef:

87

def __init__(self, value: str): ...

88

def toPython(self): ...

89

def n3(self): ...

90

91

class BNode:

92

def __init__(self, value: str = None): ...

93

def skolemize(self, authority: str = None): ...

94

95

class Literal:

96

def __init__(self, lexical_or_value, lang: str = None, datatype: URIRef = None): ...

97

def toPython(self): ...

98

@property

99

def value(self): ...

100

@property

101

def datatype(self): ...

102

@property

103

def language(self): ...

104

```

105

106

[RDF Terms](./rdf-terms.md)

107

108

### Graph Operations

109

110

Primary interface for working with RDF data. Provides methods for adding, removing, and querying RDF triples, plus parsing and serialization capabilities across multiple formats.

111

112

```python { .api }

113

class Graph:

114

def __init__(self, store: str = 'default', identifier: Node = None): ...

115

def add(self, triple: Tuple[Node, Node, Node]): ...

116

def remove(self, triple: Tuple[Node, Node, Node]): ...

117

def parse(self, source, format: str = None, **kwargs): ...

118

def serialize(self, destination=None, format: str = 'xml', **kwargs): ...

119

def query(self, query_object, **kwargs): ...

120

def update(self, update_object, **kwargs): ...

121

def triples(self, triple: Tuple[Node, Node, Node]) -> Iterator: ...

122

```

123

124

[Graph Operations](./graph-operations.md)

125

126

### Dataset and Named Graphs

127

128

RDF 1.1 Dataset implementation for working with collections of named graphs. Provides methods for managing multiple graphs within a single container.

129

130

```python { .api }

131

class Dataset:

132

def __init__(self, store: str = 'default', default_union: bool = False): ...

133

def graph(self, identifier: Node = None) -> Graph: ...

134

def add_graph(self, g: Graph): ...

135

def remove_graph(self, g: Graph): ...

136

@property

137

def default_graph(self) -> Graph: ...

138

def named_graphs(self) -> Iterator[Graph]: ...

139

```

140

141

[Dataset and Named Graphs](./dataset-named-graphs.md)

142

143

### Namespace Management

144

145

Tools for managing URI namespaces and prefixes. Provides predefined vocabularies and utilities for creating custom namespaces.

146

147

```python { .api }

148

class Namespace:

149

def __init__(self, value: str): ...

150

def __getattr__(self, name: str) -> URIRef: ...

151

def __getitem__(self, key: str) -> URIRef: ...

152

153

class NamespaceManager:

154

def bind(self, prefix: str, namespace: Namespace): ...

155

def compute_qname(self, uri: URIRef) -> Tuple[str, URIRef, str]: ...

156

def normalizeUri(self, rdfTerm: URIRef) -> str: ...

157

```

158

159

[Namespace Management](./namespace-management.md)

160

161

### SPARQL Queries and Updates

162

163

Complete SPARQL 1.1 implementation supporting SELECT, CONSTRUCT, ASK, DESCRIBE queries and INSERT, DELETE, LOAD, CLEAR updates with extension mechanisms for custom functions.

164

165

```python { .api }

166

def prepareQuery(queryString: str, initNs: Dict[str, Namespace] = None): ...

167

168

class Result:

169

def __iter__(self): ...

170

def serialize(self, format: str = 'xml'): ...

171

@property

172

def type(self) -> str: ...

173

@property

174

def bindings(self) -> List[Dict]: ...

175

```

176

177

[SPARQL Queries and Updates](./sparql-queries-updates.md)

178

179

### RDF Containers and Collections

180

181

Support for RDF containers (Bag, Seq, Alt) and collections (rdf:List) providing ordered and unordered groupings of RDF resources.

182

183

```python { .api }

184

class Container:

185

def __init__(self, graph: Graph, uri: Node, seq: List = None, rtype: str = "Bag"): ...

186

def append(self, item: Node): ...

187

def __getitem__(self, index: int) -> Node: ...

188

189

class Collection:

190

def __init__(self, graph: Graph, uri: Node, seq: List = None): ...

191

def append(self, item: Node): ...

192

def clear(self): ...

193

```

194

195

[RDF Containers and Collections](./rdf-containers-collections.md)

196

197

### Parsers and Serializers

198

199

Pluggable parsers and serializers supporting RDF/XML, Turtle, N-Triples, N-Quads, TriG, JSON-LD, and other RDF formats through a unified interface.

200

201

```python { .api }

202

# Parsing

203

def parse(source, format: str = None, **kwargs): ...

204

205

# Serialization

206

def serialize(destination=None, format: str = 'xml', **kwargs): ...

207

208

# Plugin registration

209

def register(name: str, kind: str, module_path: str, class_name: str): ...

210

```

211

212

[Parsers and Serializers](./parsers-serializers.md)

213

214

### Utilities and Helpers

215

216

Utility functions for RDF data manipulation, format detection, term conversion, and graph comparison operations.

217

218

```python { .api }

219

def to_term(s) -> Node: ...

220

def from_n3(s: str) -> Node: ...

221

def first(seq): ...

222

def uniq(seq): ...

223

def guess_format(fpath: str) -> str: ...

224

def isomorphic(graph1: Graph, graph2: Graph) -> bool: ...

225

```

226

227

[Utilities and Helpers](./utilities-helpers.md)

228

229

## Global Configuration

230

231

```python { .api }

232

# Literal normalization setting

233

NORMALIZE_LITERALS: bool

234

235

# SPARQL literal comparison mode

236

DAWG_LITERAL_COLLATION: bool

237

```