Skip to content

Commit 6eef7a1

Browse files
committed
v0.6.1
- Added missing requirements - Fixed extras
1 parent 28d268d commit 6eef7a1

File tree

4 files changed

+29
-61
lines changed

4 files changed

+29
-61
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,20 @@ from py_trans import Async_PyTranslator
8787
```py
8888
tr.translate_dict("Hello!", "es")
8989
```
90+
- Get language code/name
91+
-
92+
```py
93+
# Sync version
94+
tr.get_lang_code("arabic")
95+
tr.get_lang_name("ar")
96+
97+
# Async version
98+
tr.get_lang_code_async("arabic")
99+
tr.get_lang_name_async("ar")
100+
```
90101

91102
> [!NOTE]
92-
> All the above examples also applies to async version
103+
> All the above examples also applies to async version (`Async_PyTranslator`)
93104
94105

95106
## License

py_trans/data/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "v0.6"
2+
"version": "v0.6.1"
33
}

py_trans/extras.py

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,6 @@
44

55
import os, json, aiofiles
66

7-
from sys import _getframe
8-
from functools import wraps
9-
from inspect import signature
10-
11-
12-
# Taken from https://github.com/dabeaz/curio/blob/master/curio/meta.py
13-
def from_coroutine(level=2, _cache={}):
14-
f_code = _getframe(level).f_code
15-
if f_code in _cache:
16-
return _cache[f_code]
17-
if f_code.co_flags & _CO_FROM_COROUTINE:
18-
_cache[f_code] = True
19-
return True
20-
else:
21-
if (f_code.co_flags & _CO_NESTED and f_code.co_name[0] == '<'):
22-
return from_coroutine(level + 2)
23-
else:
24-
_cache[f_code] = False
25-
return False
26-
27-
def awaitable(syncfunc):
28-
def decorate(asyncfunc):
29-
if signature(syncfunc) != signature(asyncfunc):
30-
raise TypeError(f'{syncfunc.__name__} and async {asyncfunc.__name__} have different signatures')
31-
32-
@wraps(asyncfunc)
33-
def wrapper(*args, **kwargs):
34-
if from_coroutine():
35-
return asyncfunc(*args, **kwargs)
36-
else:
37-
return syncfunc(*args, **kwargs)
38-
wrapper._syncfunc = syncfunc
39-
wrapper._asyncfunc = asyncfunc
40-
wrapper._awaitable = True
41-
wrapper.__doc__ = syncfunc.__doc__ or asyncfunc.__doc__
42-
return wrapper
43-
return decorate
44-
45-
46-
# Read json file
47-
def json_read():
48-
with open(f"{os.path.dirname(__file__)}/data/languages.json", "r") as jsn_f:
49-
codes = json.load(jsn_f)
50-
return codes
51-
52-
@awaitable(json_read)
537
async def json_read():
548
async with aiofiles.open(f"{os.path.dirname(__file__)}/data/languages.json", "r") as jsn_f:
559
codes = json.load(jsn_f)
@@ -58,20 +12,22 @@ async def json_read():
5812

5913
# Get language code
6014
def get_lang_code(name):
61-
dt = json_read()
62-
return list(dt.keys())[list(dt.values()).index(str(name).lower())]
15+
with open(f"{os.path.dirname(__file__)}/data/languages.json", "r") as jsn_f:
16+
dt = json.load(jsn_f)
17+
return list(dt.keys())[list(dt.values()).index(str(name).lower())]
6318

64-
@awaitable(get_lang_code)
65-
async def get_lang_code(name):
66-
dt = await json_read()
67-
return list(dt.keys())[list(dt.values()).index(str(name).lower())]
19+
async def get_lang_code_async(name):
20+
async with aiofiles.open(f"{os.path.dirname(__file__)}/data/languages.json", "r") as jsn_f:
21+
dt = json.load(jsn_f)
22+
return list(dt.keys())[list(dt.values()).index(str(name).lower())]
6823

6924
# Get language name
7025
def get_lang_name(code):
71-
dt = json_read()
72-
return dt[str(code)]
26+
with open(f"{os.path.dirname(__file__)}/data/languages.json", "r") as jsn_f:
27+
dt = json.load(jsn_f)
28+
return dt[str(code)]
7329

74-
@awaitable(get_lang_name)
75-
async def get_lang_name(code):
76-
dt = await json_read()
77-
return dt[str(name)]
30+
async def get_lang_name_async(code):
31+
async with aiofiles.open(f"{os.path.dirname(__file__)}/data/languages.json", "r") as jsn_f:
32+
dt = json.load(jsn_f)
33+
return dt[str(name)]

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
requests
2-
aiohttp
2+
aiohttp
3+
aiofiles

0 commit comments

Comments
 (0)