Skip to content

Commit ae0114f

Browse files
committed
some cosmetic corrections and a change of __ to _
1 parent 8c2bd89 commit ae0114f

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

generator/generator.py

+33-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from datetime import datetime
99
from pprint import pprint
10+
from functools import wraps
1011

1112
from config import *
1213
from game import Game
@@ -27,8 +28,8 @@ class Generator(object):
2728
GENERATOR_STATUS = False
2829

2930
def __init__(self, token: str):
30-
self.__token = token
31-
self.__auth_headers = asyncio.run(self.__get_auth())
31+
self._token = token
32+
self._auth_headers = asyncio.run(self._get_auth())
3233

3334
self.reg = re.compile("{(.*?)}")
3435
self.list_reg = re.compile(r"\[\s*['\"][^'\"]*['\"]\s*(?:,\s*['\"][^'\"]*['\"]\s*)*]")
@@ -57,6 +58,7 @@ def get_age():
5758
@staticmethod
5859
def check_current_generation():
5960
def generator(func):
61+
@wraps(func)
6062
async def wrapper(self, *args, **kwargs):
6163
if self.GENERATOR_STATUS:
6264
raise KeyError("The generator is busy. Please, try again later.")
@@ -69,7 +71,7 @@ async def wrapper(self, *args, **kwargs):
6971
return wrapper
7072
return generator
7173

72-
def __token_processing(self, response: httpx.Response) -> dict:
74+
def _token_processing(self, response: httpx.Response) -> dict:
7375
result = response.json()["result"]["alternatives"][-1]["message"]["text"]
7476
result = result.replace("\n", "")
7577
result = result.replace("\t", "")
@@ -103,16 +105,16 @@ async def generate_data_list(self, model_data):
103105
resp = await client.post(
104106
self.URL,
105107
json=data,
106-
headers=self.__auth_headers, timeout=80
108+
headers=self._auth_headers, timeout=80
107109
)
108110
# print(resp.text)
109111
if resp.status_code == 401:
110-
self.__auth_headers = await self.__get_auth()
112+
self._auth_headers = await self._get_auth()
111113
async with httpx.AsyncClient() as client:
112114
resp = await client.post(
113115
self.URL,
114116
json=data,
115-
headers=self.__auth_headers, timeout=80
117+
headers=self._auth_headers, timeout=80
116118
)
117119
if resp.status_code != 200:
118120
raise KeyError("Something went wrong, try again later."
@@ -128,18 +130,18 @@ async def generate_data_list(self, model_data):
128130
result = json.loads(result[0])
129131
return result
130132

131-
async def __get_auth(self):
132-
if "y0_" in self.__token:
133+
async def _get_auth(self):
134+
if "y0_" in self._token:
133135
async with httpx.AsyncClient() as client:
134136
response = await client.post(
135137
"https://iam.api.cloud.yandex.net/iam/v1/tokens/",
136-
json={"yandexPassportOauthToken": self.__token}, timeout=10
138+
json={"yandexPassportOauthToken": self._token}, timeout=10
137139
)
138140
return {
139141
"Authorization": f"Bearer {str(response.json()['iamToken'])}"
140142
}
141143
return {
142-
"Authorization": f"Api-Key {self.__token}"
144+
"Authorization": f"Api-Key {self._token}"
143145
}
144146

145147
@check_current_generation()
@@ -162,22 +164,22 @@ async def generate_catastrophe(self):
162164
resp = await client.post(
163165
self.URL,
164166
json=data,
165-
headers=self.__auth_headers, timeout=80
167+
headers=self._auth_headers, timeout=80
166168
)
167169
if resp.status_code == 401:
168-
self.__auth_headers = await self.__get_auth()
170+
self._auth_headers = await self._get_auth()
169171
async with httpx.AsyncClient() as client:
170172
resp = await client.post(
171173
self.URL,
172174
json=data,
173-
headers=self.__auth_headers, timeout=80
175+
headers=self._auth_headers, timeout=80
174176
)
175177
self.process_generation(False)
176178
if resp.status_code != 200:
177179
raise KeyError("Something went wrong, try again later."
178180
"It should be Yandex Cloud error (generation error), but may not be.")
179181
self.tokens += int(resp.json()["result"]["usage"]["totalTokens"])
180-
result = self.__token_processing(resp)
182+
result = self._token_processing(resp)
181183
return result
182184

183185
@check_current_generation()
@@ -264,23 +266,23 @@ async def generate_player(self, game_code):
264266
resp = await client.post(
265267
self.URL,
266268
json=data,
267-
headers=self.__auth_headers, timeout=80
269+
headers=self._auth_headers, timeout=80
268270
)
269271
if resp.status_code == 401:
270-
self.__auth_headers = await self.__get_auth()
272+
self._auth_headers = await self._get_auth()
271273
async with httpx.AsyncClient() as client:
272274
resp = await client.post(
273275
self.URL,
274276
json=data,
275-
headers=self.__auth_headers, timeout=80
277+
headers=self._auth_headers, timeout=80
276278
)
277279
self.process_generation(False)
278280
if resp.status_code != 200:
279281
raise KeyError("Something went wrong, try again later."
280282
"It should be Yandex Cloud error (generation error), but may not be.")
281283
# print(resp.json())
282284
self.tokens += int(resp.json()["result"]["usage"]["totalTokens"])
283-
result = self.__token_processing(resp)
285+
result = self._token_processing(resp)
284286

285287
result["age"] = round(self.get_age())
286288
active_card = self.games[game_code].active_card
@@ -322,18 +324,18 @@ async def generate_bunker(self):
322324
)
323325

324326
async with httpx.AsyncClient() as client:
325-
resp = await client.post(self.URL, json=data, headers=self.__auth_headers, timeout=80)
327+
resp = await client.post(self.URL, json=data, headers=self._auth_headers, timeout=80)
326328
if resp.status_code == 401:
327-
self.__auth_headers = await self.__get_auth()
329+
self._auth_headers = await self._get_auth()
328330
async with httpx.AsyncClient() as client:
329-
resp = await client.post(self.URL, json=data, headers=self.__auth_headers, timeout=80)
331+
resp = await client.post(self.URL, json=data, headers=self._auth_headers, timeout=80)
330332
self.process_generation(False)
331333
if resp.status_code != 200:
332334
raise KeyError("Something went wrong, try again later."
333335
"It should be Yandex Cloud error (generation error), but may not be.")
334336

335337
self.tokens += int(resp.json()["result"]["usage"]["totalTokens"])
336-
result = self.__token_processing(resp)
338+
result = self._token_processing(resp)
337339
return result
338340

339341
@staticmethod
@@ -378,19 +380,19 @@ async def generate_bunker_result(self, game_data):
378380
}
379381
)
380382
async with httpx.AsyncClient() as client:
381-
resp = await client.post(self.URL, json=data, headers=self.__auth_headers, timeout=80)
383+
resp = await client.post(self.URL, json=data, headers=self._auth_headers, timeout=80)
382384
# print(resp.json())
383385
if resp.status_code == 401:
384-
self.__auth_headers = await self.__get_auth()
386+
self._auth_headers = await self._get_auth()
385387
async with httpx.AsyncClient() as client:
386-
resp = await client.post(self.URL, json=data, headers=self.__auth_headers, timeout=80)
388+
resp = await client.post(self.URL, json=data, headers=self._auth_headers, timeout=80)
387389
self.process_generation(False)
388390
if resp.status_code != 200:
389391
raise KeyError("Something went wrong, try again later."
390392
"It should be Yandex Cloud error (generation error), but may not be.")
391393

392394
self.tokens += int(resp.json()["result"]["usage"]["totalTokens"])
393-
result = self.__token_processing(resp)
395+
result = self._token_processing(resp)
394396
return result
395397

396398
@check_current_generation()
@@ -413,24 +415,24 @@ async def generate_result(self, game_data):
413415
)
414416

415417
async with httpx.AsyncClient() as client:
416-
resp = await client.post(self.URL, json=data, headers=self.__auth_headers, timeout=80)
418+
resp = await client.post(self.URL, json=data, headers=self._auth_headers, timeout=80)
417419
# print(resp.json())
418420
if resp.status_code == 401:
419-
self.__auth_headers = await self.__get_auth()
421+
self._auth_headers = await self._get_auth()
420422
async with httpx.AsyncClient() as client:
421-
resp = await client.post(self.URL, json=data, headers=self.__auth_headers, timeout=80)
423+
resp = await client.post(self.URL, json=data, headers=self._auth_headers, timeout=80)
422424
self.process_generation(False)
423425

424426
if resp.status_code != 200:
425427
raise KeyError("Something went wrong, try again later."
426428
"It should be Yandex Cloud error (generation error), but may not be.")
427429

428430
self.tokens += int(resp.json()["result"]["usage"]["totalTokens"])
429-
result = self.__token_processing(resp)
431+
result = self._token_processing(resp)
430432
return result
431433

432434

433-
if __name__ == "__main__":
435+
if __name__ == "_main_":
434436
with open("../secrets/gpt-reserve/api_key.txt") as file:
435437
model_token = file.read().strip()
436438
with open("../secrets/gpt-reserve/model_uri.txt") as file:

0 commit comments

Comments
 (0)