utype - 基于 Python 类型注解的数据解析与类型安全保障库

utype 是一个基于 Python 类型注解的数据类型声明与解析库,能够在运行时根据声明对类与函数的参数进行解析转化,保障项目类型安全。支持函数参数类型声明解析、简洁约束和类型声明、多种逻辑条件组合类型等。

Check out utype - 基于 Python 类型注解的数据解析与类型安全保障库 on CurateClick

• Copy the embed code to showcase this product on your website

• Share on X to spread the word about this amazing tool

  • 项目地址:https://github.com/utilmeta/utype * 官网:https://utype.io/ * 中文文档:https://utype.io/zh/- 类别:Python- 项目标题:utype - 基于 Python 类型注解的数据解析与类型安全保障库- 项目描述:utype 是一个基于 Python 类型注解的数据类型声明与解析库,能够在运行时根据你的声明对类与函数的参数进行解析转化,保障项目的类型安全- 亮点:相比于pydantic 等库,支持了对函数参数类型声明的解析(包括生成器与异步函数),支持简洁的约束和类型声明,支持多种逻辑条件组合类型,支持丰富的解析选项与偏好配置- 示例代码:pythonimport utype@utype.parsedef login( username: str = utype.Param(regex='[0-9a-zA-Z]{3,20}'), password: str = utype.Param(min_length=6)): # 你可以直接开始编写逻辑了 return username, passwordprint(login('alice', 123456))('alice', '123456')try: login('@invalid', 123456)except utype.exc.ParseError as e: print(e) """ parse item: ['username'] failed: Constraint: <regex>: '[0-9a-zA-Z]{3,20}' violated """``````pythonfrom utype import Schema, Field, excfrom datetime import datetimeclass UserSchema(Schema): username: str = Field(regex='[0-9a-zA-Z]{3,20}') signup_time: datetime# 1. 正常输入data = {'username': 'bob', 'signup_time': '2022-10-11 10:11:12'}print(UserSchema(**data))#> UserSchema(username='bob', signup_time=datetime.datetime(2022, 10, 11, 10, 11, 12))# 2. 异常输入try: UserSchema(username='@invalid', signup_time='2022-10-11 10:11:12')except exc.ParseError as e: print(e) """ parse item: ['username'] failed: Constraint: <regex>: '[0-9a-zA-Z]{3,20}' violated """- 后续更新计划: * 支持生成 json-schema 格式的输入输出模板文档 * 完善解析错误的处理机制,包括错误处理钩子函数等 * 支持环境变量和配置文件的解析和管理 * 支持命令行参数的声明与解析 * 支持 Python 泛型,类型变量等更多类型注解语法 * 开发 Pycharm / VS Code 插件,支持对约束,逻辑类型和嵌套类型的 IDE 检测与提示

Latest Weekly Picks

pslscale.com favicon
Weekly Pick

Discover your PSL (Perceived Sexual Market Value) score with our AI-powered facial analysis

MILO

IPYNB to PDF Converter – Free & Secure

lejintian
www.reverie.im favicon
Weekly Pick

Dream It, Chat It, Create It

Louis
make.ink favicon
Weekly Pick

Your AI tattoo generator for pro-grade concepts

Leo Wade
sellfy.com favicon
Weekly Pick

A code-free online store builder to turn views into revenue. Sell digital products, subscriptions, and merch, without fees or hassle.

Maris
www.videotoblog.ai favicon
Weekly Pick

Convert videos into awesome blog posts.

Video To Blog
www.sellerpic.ai favicon
Weekly Pick

SellerPic is the all-in-one AI design platform for e-commerce

SellerPic
fastimage.ai favicon
Weekly Pick

Fast Image AI instantly transforms your photos into stunning styles like Ghibli, Sketch, and Pixar. Effortlessly control image elements and create amazing effects with just one click.

Fast AI Team