python的thrift2pyi学习

pip install thrift2pyi

thrift2pyi tests/example.thrift

复制代码
namespace py example

include "a/a.thrift"

const string A = "1";
const i32 B = 1;
const list<i32> C = [];
const list<i32> D = [1,2];
const map<i32,string> E = {};
const double F = 1.1;

struct Example {
    1: map<map<a.A,a.A>, a.A> A
    2: optional i16 B
    3: i32 C
    4: i64 D
    5: bool E = true
    6: byte F
    7: double G = 0.1
    8: string H = "hello"
    9: a.A I
    10: map<a.A, map<a.A,a.A>> J
    11: map<a.A, a.A> K
    12: map<string, bool> L = {}
    13: map<string, list<map<string, a.A>>> M
    14: list<a.A> N
    15: list<string> O
    16: set<i16> P
    17: list<map<string, a.A>> Q
    18: binary R
}

enum ExampleEnum {
    A = 0
    B = 1
    C = 2
}


enum ErrCode {
    ERR_SUCCESS = 0
    ERR_REQ_PARAM_INVALID  = 4000
    ERR_UNKNOWN = 5000
    ERR_SYSTEM_INNER_EXCEPTION = 5001
    ERR_LIMIT_EXCEEDED = 5002
}

union ExampleUnion {
    1: i32 A = 1
}

exception ByteException {
    1: i32 ErrorCode = 0,
}

service ExampleService {
    Example Get(1: a.A rq, 2: i64 xx)
    i16 Pull(1: string name = "", 2: map<string,string> xxx) throws (1: ByteException exc)
    void Test()
}

generated by thrift2pyi

pyi 复制代码
# coding:utf-8
from typing import List, Set, Dict
from .a import a_thrift as a
from thriftpy2.thrift import TException
from enum import Enum

A = '1'
B = 1
C = []
D = [1, 2]
E = {}
F = 1.1


# noinspection PyPep8Naming, PyShadowingNames
class ExampleEnum(Enum):
    A = 0
    B = 1
    C = 2


# noinspection PyPep8Naming, PyShadowingNames
class ErrCode(Enum):
    ERR_SUCCESS = 0
    ERR_REQ_PARAM_INVALID = 4000
    ERR_UNKNOWN = 5000
    ERR_SYSTEM_INNER_EXCEPTION = 5001
    ERR_LIMIT_EXCEEDED = 5002


# noinspection PyPep8Naming, PyShadowingNames
class Example(object):
    A: Dict[Dict[a.A, a.A], a.A]
    B: int
    C: int
    D: int
    E: bool
    F: int
    G: float
    H: str
    I: a.A
    J: Dict[a.A, Dict[a.A, a.A]]
    K: Dict[a.A, a.A]
    L: Dict[str, bool]
    M: Dict[str, List[Dict[str, a.A]]]
    N: List[a.A]
    O: List[str]
    P: Set[int]
    Q: List[Dict[str, a.A]]

    def __init__(self,
                 A: Dict[Dict[a.A, a.A], a.A] = None,
                 B: int = None,
                 C: int = None,
                 D: int = None,
                 E: bool = True,
                 F: int = None,
                 G: float = 0.1,
                 H: str = 'hello',
                 I: a.A = None,
                 J: Dict[a.A, Dict[a.A, a.A]] = None,
                 K: Dict[a.A, a.A] = None,
                 L: Dict[str, bool] = {},
                 M: Dict[str, List[Dict[str, a.A]]] = None,
                 N: List[a.A] = None,
                 O: List[str] = None,
                 P: Set[int] = None,
                 Q: List[Dict[str, a.A]] = None) -> None:
        ...


# noinspection PyPep8Naming, PyShadowingNames
class ByteException(TException):
    ErrorCode: int

    def __init__(self, ErrorCode: int = 0) -> None:
        ...


# noinspection PyPep8Naming, PyShadowingNames
class ExampleService(object):
    def Get(self, rq: a.A = None, xx: int = None) -> Example:
        ...

    def Pull(self, name: str = '', xxx: Dict[str, str] = None) -> int:
        ...

    def Test(self, ) -> None:
        ...
相关推荐
dhxhsgrx20 分钟前
PYTHON训练营DAY25
java·开发语言·python
伊织code2 小时前
PyTorch API 5 - 全分片数据并行、流水线并行、概率分布
pytorch·python·ai·api·-·5
风逸hhh3 小时前
python打卡day25@浙大疏锦行
开发语言·python
tyn1883 小时前
记录一次conda虚拟环境pip安装报错[WinError 32] 另一个程序正在使用此文件,进程无法访问
windows·conda·pip·虚拟环境·虚环境
令狐前生3 小时前
设计模式学习整理
学习·设计模式
湘-枫叶情缘3 小时前
解构认知边界:论万能方法的本体论批判与方法论重构——基于跨学科视阈的哲学-科学辩证
科技·学习·重构·生活·学习方法
魔尔助理顾问4 小时前
Flask如何读取配置信息
python·flask·bootstrap
jc_hook5 小时前
Python 接入DeepSeek
python·大模型·deepseek
inputA5 小时前
【LwIP源码学习6】UDP部分源码分析
c语言·stm32·单片机·嵌入式硬件·网络协议·学习·udp
海尔辛5 小时前
学习黑客5 分钟读懂Linux Permissions 101
linux·学习·安全