Git Product home page Git Product logo

pydantic-example's Introduction

Pydantic Examples

FullStackWithLawrence
Python Pydantic JSON
Release Notes GHA pushMain Status AGPL License hack.d Lawrence McDaniel

Pydantic is the most widely used data validation library for Python. This repo demonstrates three popular use cases for Pydantic:

  1. Validation. Pydantic ensures that the data your class instances receive matches the expected format/type. It validates the input data types and structures, and raises exceptions when the data is invalid. See the example Python class in this repo.

  2. Data Parsing and Serialization. See the JSON validator in this repo for an example of how to validate a JSON string against a schema. Pydantic can parse complex data types, like JSON, into Python data structures. It can also serialize Python objects back into JSON.

  3. Exception Handling. See the custom exceptions in this repo which demonstrate how you catch Pydantic exceptions, analyze them, and then raise your own custom exceptions.

Installation

git clone https://github.com/FullStackWithLawrence/pydantic-example.git
cd pydantic-example
make init
source venv/bin/activate

Usage

# command-line help
python3 -m grader.batch -h

# example usage
python3 -m grader.batch 'path/to/homework/json/files/'

About This Example

The code in the repo implements an automated homework grader that I used for an online course that I taught. It analyzes a text file that is supposed to contain a valid JSON object, and then returns a grade based on how closely the JSON object matches the intended schema.

Rubric

Rubric values are expressed as floats between 0 and 1.00, and can be overridden with environment variables.

AG_INCORRECT_RESPONSE_TYPE_PENALTY_PCT=0.10
AG_INCORRECT_RESPONSE_VALUE_PENALTY_PCT=0.15
AG_RESPONSE_FAILED_PENALTY_PCT=0.20
AG_INVALID_RESPONSE_STRUCTURE_PENALTY_PCT=0.30
AG_INVALID_JSON_RESPONSE_PENALTY_PCT=0.50

Expected output

% done! Graded 10 assignments. Output files are in path/to/homework/json/files/out
{
  "grade": 100,
  "message": "Great job!",
  "message_type": "Success"
}
{
    "grade": 80,
    "message": "The assignment's statusCode must be 200. received: 403",
    "message_type": "ResponseFailedError"
}
{
    "grade": 90,
    "message": "The assignment's statusCode must be an integer. received: <class 'str'>",
    "message_type": "IncorrectResponseTypeError"
}
{
    "grade": 70,
    "message": "The assignment is missing one or more required keys. missing: {'type', 'example', 'additional_kwargs'}",
    "message_type": "InvalidResponseStructureError"
}
{
    "grade": 70,
    "message": "The messages list must contain at least two elements. messages: [{'content': \"Oh, how delightful. I can't think of anything I'd rather do than interact with a bunch of YouTube viewers. Just kidding, I'd rather be doing literally anything else. But go ahead, introduce me to your lovely audience. I'm sure they'll be absolutely thrilled to meet me.\", 'additional_kwargs': {}, 'type': 'ai', 'example': False}]",
    "message_type": "InvalidResponseStructureError"
}
{
  "grade": 70,
  "message": "All elements in the messages list must be dictionaries. messages: ['bad', 'data']",
  "message_type": "InvalidResponseStructureError"
}
{
  "grade": 70,
  "message": "The request_meta_data key lambda_langchain must exist. request_meta_data: {}",
  "message_type": "InvalidResponseStructureError"
}

Contributing

This project uses a mostly automated pull request and unit testing process. See the resources in .github for additional details. You additionally should ensure that pre-commit is installed and working correctly on your dev machine by running the following command from the root of the repo.

pre-commit run --all-files

Pull requests should pass these tests before being submitted:

make test

Developer setup

git clone https://github.com/lpm0073/automatic-grader.git
cd automatic-grader
make init
make activate

pydantic-example's People

Contributors

actions-user avatar dependabot[bot] avatar github-actions[bot] avatar lpm0073 avatar mergify[bot] avatar

Watchers

 avatar

pydantic-example's Issues

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Missing package.json file.

A package.json file at the root of your project is required to release on npm.

Please follow the npm guideline to create a valid package.json file.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Missing name property in package.json.

The package.json's name property is required in order to publish a package to the npm registry.

Please make sure to add a valid name for your package in your package.json.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.