Python 工具集合
Project description
一. common.utils.models.LooseModel
描述: 别名基类
1. 解决问题:
在如下结构中
from pydantic import BaseModel
class User(BaseModel):
user_id: int
即使定义了alias_generator, 也只是model的user_id的别名,但是无法改变初始化时传入的参数名称,比如有些api可能有些接口传递了userId, 有些接口传递的是userID,这种同一个平台的接口返回的同一字段大小写不一致的情况很难考虑周全
2. 设计思路
设计了一个LooseModel,通过model_config的alias_generator 把user_id转换成userid,在LooseModel的from_any_dict中将输入的字典所有的键小写去掉下划线,不管是userId还是userID 或者是user_ID, 都会变成userid,这样就可以模糊匹配到model中
3. 使用方式
from wmain.common.models import LooseModel
class Person(LooseModel):
age: int
s_name: str
s_id: int
class Teacher(Person):
students: list[Person]
dic = {
"age": 18,
"s_name": "张三",
"S_Id": 1,
"students": [
{
"age": 18,
"SName": "张三",
"SID": 1
}
]
}
model = Teacher.from_any_dict(dic)
print(model)
输出
age=18 s_name='张三' s_id=1 students=[Person(age=18, s_name='张三', s_id=1)]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wmain-2.0.16.tar.gz.
File metadata
- Download URL: wmain-2.0.16.tar.gz
- Upload date:
- Size: 31.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.0 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e07af859e95487b8dedd40a98105429dc5cb2d43e53bb2e59d15e1b783cdb2b4
|
|
| MD5 |
682e709da9d895c18a17cc94602c4800
|
|
| BLAKE2b-256 |
6fff402b22b09341a8e63bd92645a38f7f477a7bf3d9f33f85b8536a09c0a1e3
|
File details
Details for the file wmain-2.0.16-py3-none-any.whl.
File metadata
- Download URL: wmain-2.0.16-py3-none-any.whl
- Upload date:
- Size: 38.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.0 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1980448aca343185df46226c715038dde4edf15b1b9e9250aa9745ad6c205dfe
|
|
| MD5 |
54bf89859a02150406d09e9ddde48428
|
|
| BLAKE2b-256 |
7c732c4cd9acd8611d61eb083a2fde054552aca15ec1c506d6bee2e80a232ed6
|