From 3b9c95a7379ec95aea64a89e091b68d5a6e866f9 Mon Sep 17 00:00:00 2001 From: luxuncang Date: Wed, 29 May 2024 18:36:29 +0800 Subject: [PATCH] v0.1.1 --- README-zh.md | 290 ++++++++++++++++++++++++++++++++++++++++++ README.md | 104 ++++++++------- pyproject.toml | 2 +- src/typegraph/core.py | 4 +- 4 files changed, 353 insertions(+), 47 deletions(-) create mode 100644 README-zh.md diff --git a/README-zh.md b/README-zh.md new file mode 100644 index 0000000..62e2917 --- /dev/null +++ b/README-zh.md @@ -0,0 +1,290 @@ +
+ +# TypeGraph + +_**TypeGraph** 是一个 Python 库,用于在不同类型之间进行类型转换,包括自定义类型、内置类型和结构类型 (如列表、集合和字典)。它支持同步和异步的转换方法。_ + +> 人间总有一两风,填我十万八千梦 + + [![CodeFactor](https://www.codefactor.io/repository/github/luxuncang/typegraph/badge)](https://www.codefactor.io/repository/github/luxuncang/typegraph) + [![GitHub](https://img.shields.io/github/license/luxuncang/typegraph)](https://github.com/luxuncang/typegraph/blob/master/LICENSE) + [![CodeQL](https://github.com/luxuncang/typegraph/workflows/CodeQL/badge.svg)](https://github.com/luxuncang/typegraph/blob/master/.github/workflows/codeql.yml) + +简体中文 | [English](./README.md) + +
+ +## 功能 +- 注册同步和异步函数的类型转换器。 +- 根据类型注解自动转换函数参数。 +- 支持子类、联合类型和结构类型的转换。 +- 使用 mermaid 语法可视化转换图。 + +## 安装 +使用以下命令安装运行该库所需的依赖项: + +```sh +pip install typegraph3 +``` + +Or + +```sh +pdm add typegraph3 +``` + +## 入门指南 + +### 示例:同步转换器 +注册同步转换器并使用: + +```python +from typegraph import TypeConverter + +converter = TypeConverter() + +@converter.register_converter(int, str) +def int_to_str(value: int) -> str: + return str(value) + +result = converter.convert(10, str) # "10" +print(result) +``` + +### 示例:异步转换器 +注册异步转换器并使用: + +```python +import asyncio +from typegraph import TypeConverter + +converter = TypeConverter() + +@converter.async_register_converter(str, int) +async def str_to_int(value: str) -> int: + return int(value) + +async def test_async_conversion(): + result = await converter.async_convert("10", int) # 10 + print(result) + +asyncio.run(test_async_conversion()) +``` + +### 实例:协议类型 + +```python +from typing import Protocol, TypedDict, runtime_checkable +from dataclasses import dataclass + +from typegraph import TypeConverter + +t = TypeConverter() + +class Person(Protocol): + name: str + phone: str + address: str + + def get_name(self) -> str: + ... + +class PersonDict(TypedDict): + name: str + phone: str + address: str + +class A: + name: str + phone: str + address: str + + def __init__(self, name: str, phone: str, address: str): + self.name = name + self.phone = phone + self.address = address + + def get_name(self) -> str: + return self.name + +@dataclass +class B: + name: str + phone: str + address: str + +@t.register_converter(dict, PersonDict) +def convert_dict_to_persondict(data: dict): + return PersonDict( + name=data["name"], + phone=data["phone"], + address=data["address"] + ) + +@t.register_converter(Person, str) +def convert_person_to_str(data: Person): + return f"{data.name} {data.phone} {data.address}" + +@t.register_converter(dict, A) +def convert_dict_to_a(data: dict): + return A(data["name"], data["phone"], data["address"]) + +@t.register_converter(dict, B) +def convert_dict_to_b(data: dict): + return B(data["name"], data["phone"], data["address"]) + +@t.auto_convert() +def test(a: str): + return a + +d = {"name": "John", "phone": "123", "address": "123"} + +``` + +`t.show_mermaid_graph()` + +```mermaid +graph TD; +dict-->PersonDict +dict-->A +dict-->B +Person-->str +``` + +`t.show_mermaid_graph(protocol=True)` + +```mermaid +graph TD; +dict-->PersonDict +dict-->A +dict-->B +Person-->str +A-.->Person +``` + +```bash +Converting dict[str, str] to using [, , , ], ... at 0x7fb9c8a948b0> + +'John 123 123' +``` + +### 自动转换装饰器 +根据类型注解自动转换函数参数: + +#### 同步 + +```python +from typegraph import TypeConverter + +converter = TypeConverter() + +@converter.register_converter(str, int) +def str_to_int(value: str) -> int: + return int(value) + +@converter.auto_convert() +def add_one(x: int) -> int: + return x + 1 + +result = add_one("10") # 11 +print(result) +``` + +#### 异步 + +```python +from typegraph import TypeConverter +import asyncio + +converter = TypeConverter() + +@converter.async_register_converter(str, int) +async def str_to_int(value: str) -> int: + return int(value) + +@converter.async_auto_convert() +async def add_one(x: int) -> int: + return x + 1 + +async def test_async(): + result = await add_one("10") # 11 + print(result) + +asyncio.run(test_async()) +``` + +## 测试 + +提供了单元测试,以确保库的正确功能。运行测试: + +```bash +pytest test_switch.py +``` + +测试覆盖了: +- 同步转换器的注册与执行。 +- 异步转换器的注册与执行。 +- 转换能力检查。 +- 函数参数的自动转换(同步和异步)。 + +## 可视化 + +您可以可视化类型转换图: + +```python +from typegraph import TypeConverter + +t = TypeConverter() + +class Test: + def __init__(self, t): + self.t = t + +class TestFloat(float): + ... + + +@t.register_converter(float, Test) +def str_to_Test(input_value): + return Test(input_value) + +@t.register_converter(Test, float) +def B_to_float(input_value): + return float(input_value.t) + +@t.register_converter(float, str) +async def float_to_str(input_value): + return str(input_value) + +t.show_mermaid_graph(subclass=True) +``` + + +```mermaid +graph TD; +float-->Test +float-->str +Test-->float +TestFloat-.->float +``` + +图将使用 mermaid 语法显示,您可以在线渲染或在支持的环境中(如 Jupyter Notebooks)进行渲染。 + +## 支持的类型 + +- [X] 子类类型 (SubClass type) +- [X] 联合类型 (Union type) +- [X] 注解类型 (Annotated type) +- [X] 结构类型 (Structural type) +- [X] 协议类型 (Protocol type) (只支持输入类型) +- [X] 字典类型 (TypedDict type) +- [ ] 泛型类型 (Generic type) + +## 许可 +此项目使用 MIT 许可证。 + +## 贡献 +欢迎贡献!请提出 issue 或提交 pull request 来进行更改。 + +## 联系方式 +如果您有任何问题或疑问,请在此仓库中提出 issue。 diff --git a/README.md b/README.md index 3d7152f..7d48bf6 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,42 @@ +
+ # TypeGraph -## 概述 -**TypeGraph** 是一个 Python 库,用于在不同类型之间进行类型转换,包括自定义类型、内置类型和结构类型 (如列表、集合和字典)。它支持同步和异步的转换方法。 +_**TypeGraph** is a Python library designed for type conversion between various types, including custom types, built-in types, and structural types (such as lists, sets, and dictionaries). It supports both synchronous and asynchronous conversion methods._ + +> 人间总有一两风,填我十万八千梦 + + [![CodeFactor](https://www.codefactor.io/repository/github/luxuncang/typegraph/badge)](https://www.codefactor.io/repository/github/luxuncang/typegraph) + [![GitHub](https://img.shields.io/github/license/luxuncang/typegraph)](https://github.com/luxuncang/typegraph/blob/master/LICENSE) + [![CodeQL](https://github.com/luxuncang/typegraph/workflows/CodeQL/badge.svg)](https://github.com/luxuncang/typegraph/blob/master/.github/workflows/codeql.yml) + +English | [简体中文](./README-zh.md) + +
-## 功能 -- 注册同步和异步函数的类型转换器。 -- 根据类型注解自动转换函数参数。 -- 支持子类、联合类型和结构类型的转换。 -- 使用 mermaid 语法可视化转换图。 +## Features +- Register type converters for synchronous and asynchronous functions. +- Automatically convert function arguments based on type annotations. +- Support for subclass, union types, and structural types conversion. +- Visualize the conversion graph using mermaid syntax. -## 安装 -使用以下命令安装运行该库所需的依赖项: +## Installation +Install the required dependencies with the following command: ```sh pip install typegraph3 ``` -## 入门指南 +Or -### 示例:同步转换器 -注册同步转换器并使用: +```sh +pdm add typegraph3 +``` + +## Getting Started + +### Example: Synchronous Converter +Register and use a synchronous converter: ```python from typegraph import TypeConverter @@ -34,8 +51,8 @@ result = converter.convert(10, str) # "10" print(result) ``` -### 示例:异步转换器 -注册异步转换器并使用: +### Example: Asynchronous Converter +Register and use an asynchronous converter: ```python import asyncio @@ -54,7 +71,7 @@ async def test_async_conversion(): asyncio.run(test_async_conversion()) ``` -### 实例:协议类型 +### Example: Protocol Types ```python from typing import Protocol, TypedDict, runtime_checkable @@ -151,10 +168,10 @@ Converting dict[str, str] to using [, Test @@ -251,23 +267,23 @@ Test-->float TestFloat-.->float ``` -图将使用 mermaid 语法显示,您可以在线渲染或在支持的环境中(如 Jupyter Notebooks)进行渲染。 +The graph will be displayed using mermaid syntax, which can be rendered online or in supported environments like Jupyter Notebooks. -## 支持的类型 +## Supported Types -- [X] 子类类型 (SubClass type) -- [X] 联合类型 (Union type) -- [X] 注解类型 (Annotated type) -- [X] 结构类型 (Structural type) -- [X] 协议类型 (Protocol type) (只支持输入类型) -- [X] 字典类型 (TypedDict type) -- [ ] 泛型类型 (Generic type) +- [X] Subclass type +- [X] Union type +- [X] Annotated type +- [X] Structural type +- [X] Protocol type (input types only) +- [X] TypedDict type +- [ ] Generic type -## 许可 -此项目使用 MIT 许可证。 +## License +This project is licensed under the MIT License. -## 贡献 -欢迎贡献!请提出 issue 或提交 pull request 来进行更改。 +## Contributing +Contributions are welcome! Please open an issue or submit a pull request for any changes. -## 联系方式 -如果您有任何问题或疑问,请在此仓库中提出 issue。 +## Contact +If you have any questions or concerns, please open an issue in this repository. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index a053a6a..6cf1714 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "typegraph3" -version = "0.1.0" +version = "0.1.1" description = "Type Auto Switch" authors = [ {name = "luxuncang", email = "luxuncang@qq.com"}, diff --git a/src/typegraph/core.py b/src/typegraph/core.py index a2fc9db..619a8c9 100644 --- a/src/typegraph/core.py +++ b/src/typegraph/core.py @@ -258,7 +258,7 @@ def convert( ) return converter(input_value) except Exception: - continue + ... if is_structural_type(input_type) and is_structural_type(out_type): in_origin = get_origin(input_type) out_origin = get_origin(out_type) @@ -334,7 +334,7 @@ async def async_convert( ) return await converter(input_value) except Exception: - continue + ... if is_structural_type(input_type) and is_structural_type(out_type): in_origin = get_origin(input_type) out_origin = get_origin(out_type)