CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.

Pending
Overview
Eval results
Files

rdf-terms.mddocs/

RDF Terms

Core RDF data types representing the fundamental building blocks of RDF graphs. These classes provide the foundation for creating, manipulating, and serializing RDF data.

Capabilities

URIRef - URI References

Represents URI/IRI references in RDF graphs. URIRefs identify resources and properties in RDF data.

# Type imports for annotations
from typing import Optional, Any, Union
from rdflib.term import URIRef

class URIRef:
    def __new__(cls, value: str, base: Optional[str] = None) -> 'URIRef':
        """
        Create a URI reference.
        
        Parameters:
        - value: The URI string
        """
    
    def toPython(self):
        """
        Return the URI as a Python string.
        
        Returns:
        str: The URI string
        """
    
    def n3(self, namespace_manager=None) -> str:
        """
        Return N3 representation.
        
        Parameters:
        - namespace_manager: Optional namespace manager for prefixed notation
        
        Returns:
        str: N3 formatted string
        """
    
    def defrag(self) -> 'URIRef':
        """
        Remove fragment identifier from URI.
        
        Returns:
        URIRef: URI without fragment
        """
    
    def __str__(self) -> str:
        """Return string representation."""
    
    def __hash__(self) -> int:
        """Return hash value."""
    
    def de_skolemize(self) -> 'BNode':
        """
        Convert skolemized URI back to blank node.
        
        Returns:
        BNode: Corresponding blank node
        """

BNode - Blank Nodes

Represents blank nodes (anonymous resources) in RDF graphs. Blank nodes are local identifiers within a graph.

class BNode:
    def __new__(cls, value: Optional[str] = None) -> 'BNode':
        """
        Create a blank node.
        
        Parameters:
        - value: Optional identifier string; auto-generated if None
        """
    
    def toPython(self):
        """
        Return the blank node identifier.
        
        Returns:
        str: The blank node identifier
        """
    
    def n3(self, namespace_manager=None) -> str:
        """
        Return N3 representation.
        
        Returns:
        str: N3 formatted blank node
        """
    
    def skolemize(self, authority: Optional[str] = None, basepath: Optional[str] = None) -> URIRef:
        """
        Convert blank node to skolem IRI.
        
        Parameters:
        - authority: Authority for the skolem IRI
        
        Returns:
        URIRef: Skolemized URI
        """

Literal - Literal Values

Represents literal values in RDF graphs with optional datatype and language tags.

class Literal:
    def __new__(cls, lexical_or_value: Any, lang: Optional[str] = None, datatype: Optional[str] = None, normalize: Optional[bool] = None) -> 'Literal':
        """
        Create a literal.
        
        Parameters:
        - lexical_or_value: The literal value or lexical form
        - lang: Language tag (e.g., 'en', 'fr')
        - datatype: Datatype URI (e.g., XSD.int, XSD.string)
        - normalize: Whether to normalize the literal
        """
    
    def toPython(self):
        """
        Convert literal to appropriate Python type.
        
        Returns:
        Python object corresponding to the datatype
        """
    
    def n3(self, namespace_manager=None) -> str:
        """
        Return N3 representation.
        
        Returns:
        str: N3 formatted literal
        """
    
    @property
    def value(self):
        """
        Get the Python value of the literal.
        
        Returns:
        Python object
        """
    
    @property  
    def datatype(self) -> Optional[URIRef]:
        """
        Get the datatype URI.
        
        Returns:
        URIRef: Datatype URI or None
        """
    
    @property
    def language(self) -> Optional[str]:
        """
        Get the language tag.
        
        Returns:
        str: Language tag or None
        """
    
    def __str__(self) -> str:
        """Return string representation."""
    
    def __add__(self, other):
        """Add literals or convert to Python types for addition."""
    
    def __sub__(self, other):
        """Subtract literals or convert to Python types for subtraction."""
    
    def __lt__(self, other) -> bool:
        """Less than comparison."""
    
    def __le__(self, other) -> bool:
        """Less than or equal comparison."""
    
    def __gt__(self, other) -> bool:
        """Greater than comparison."""
    
    def __ge__(self, other) -> bool:
        """Greater than or equal comparison."""

Variable - SPARQL Variables

Represents variables in SPARQL queries and N3 formulas.

class Variable:
    def __init__(self, value: str):
        """
        Create a variable.
        
        Parameters:
        - value: Variable name (without ? or $ prefix)
        """
    
    def n3(self, namespace_manager=None) -> str:
        """
        Return N3 representation.
        
        Returns:
        str: N3 formatted variable (with ? prefix)
        """
    
    def toPython(self):
        """
        Return the variable name.
        
        Returns:
        str: Variable name
        """

Variable - SPARQL Variables

Represents variables in SPARQL queries and expressions. Variables are identified by names starting with '?' or '$'.

class Variable:
    def __new__(cls, name: str) -> 'Variable':
        """
        Create a SPARQL variable.
        
        Parameters:
        - name: Variable name (without ? or $ prefix)
        """
    
    def toPython(self) -> str:
        """
        Return the variable name.
        
        Returns:
        str: Variable name
        """
    
    def n3(self, namespace_manager=None) -> str:
        """
        Return N3 representation with ? prefix.
        
        Returns:
        str: N3 formatted variable
        """
    
    @property
    def name(self) -> str:
        """
        Get the variable name.
        
        Returns:
        str: Variable name without prefix
        """

Node - Base Class

Abstract base class for all RDF nodes.

class Node:
    """Abstract base class for RDF nodes."""
    
    def n3(self, namespace_manager=None) -> str:
        """Return N3 representation."""
    
    def toPython(self):
        """Convert to appropriate Python representation."""

IdentifiedNode - Base for Identifiable Nodes

Abstract base class for nodes that can be identified (URIRef and BNode).

class IdentifiedNode(Node):
    """Abstract base class for identifiable nodes (URIRef, BNode)."""

Utility Functions

bind

Utility for namespace binding in term contexts.

def bind(namespace: str, term: str) -> URIRef:
    """
    Bind a namespace to create a URIRef.
    
    Parameters:
    - namespace: Namespace URI
    - term: Local term name
    
    Returns:
    URIRef: Combined URI reference
    """

_is_valid_uri

URI validation utility.

def _is_valid_uri(uri: str) -> bool:
    """
    Check if a string is a valid URI.
    
    Parameters:
    - uri: URI string to validate
    
    Returns:
    bool: True if valid URI
    """

Usage Examples

Creating Terms

from rdflib import URIRef, BNode, Literal
from rdflib.namespace import XSD

# Create a URI reference
person_uri = URIRef("http://example.org/person/1")

# Create a blank node
blank = BNode()

# Create literals with different datatypes
name = Literal("John Doe")  # string literal
age = Literal(30, datatype=XSD.integer)
height = Literal(5.9, datatype=XSD.decimal)
is_active = Literal(True, datatype=XSD.boolean)

# Create a literal with language tag
description = Literal("Une personne", lang="fr")

Working with Literals

from rdflib import Literal
from rdflib.namespace import XSD

# Create typed literals
num1 = Literal(10, datatype=XSD.integer)
num2 = Literal(20, datatype=XSD.integer)

# Convert to Python values
python_val = num1.toPython()  # Returns 10 as int

# Perform operations
result = num1 + num2  # Returns Literal(30)

# Access properties
print(num1.datatype)  # XSD.integer
print(description.language)  # 'fr'

Blank Node Operations

from rdflib import BNode

# Create blank nodes
blank1 = BNode()
blank2 = BNode("custom_id")

# Skolemize blank node
skolem_uri = blank1.skolemize("http://example.org")
print(skolem_uri)  # URIRef with skolem authority

# De-skolemize back to blank node
back_to_blank = skolem_uri.de_skolemize()

Install with Tessl CLI

npx tessl i tessl/pypi-rdflib

docs

dataset-named-graphs.md

graph-operations.md

index.md

namespace-management.md

parsers-serializers.md

rdf-containers-collections.md

rdf-terms.md

sparql-queries-updates.md

utilities-helpers.md

tile.json