@@ -89,10 +89,12 @@ def __init__(
8989 source : str = None ,
9090 fmt : str = 'html' ,
9191 model : str = 'nmt' ,
92- proxies : Dict = None
92+ proxies : Dict = None ,
93+ timeout = None
9394 ):
9495 self .api_key = api_key
9596 self .target = target
97+ self .timeout = timeout
9698 if source == 'auto' :
9799 # '不提供' 替换 'auto','auto' 会导致 400,参数错误。
98100 source = None
@@ -105,29 +107,33 @@ def __init__(
105107 self .session .trust_env = False
106108 self .session .proxies = proxies
107109
108- def languages (self , target : str = None , model : str = None ) -> Union [List [LanguageResponse ], Null ]:
110+ def languages (self , target : str = None , model : str = None , timeout = ... ) -> Union [List [LanguageResponse ], Null ]:
109111 """语言支持列表"""
110112 if target is None :
111113 target = self .target
112114 if model is None :
113115 model = self .model
114- response = self .session .get (self ._LANGUAGE_URL , params = {'key' : self .api_key , 'target' : target , 'model' : model })
116+ if timeout is ...:
117+ timeout = self .timeout
118+ response = self .session .get (self ._LANGUAGE_URL , params = {'key' : self .api_key , 'target' : target , 'model' : model },
119+ timeout = timeout )
115120 if response .status_code == 200 :
116121 return [LanguageResponse (** i ) for i in response .json ()['data' ]['languages' ]]
117122 return Null (response )
118123
119124 @overload
120- def detect (self , q : str ) -> DetectResponse :
125+ def detect (self , q : str , timeout = ... ) -> DetectResponse :
121126 """..."""
122127
123128 @overload
124- def detect (self , q : List [str ]) -> List [DetectResponse ]:
129+ def detect (self , q : List [str ], timeout = ... ) -> List [DetectResponse ]:
125130 """..."""
126131
127- def detect (self , q : Union [str , List [str ]]) -> Union [DetectResponse , List [DetectResponse ], Null ]:
132+ def detect (self , q : Union [str , List [str ]], timeout = ... ) -> Union [DetectResponse , List [DetectResponse ], Null ]:
128133 """语言检测, 支持批量
129134
130135 :param q: 字符串或字符串列表
136+ :param timeout: 超时时间, int | None
131137 :return: 成功则返回: :class:`pygtrans.TranslateResponse.DetectResponse` 对象,
132138 或 :class:`pygtrans.TranslateResponse.DetectResponse` 对象列表, 这取决于 `参数: q` 是字符串还是字符串列表.
133139 失败则返回 :class:`pygtrans.Null.Null` 对象
@@ -141,15 +147,14 @@ def detect(self, q: Union[str, List[str]]) -> Union[DetectResponse, List[DetectR
141147 >>> assert isinstance(client.detect(['Hello', 'Google']), list)
142148
143149 """
150+ if timeout is ...:
151+ timeout = self .timeout
144152 ll = []
145153 for ql in split_list (q ):
146154 for qli in split_list_by_content_size (ql ):
147155 for i in range (1 , 4 ):
148- response = self .session .post (self ._DETECT_URL , params = {
149- 'key' : self .api_key
150- }, data = {
151- 'q' : qli
152- })
156+ response = self .session .post (self ._DETECT_URL , params = {'key' : self .api_key }, data = {'q' : qli },
157+ timeout = timeout )
153158 if response .status_code == 429 :
154159 time .sleep (5 * i )
155160 continue
@@ -164,19 +169,19 @@ def detect(self, q: Union[str, List[str]]) -> Union[DetectResponse, List[DetectR
164169
165170 @overload
166171 def translate (
167- self , q : str , target : str = None , source : str = None , fmt : str = None , model : str = None
172+ self , q : str , target : str = None , source : str = None , fmt : str = None , model : str = None , timeout = ...
168173 ) -> TranslateResponse :
169174 """..."""
170175
171176 @overload
172177 def translate (
173- self , q : List [str ], target : str = None , source : str = None , fmt : str = None , model : str = None
178+ self , q : List [str ], target : str = None , source : str = None , fmt : str = None , model : str = None , timeout = ...
174179 ) -> List [TranslateResponse ]:
175180 """..."""
176181
177182 def translate (
178183 self , q : Union [str , List [str ]], target : str = None , source : str = None , fmt : str = None ,
179- model : str = None
184+ model : str = None , timeout = ...
180185 ) -> Union [TranslateResponse , List [TranslateResponse ], Null ]:
181186 """文本翻译, 支持批量
182187
@@ -185,6 +190,7 @@ def translate(
185190 :param source: str: (可选) 源语言, 默认: ``self.source``, :doc:`查看支持列表 <source>`
186191 :param fmt: str: (可选) 文本格式, ``text`` | ``html``, 默认: ``self.format``
187192 :param model: str: (可选) 翻译模型, ``nmt`` | ``pbmt``, 默认: ``self.model``
193+ :param timeout: 超时时间, int | None
188194 :return: 成功则返回: :class:`pygtrans.TranslateResponse.TranslateResponse` 对象,
189195 或 :class:`pygtrans.TranslateResponse.TranslateResponse` 对象列表, 这取决于 `参数: q` 是字符串还是字符串列表.
190196 失败则返回 :class:`pygtrans.Null.Null` 对象
@@ -216,14 +222,15 @@ def translate(
216222 fmt = self .fmt
217223 if model is None :
218224 model = self .model
219-
225+ if timeout is ...:
226+ timeout = self .timeout
220227 ll = []
221228 for ql in split_list (q ):
222229 for qli in split_list_by_content_size (ql ):
223230 for i in range (1 , 4 ):
224231 response = self .session .post (self ._BASE_URL , params = {
225232 'key' : self .api_key , 'target' : target , 'source' : source , 'format' : fmt , 'model' : model
226- }, data = {'q' : qli })
233+ }, data = {'q' : qli }, timeout = timeout )
227234 if response .status_code == 429 :
228235 time .sleep (5 * i )
229236 continue
0 commit comments