跳转到主要内容

Bioregistry和Pydantic类型注解之间的集成。

项目描述

Semantic Pydantic

Tests PyPI PyPI - Python Version PyPI - License Documentation Status Codecov status Cookiecutter template from @cthoyt Code style: black Contributor Covenant

使用Bioregistry在Pydantic中注释数据模型,并在FastAPI中注释API,使它们更加FAIR。

💪 入门指南

您可以使用Pydantic和FastAPI的Field类的一些扩展之一。

from pydantic import BaseModel, Field

from semantic_pydantic import SemanticField


class Scholar(BaseModel):
    """A model representing a researcher, who might have several IDs on different services."""

    orcid: str = SemanticField(..., prefix="orcid")
    name: str = Field(..., example="Charles Tapley Hoyt")

    wos: str | None = SemanticField(default=None, prefix="wos.researcher")
    dblp: str | None = SemanticField(default=None, prefix="dblp.author")
    github: str | None = SemanticField(default=None, prefix="github")
    scopus: str | None = SemanticField(default=None, prefix="scopus")
    semion: str | None = SemanticField(default=None, prefix="semion")
    publons: str | None = SemanticField(default=None, prefix="publons.researcher")
    authorea: str | None = SemanticField(default=None, prefix="authorea.author")

同样,这也可以在FastAPI中使用。

from fastapi import FastAPI
from semantic_pydantic import SemanticPath

app = FastAPI(title="Semantic Pydantic Demo")
Scholar = ...  # defined before


@app.get("/api/orcid/{orcid}", response_model=Scholar)
def get_scholar_from_orcid(orcid: str = SemanticPath(prefix="orcid")):
    """Get xrefs for a researcher in Wikidata, given ORCID identifier."""
    ...  # full implementation in https://github.com/cthoyt/semantic-pydantic
    return Scholar(...)

以下是Swagger UI的示例,包括数据模型和端点参数上的所有注释。

可以通过克隆仓库,安装其需求,并运行自包含的demo.py来运行演示。

🚀 安装

可以从PyPI安装最新版本

pip install semantic_pydantic

可以直接从GitHub安装最新的代码和数据

pip install git+https://github.com/cthoyt/semantic-pydantic.git

👐 贡献

无论是通过提交问题、创建拉取请求还是分支,贡献都受到欢迎。有关参与信息,请参阅 CONTRIBUTING.md

👋 著作权归属

⚖️ 许可证

本包中的代码采用 MIT 许可证。

💰 资金支持

本工作最初由 Chan Zuckerberg 创新基金(CZI)在项目 2023-329850 下资助。

🍪 Cookiecutter

本包使用 @audreyfeldroycookiecutter 包以及 @cthoytcookiecutter-snekpack 模板创建。

🛠️ 开发者指南

请参阅开发者说明

README 的最后一节是为那些希望通过代码贡献来参与的人准备的。

开发安装

要开发模式安装,请使用以下命令

git clone git+https://github.com/cthoyt/semantic-pydantic.git
cd semantic-pydantic
pip install -e .

更新包模板

本项目使用 cruft 来确保模板(即配置、贡献指南、文档配置)与上游 cookiecutter 包保持更新。更新方法如下

pip install cruft
cruft update

有关 Cruft 更新命令的更多信息,请参阅这里

🥼 测试

在克隆仓库并使用 pip install tox tox-uv 安装 toxtox-uv 后,可以重复运行 tests/ 目录中的单元测试

tox

此外,这些测试在每次提交时都会自动重新运行,并在 GitHub Action 中执行。

📖 构建文档

可以使用以下命令在本地构建文档

git clone git+https://github.com/cthoyt/semantic-pydantic.git
cd semantic-pydantic
tox -e docs
open docs/build/html/index.html

文档会自动安装包以及 pyproject.toml 中指定的 docs 额外内容。可以在其中添加 texext 等 Sphinx 插件。此外,还需要将它们添加到 docs/source/conf.py 中的 extensions 列表中。

可以使用此指南使用 ReadTheDocs 将文档部署。在 .readthedocs.yml YAML 文件中包含了所有需要的配置。您还可以在 GitHub 上设置持续集成,以确保 Sphinx 可以在隔离环境中构建文档(即使用 tox -e docs-test),同时确保 ReadTheDocs 也能构建它。

配置 ReadTheDocs

  1. 使用您的 GitHub 账户登录 ReadTheDocs,在 https://readthedocs.org/accounts/login/?next=/dashboard/ 安装集成
  2. 导航到 https://readthedocs.org/dashboard/import,然后点击您仓库旁边的加号图标导入您的项目
  3. 在下一屏上,您可以使用更风格的名称(例如,带空格和首字母大写)重命名仓库
  4. 点击“下一步”,您就可以开始了!

📦 发布版本

配置 Zenodo

Zenodo 是一个长期存档系统,为您的包的每个版本分配一个 DOI。

  1. 通过以下链接使用 GitHub 登录 Zenodo:https://zenodo.org/oauth/login/github/?next=%2F。这会带您到一个页面,列出您所有的组织,并要求您批准在 GitHub 上安装 Zenodo 应用程序。点击任何您想启用集成的组织旁边的“授权”,然后点击大的绿色“批准”按钮。这一步只需要做一次。
  2. 导航到 https://zenodo.org/account/settings/github/,这里列出了您所有的GitHub仓库(包括您的用户名和您启用的任何组织)。点击任何相关仓库的开关。当您创建新仓库时,您需要回到这里。

完成这些步骤后,您就可以开始了!在GitHub上创建“发布”后(以下为操作步骤),您可以导航到 https://zenodo.org/account/settings/github/repository/cthoyt/semantic-pydantic 来查看发布的DOI并链接到Zenodo记录。

注册Python包索引(PyPI)

您只需执行以下步骤一次。

  1. Python包索引(PyPI) 注册账户
  2. 导航到 https://pypi.ac.cn/manage/account 并确保您已验证了电子邮件地址。默认情况下可能未发送验证电子邮件,因此您可能需要点击地址旁边的“选项”下拉菜单以获取“重新发送验证电子邮件”按钮。
  3. 自2023年底以来,PyPI要求进行两步验证(请参阅此 PyPI博客文章)。这意味着您必须首先生成账户恢复代码,然后设置两步验证。
  4. https://pypi.ac.cn/manage/account/token 发行API令牌

配置您的机器连接到PyPI

您必须为每台机器执行以下步骤一次。在您的家目录中创建一个名为 .pypirc 的文件,并包含以下内容

[distutils]
index-servers =
    pypi
    testpypi

[pypi]
username = __token__
password = <the API token you just got>

# This block is optional in case you want to be able to make test releases to the Test PyPI server
[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__
password = <an API token from test PyPI>

请注意,由于PyPI要求基于令牌的验证,我们使用 __token__ 作为用户名,不加修改。如果您已经有了包含 [distutils] 部分的 .pypirc 文件,只需确保存在 index-servers 键,并且 pypi 在其相关列表中。有关配置 .pypirc 文件的信息,请参阅 此处

上传到PyPI

在开发模式下安装包并使用 pip install tox tox-uv 安装 toxtox-uv 后,创建新发布的命令包含在 tox.inifinish 环境中。从shell中运行以下命令

tox -e finish

此脚本执行以下操作

  1. 使用 Bump2Versionpyproject.tomlCITATION.cffsrc/semantic_pydantic/version.pydocs/source/conf.py 中的版本号切换为不带 -dev 后缀
  2. 使用 build 将代码打包成tar存档和wheel
  3. 使用 twine 上传到PyPI。
  4. 推送到GitHub。您需要创建一个带有版本号更新的提交的发布。
  5. 将版本号提升到下一个补丁。如果您进行了重大更改并希望通过次要版本提升版本号,可以在之后使用 tox -e bumpversion -- minor

在GitHub上发布

  1. 导航到 https://github.com/cthoyt/semantic-pydantic/releases/new 来草拟一个新发布
  2. 点击“选择一个标签”下拉菜单,并选择与您刚刚创建的发布对应的标签
  3. 点击“生成发布说明”按钮以获取最近更改的快速概述。根据需要修改标题和描述
  4. 点击大号绿色“发布发布”按钮

这将触发Zenodo为您发布的版本分配DOI。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

semantic_pydantic-0.0.2.tar.gz (16.0 kB 查看哈希值)

上传时间 源代码

构建分发

semantic_pydantic-0.0.2-py3-none-any.whl (10.1 kB 查看哈希值)

上传时间 Python 3

由以下支持