### Creating and Serializing a PydanticRDF Instance (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/quickstart.md This code demonstrates creating an instance of a previously defined PydanticRDF model, including setting the required `uri`. It then shows how to serialize the model instance into an `rdflib.Graph` using the `model_dump_rdf()` method and print the resulting graph in Turtle format. ```python # Create an instance person = Person( uri=SDO.Person_1, # URI is a required field for all RDF models name="John Doe", email="john.doe@example.com", job_title="Software Engineer" ) # Serialize to RDF graph graph = person.model_dump_rdf() # Print the graph as Turtle format print(graph.serialize(format="turtle")) ``` -------------------------------- ### Install All Dependencies (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Installs all project dependencies, including development dependencies, using the uv package manager. ```Shell uv sync --all-groups ``` -------------------------------- ### Serve PydanticRDF Documentation Locally (mkdocs) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/README.md Starts a local development server to preview the documentation in a web browser, automatically rebuilding on changes. ```bash mkdocs serve ``` -------------------------------- ### Installing pydantic-rdf via pip Source: https://github.com/omegaice/pydantic-rdf/blob/master/README.md Provides the command to install the pydantic-rdf library using the pip package manager. ```Bash pip install pydantic-rdf ``` -------------------------------- ### Install Documentation Dependencies (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/README.md Installs the necessary Python packages required to build the documentation using the uv package manager and the 'docs' dependency group. ```bash uv sync --group docs ``` -------------------------------- ### Install Development Dependencies (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Installs only the development dependencies for the project using the uv package manager. ```Shell uv sync --group dev ``` -------------------------------- ### Handling Nested PydanticRDF Models (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/quickstart.md This example shows how to define models that contain other PydanticRDF models as fields, demonstrating support for nested structures. It includes defining both the nested (`PostalAddress`) and parent (`PersonWithAddress`) models, creating instances, and serializing the parent model to RDF. ```python class PostalAddress(BaseRdfModel): rdf_type = SDO.PostalAddress _rdf_namespace = SDO streetAddress: str addressLocality: str class PersonWithAddress(BaseRdfModel): rdf_type = SDO.Person _rdf_namespace = SDO name: str address: PostalAddress # Create nested models address = PostalAddress(uri=SDO.PostalAddress_1, streetAddress="123 Main St", addressLocality="Springfield") person = PersonWithAddress(uri=SDO.Person_2, name="John Doe", address=address) # Serialize to RDF graph = person.model_dump_rdf() ``` -------------------------------- ### Defining a Basic PydanticRDF Model (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/quickstart.md This snippet shows how to define a basic PydanticRDF model by inheriting from `BaseRdfModel`. It demonstrates setting the RDF type (`rdf_type`), the default namespace (`_rdf_namespace`), and defining fields, including using `Annotated` and `WithPredicate` for custom predicates. ```python from rdflib import SDO from pydantic_rdf import BaseRdfModel, WithPredicate from pydantic import Annotated # Define a model using Schema.org types class Person(BaseRdfModel): # RDF type for this model (maps to rdf:type) rdf_type = SDO.Person # Default namespace for properties _rdf_namespace = SDO # Model fields name: str email: str job_title: Annotated[str, WithPredicate(SDO.jobTitle)] # Custom predicate ``` -------------------------------- ### Deserializing RDF into a PydanticRDF Instance (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/quickstart.md This snippet illustrates how to load a PydanticRDF model instance from an existing `rdflib.Graph`. It uses the `parse_graph` class method, providing the graph and the URI of the specific instance to be loaded, and includes assertions to verify that the data was correctly deserialized. ```python # Parse an instance from the graph loaded_person = Person.parse_graph(graph, SDO.Person_1) # Access attributes assert loaded_person.name == "John Doe" assert loaded_person.email == "john.doe@example.com" assert loaded_person.job_title == "Software Engineer" ``` -------------------------------- ### Generating JSON Schema for PydanticRDF Models (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/quickstart.md This code shows how to generate a JSON schema for a PydanticRDF model using Pydantic's `TypeAdapter`. It highlights that the `uri` field, which is internally an `rdflib.URIRef`, is correctly represented in the schema as a string with the 'uri' format. ```python from pydantic import TypeAdapter # Define your model as before class Person(BaseRdfModel): rdf_type = SDO.Person _rdf_namespace = SDO name: str email: str # Generate JSON schema schema = TypeAdapter(Person).json_schema() ``` -------------------------------- ### Define and Use PydanticRDF Model (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/index.md This example demonstrates defining a Pydantic model that maps to an RDF type using `BaseRdfModel` and `WithPredicate`. It shows how to create an instance, serialize it to an RDF graph, and deserialize it back into a model instance. ```Python from rdflib import SDO from pydantic_rdf import BaseRdfModel, WithPredicate from typing import Annotated # Define a model using Schema.org types class Person(BaseRdfModel): rdf_type = SDO.Person _rdf_namespace = SDO name: str email: str job_title: Annotated[str, WithPredicate(SDO.jobTitle)] # Create an instance person = Person( uri=SDO.Person_1, name="John Doe", email="john.doe@example.com", job_title="Software Engineer" ) # Serialize to RDF graph = person.model_dump_rdf() # Deserialize from RDF loaded_person = Person.parse_graph(graph, SDO.Person_1) ``` -------------------------------- ### Handling List Fields in PydanticRDF Models (Python) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/quickstart.md This snippet demonstrates how PydanticRDF handles model fields defined as lists of primitive types, such as `list[str]`. It shows defining a model with a list field and creating an instance, explaining that serialization will generate multiple RDF triples with the same predicate for each item in the list. ```python class BlogPosting(BaseRdfModel): rdf_type = SDO.BlogPosting _rdf_namespace = SDO headline: str keywords: list[str] # Will create multiple triples with the same predicate # Create with a list post = BlogPosting( uri=SDO.BlogPosting_1, headline="PydanticRDF Introduction", keywords=["RDF", "Pydantic", "Python"] ) # Serialize to RDF graph = post.model_dump_rdf() ``` -------------------------------- ### Build PydanticRDF Documentation (mkdocs) Source: https://github.com/omegaice/pydantic-rdf/blob/master/docs/README.md Executes the MkDocs build command to generate the static HTML documentation site from the Markdown source files. ```bash mkdocs build ``` -------------------------------- ### Format Code with Ruff (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Formats the project code according to the configured style using ruff, run via uv. ```Shell uv run ruff format . ``` -------------------------------- ### Add Project Dependency (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Adds a new package as a regular project dependency using uv. ```Shell uv add ``` -------------------------------- ### Update Dependencies (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Updates all project dependencies to their latest compatible versions using uv. ```Shell uv sync --upgrade ``` -------------------------------- ### Lint Code with Ruff (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Checks the project code for linting issues and attempts to automatically fix them using ruff, run via uv. ```Shell uv run ruff check --fix . ``` -------------------------------- ### Run All Pytest Tests (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Executes all tests in the project using pytest, orchestrated by the uv runner. ```Shell uv run pytest ``` -------------------------------- ### Add Development Dependency (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Adds a new package specifically as a development dependency using uv. ```Shell uv add --dev ``` -------------------------------- ### Run Pytest Tests Verbose (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Executes all tests with verbose output using pytest and the uv runner. ```Shell uv run pytest -v ``` -------------------------------- ### Type Check Code with Mypy (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Performs static type checking on the source code using mypy, run via uv. ```Shell uv run mypy src/ ``` -------------------------------- ### Defining, Serializing, and Deserializing PydanticRDF Model Source: https://github.com/omegaice/pydantic-rdf/blob/master/README.md Demonstrates how to define a Pydantic model that maps to RDF, create an instance, serialize it to an RDF graph using RDFLib, and then deserialize the graph back into a Pydantic model instance. It shows the use of BaseRdfModel, WithPredicate, and namespace handling. ```Python from rdflib import Namespace from pydantic_rdf import BaseRdfModel, WithPredicate from typing import Annotated # Define a model EX = Namespace("http://example.org/") class Person(BaseRdfModel): rdf_type = EX.Person _rdf_namespace = EX name: str age: int email: Annotated[str, WithPredicate(EX.emailAddress)] # Create and serialize person = Person(uri=EX.person1, name="John Doe", age=30, email="john@example.com") graph = person.model_dump_rdf() # The resulting RDF graph looks like this: # @prefix ex: . # @prefix rdf: . # # ex:person1 a ex:Person ; # ex:age 30 ; # ex:emailAddress "john@example.com" ; # ex:name "John Doe" . # Deserialize loaded_person = Person.parse_graph(graph, EX.person1) ``` -------------------------------- ### Remove Dependency (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Removes an existing package dependency from the project using uv. ```Shell uv remove ``` -------------------------------- ### Run Single Pytest Test (uv) Source: https://github.com/omegaice/pydantic-rdf/blob/master/CLAUDE.md Runs a specific test function within a specified test file using pytest via the uv runner. ```Shell uv run pytest tests/test_file.py::test_function ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.