Skip to content

Commit 83010d5

Browse files
authored
V2 SDK (#10)
* Add mise * Switch to pytest * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Switch to v3 api * Add proxy support * Update readme * Adjust readme * Adjust readme * Add changelog * Bump version * Add publish action * Adjust build * Add deprecations
1 parent 093d8a8 commit 83010d5

17 files changed

+291
-93
lines changed

.github/workflows/main.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ jobs:
66
strategy:
77
matrix:
88
python-version:
9-
- '2.7'
10-
- '3.6'
11-
- '3.7'
9+
- '3.8'
1210
- '3.9'
11+
- '3.10'
12+
- '3.11'
13+
- '3.12'
14+
- '3.13'
15+
- '3.14.0-beta.4'
1316
name: Python ${{ matrix.python-version }} sample
1417
steps:
15-
- uses: actions/checkout@v3
16-
- uses: actions/setup-python@v3
18+
- uses: actions/checkout@v4
19+
- uses: actions/setup-python@v4
1720
with:
1821
python-version: ${{ matrix.python-version }}
1922
cache: 'pip'
2023
- name: Install dependencies
2124
run: |
22-
python -m pip install --upgrade pip
2325
pip install -r requirements.txt
2426
pip install -r test-requirements.txt
25-
- env:
27+
- name: Run tests
28+
env:
2629
DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }}
27-
run: nosetests
30+
run: pytest

.github/workflows/publish.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Publish Python 🐍 distribution 📦 to PyPI
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build:
9+
name: Build distribution 📦
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
persist-credentials: false
16+
- name: Set up Python
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: "3.x"
20+
- name: Install pypa/build
21+
run: >-
22+
python3 -m
23+
pip install
24+
build
25+
--user
26+
- name: Build a binary wheel and a source tarball
27+
run: python3 -m build
28+
- name: Store the distribution packages
29+
uses: actions/upload-artifact@v4
30+
with:
31+
name: python-package-distributions
32+
path: dist/
33+
34+
publish-to-pypi:
35+
name: >-
36+
Publish Python 🐍 distribution 📦 to PyPI
37+
needs:
38+
- build
39+
runs-on: ubuntu-latest
40+
environment:
41+
name: pypi
42+
url: https://pypi.org/p/detectlanguage
43+
permissions:
44+
id-token: write # IMPORTANT: mandatory for trusted publishing
45+
46+
steps:
47+
- name: Download all the dists
48+
uses: actions/download-artifact@v4
49+
with:
50+
name: python-package-distributions
51+
path: dist/
52+
- name: Publish distribution 📦 to PyPI
53+
uses: pypa/gh-action-pypi-publish@release/v1

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.env.local
2+
.venv
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
9+
## v2.0.0
10+
11+
### Added
12+
- `detect_batch` method for batch detections
13+
- Proxy support
14+
15+
### Changed
16+
- Switched to v3 API which uses updated language detection model
17+
- ⚠️ `detect` method result fields are `language` and `score`
18+
19+
### Deprecated
20+
- Calling `detect()` with list argument. Use `detect_batch` instead.
21+
- `simple_detect()` - Use `detect_code()` instead. Will be removed in a future version.
22+
- `user_status()` - Use `account_status()` instead. Will be removed in a future version.
23+
24+
### Removed
25+
- Secure mode configuration. HTTPS is used by default.

README.md

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,70 @@ You can get it by signing up at https://detectlanguage.com
1515
pip install detectlanguage
1616
```
1717

18+
### Upgrading
19+
20+
When upgrading please check [changelog](CHANGELOG.md) for breaking changes.
21+
1822
### Configuration
1923

2024
```python
2125
import detectlanguage
2226

2327
detectlanguage.configuration.api_key = "YOUR API KEY"
2428

25-
# Enable secure mode (SSL) if you are passing sensitive data
26-
# detectlanguage.configuration.secure = True
29+
# You can use proxy if needed
30+
# detectlanguage.configuration.proxies = {'https': 'https://user:pass@proxy:8080'}
2731
```
2832

2933
## Usage
3034

31-
### Language detection
35+
### Detect language
3236

3337
```python
34-
detectlanguage.detect("Buenos dias señor")
38+
detectlanguage.detect("Dolce far niente")
3539
```
3640

3741
#### Result
3842

3943
```python
40-
[{'isReliable': True, 'confidence': 12.04, 'language': 'es'}]
44+
[{'language': 'it', 'score': 0.5074}]
4145
```
4246

43-
### Simple language detection
47+
### Detect single code
4448

45-
If you need just a language code you can use `simple_detect`. It returns just the language code.
49+
If you need just a language code you can use `detect_code`.
4650

4751
```python
48-
detectlanguage.simple_detect("Buenos dias señor")
52+
detectlanguage.detect_code("Dolce far niente")
4953
```
5054

5155
#### Result
5256

5357
```python
54-
'es'
58+
'it'
5559
```
5660

5761
### Batch detection
5862

5963
It is possible to detect language of several texts with one request.
6064
This method is faster than doing one request per text.
61-
To use batch detection just pass array of texts to `detect` method.
6265

6366
```python
64-
detectlanguage.detect(["Buenos dias señor", "Labas rytas"])
67+
detectlanguage.detect_batch(["Dolce far niente", "Hello world"])
6568
```
6669

6770
#### Result
6871

6972
Result is array of detections in the same order as the texts were passed.
7073

7174
```python
72-
[ [ {'isReliable': True, 'confidence': 12.04, 'language': 'es'} ],
73-
[ {'isReliable': True, 'confidence': 9.38, 'language': 'lt'} ] ]
75+
[[{'language': 'it', 'score': 0.5074}], [{'language': 'en', 'score': 0.9098}]]
7476
```
7577

76-
### Getting your account status
78+
### Get your account status
7779

7880
```python
79-
detectlanguage.user_status()
81+
detectlanguage.account_status()
8082
```
8183

8284
#### Result
@@ -87,25 +89,17 @@ detectlanguage.user_status()
8789
'plan_expires': None }
8890
```
8991

90-
### Getting list detectable languages
92+
### Get list of supported languages
9193

9294
```python
9395
detectlanguage.languages()
9496
```
9597

9698
#### Result
9799

98-
Array of language codes and names.
99-
100-
## Contribution
101-
102-
You are welcome to patch and send GitHub pull requests.
103-
104-
### Testing
105-
106-
pip install -r requirements.txt
107-
pip install -r test-requirements.txt
108-
nosetests
100+
```python
101+
[{'code': 'aa', 'name': 'Afar'}, {'code': 'ab', 'name': 'Abkhazian'}, ...]
102+
```
109103

110104
## License
111105

detectlanguage/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
__version__ = '1.5.0'
1+
__version__ = '2.0.0'
22

33
from .exceptions import *
44
from .configuration import Configuration
55
from .client import Client
6-
from .api import simple_detect, detect, user_status, languages
6+
from .api import detect, detect_code, detect_batch, account_status, languages
7+
8+
# deprecated functions
9+
from .api import simple_detect, user_status
710

811
configuration = Configuration()
912
client = Client(configuration)

detectlanguage/api.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
11
import detectlanguage
2+
import warnings
23

34
def detect(data):
4-
result = detectlanguage.client.post('detect', { 'q': data })
5-
return result['data']['detections']
5+
if isinstance(data, list):
6+
_warn_deprecated('use detect_batch instead for multiple texts')
7+
return detect_batch(data)
68

7-
def simple_detect(data):
9+
return detectlanguage.client.post('detect', { 'q': data })
10+
11+
def detect_code(data):
812
result = detect(data)
913
return result[0]['language']
1014

11-
def user_status():
12-
return detectlanguage.client.get('user/status')
15+
def detect_batch(data):
16+
return detectlanguage.client.post('detect-batch', { 'q': data })
17+
18+
def account_status():
19+
return detectlanguage.client.get('account/status')
1320

1421
def languages():
1522
return detectlanguage.client.get('languages')
23+
24+
25+
### DEPRECATED
26+
27+
def simple_detect(data):
28+
"""
29+
DEPRECATED: This function is deprecated and will be removed in a future version.
30+
Use detect_code() instead.
31+
32+
Args:
33+
data: Text to detect language for
34+
35+
Returns:
36+
str: Language code of the detected language
37+
"""
38+
_warn_deprecated(
39+
"simple_detect() is deprecated and will be removed in a future version. "
40+
"Use detect_code() instead."
41+
)
42+
return detect_code(data)
43+
44+
def user_status():
45+
"""
46+
DEPRECATED: This function is deprecated and will be removed in a future version.
47+
Use account_status() instead.
48+
49+
Returns:
50+
dict: Account status information
51+
"""
52+
_warn_deprecated(
53+
"user_status() is deprecated and will be removed in a future version. "
54+
"Use account_status() instead."
55+
)
56+
return account_status()
57+
58+
def _warn_deprecated(message):
59+
"""Internal utility function to emit deprecation warnings."""
60+
warnings.warn(
61+
message,
62+
DeprecationWarning,
63+
stacklevel=2
64+
)

detectlanguage/client.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ def __init__(self, configuration):
1313
self.configuration = configuration
1414

1515
def get(self, path, payload = {}):
16-
r = requests.get(self.url(path), params=payload, headers = self.headers(), timeout = self.configuration.timeout)
16+
r = requests.get(self.url(path), params=payload, headers = self.headers(), timeout = self.configuration.timeout, proxies = self.configuration.proxies)
1717
return self.handle_response(r)
1818

1919
def post(self, path, payload):
20-
r = requests.post(self.url(path), json=payload, headers = self.headers(), timeout = self.configuration.timeout)
20+
r = requests.post(self.url(path), json=payload, headers = self.headers(), timeout = self.configuration.timeout, proxies = self.configuration.proxies)
2121
return self.handle_response(r)
2222

2323
def handle_response(self, r):
@@ -42,10 +42,7 @@ def handle_http_error(self, r, err):
4242
raise DetectLanguageError(err)
4343

4444
def url(self, path):
45-
return "%s://%s/%s/%s" % (self.protocol(), self.configuration.host, self.configuration.api_version, path)
46-
47-
def protocol(self):
48-
return 'https' if self.configuration.secure else 'http'
45+
return "https://%s/%s/%s" % (self.configuration.host, self.configuration.api_version, path)
4946

5047
def headers(self):
5148
return {

detectlanguage/configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
class Configuration:
44
api_key = None
5-
api_version = '0.2'
5+
api_version = 'v3'
66
host = 'ws.detectlanguage.com'
77
user_agent = 'Detect Language API Python Client ' + detectlanguage.__version__
8-
secure = False
98
timeout = 5
9+
proxies = None # e.g., {'https': 'https://proxy.example.com:8080'}

mise.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[tools]
2+
3+
[env]
4+
_.file = ".env.local"
5+
_.python.venv = { path = ".venv", create = true }
6+
7+
[tasks]
8+
install-dev = "pip install -r requirements.txt && pip install -r test-requirements.txt && pip install build"
9+
test = "pytest"
10+
test-cov = "pytest --cov=detectlanguage --cov-report=term-missing"
11+
test-verbose = "pytest -v"
12+
console = "python -c \"import detectlanguage; import code; code.interact(local=locals())\""
13+
build = "python3 -m build"

0 commit comments

Comments
 (0)