Skip to content

Commit

Permalink
update pydantic blog
Browse files Browse the repository at this point in the history
  • Loading branch information
BuxianChen committed Apr 10, 2024
1 parent ec7ef63 commit cd315cc
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions _drafts/2024-02-18-pydantic.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,32 @@ mod = wrapper.visit(command)
print(mod.code)
```

## pydantic 的更多使用例子

### 例子 1: A 属性的值由 B 属性确定

```python
from pydantic.v1 import BaseModel, root_validator
from typing import List, Dict, Any

class A(BaseModel):
embedding_functions: Dict[str, Any]
vector_names: List[str]

@root_validator(pre=True)
def add_vector_names(cls, values):
values["vector_names"] = list(values["embedding_functions"].keys())
return values

@classmethod
def from_functions(cls, embedding_functions):
return cls(embedding_functions=embedding_functions)

embedding_functions = {"a": lambda x: [1]}
a = A.from_functions(embedding_functions)
a = A(embedding_functions=embedding_functions)
```

## FastAPI

- path parameter: 可以使用 `fastapi.Path` 配合 `typing.Annotated` 来做 type hint 以及数据校验
Expand Down

0 comments on commit cd315cc

Please sign in to comment.