Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 154ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
p

pydantic

Recently created Least recently created Recently updated Least recently updated

timmy / 使用 pydantic 進行數據驗證與模型定義

Last active 6 hours ago data-modeling data-validation pydantic python schema

pydantic 提供基於 Python 類別的數據驗證與解析功能,適用於 API 請求驗證、資料模型定義、設定管理 等場景。

0 0 0 3
1 from pydantic import BaseModel, Field, ValidationError
2
3 class User(BaseModel):
4 name: str = Field(..., title="使用者名稱", min_length=2)
5 age: int = Field(..., gt=0, le=150, description="年齡必須介於 1 到 150 之間")
6 email: str
7
8 # 測試資料
9 try:
10 user = User(name="Tim", age=30, email="tim@example.com")

timmy / 使用 Streamlit 與 Pydantic 建立使用者資訊展示

Last active 6 hours ago data-validation pretty-errors pydantic python streamlit user-model web-dashboard

這段程式碼使用 pydantic 來定義使用者模型,並且透過 Streamlit 在網頁上顯示使用者資訊,同時 pretty_errors 提供美觀的錯誤訊息格式。

0 0 0 1
1 from datetime import date
2
3 import pretty_errors
4 import streamlit as st
5 from pydantic import BaseModel, Field
6
7
8 class User(BaseModel):
9 id: int
10 name: str