Skip to content

Instantly share code, notes, and snippets.

@manisnesan
Last active October 23, 2024 10:20
Show Gist options
  • Save manisnesan/cad8f7e17e5891d7136d3d55da3f0ff7 to your computer and use it in GitHub Desktop.
Save manisnesan/cad8f7e17e5891d7136d3d55da3f0ff7 to your computer and use it in GitHub Desktop.
Qna.yaml as Pydantic Data Model with YAML to Pydantic Conversions This script defines a Pydantic data model for a Q&A system based on a YAML structure. It includes classes for handling questions and answers, document chunks, and overall document metadata. The model supports validation for SHA1 commit hashes.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@manisnesan
Copy link
Author

Key Features:

  • Pydantic Models: Defines QuestionAnswer, DocChunkQnA, Doc, and ILabQnA classes for structured data.
  • YAML Conversion: Functions to convert Pydantic models to YAML and vice versa (pydantic_to_yaml and yaml_to_pydantic).
  • Validation: Uses field_validator to enforce constraints on fields, such as SHA1 format for commits.
  • Example Usage: Demonstrates how to create an instance of the model, serialize it to YAML, and deserialize it back to a Pydantic model.

Installation:

pip install pydantic rich pyyaml

Usage:

  • Use the provided functions to read from and write to YAML files.
  • Validate the structure and content of the data model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment