Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👨‍💻 Update 2.0.0 #28

Merged
merged 43 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d976ed1
refactor: Simplify TranslatedObject class and remove license header
DavideGalilei Dec 3, 2024
fbdafeb
refactor: Improve type hints and add docstrings to exceptions and tra…
DavideGalilei Dec 3, 2024
910cdca
refactor: Simplify base translator module by removing license header
DavideGalilei Dec 3, 2024
c1f2a54
refactor: Convert TranslatedObject to dataclass with improved type sa…
DavideGalilei Dec 3, 2024
4c8d65e
refactor: Improve proxy handling and add type hints in translator cla…
DavideGalilei Dec 3, 2024
7375acd
fix: Resolve mypy type hint errors in gpytranslate library
DavideGalilei Dec 3, 2024
ea857df
refactor: Fix type hints and variable references in gpytranslate
DavideGalilei Dec 3, 2024
116bb14
refactor: Update type hints to use typing.Callable for headers parameter
DavideGalilei Dec 3, 2024
9144e4d
style: Format code with consistent trailing commas and quotes
DavideGalilei Dec 3, 2024
6ec5714
fix: Resolve mypy type checking errors in gpytranslate library
DavideGalilei Dec 3, 2024
286aede
refactor: Fix undefined variable names in translation methods
DavideGalilei Dec 3, 2024
5552b97
fix: Resolve mypy type checking errors in gpytranslate module
DavideGalilei Dec 3, 2024
75a7f9b
refactor: Fix undefined variable names in async and sync translators
DavideGalilei Dec 3, 2024
30f04e4
refactor: Simplify imports and user agent header generation
DavideGalilei Dec 3, 2024
b0d4e92
refactor: Add type annotations for generic types in types module
DavideGalilei Dec 3, 2024
8d9498c
refactor: Simplify method signatures and remove unnecessary whitespace
DavideGalilei Dec 3, 2024
fce45b1
refactor: Add type annotations for generic types in base translator
DavideGalilei Dec 3, 2024
0778a34
refactor: Remove license headers from multiple files
DavideGalilei Dec 3, 2024
88ed2a8
feat: Add return type annotations to test and example functions
DavideGalilei Dec 3, 2024
0857a45
refactor: Rename client to http_client and remove redundant import
DavideGalilei Dec 3, 2024
425ad9e
refactor: Add type annotations and type variables to improve type che…
DavideGalilei Dec 3, 2024
625e447
feat: Add typing_extensions import for TypedDict compatibility
DavideGalilei Dec 3, 2024
87e22f6
refactor: Add type annotations to resolve mypy type checking errors
DavideGalilei Dec 3, 2024
ea0d381
refactor: Add type annotations to improve type checking in gpytranslate
DavideGalilei Dec 3, 2024
3622bce
refactor: Add type annotations and fix type checking issues
DavideGalilei Dec 3, 2024
660b6ec
refactor: Add type annotations for Mapping and List in gpytranslate a…
DavideGalilei Dec 3, 2024
15b448a
refactor: Add type annotations to functions in gpytranslate and sync_…
DavideGalilei Dec 3, 2024
2afc6e2
refactor: Add type annotations and improve httpx client configuration
DavideGalilei Dec 3, 2024
997bb60
feat: Add Tuple import to resolve undefined name errors
DavideGalilei Dec 3, 2024
b7dbaab
refactor: Remove TranslatorOptions TypedDict and simplify client options
DavideGalilei Dec 3, 2024
f97f27a
style: Add type annotations for **extra kwargs in translation methods
DavideGalilei Dec 3, 2024
3dca281
fix: Add type annotation for **extra in tts() method
DavideGalilei Dec 3, 2024
99918f2
build: Migrate from hatchling to poetry and update project configuration
DavideGalilei Dec 3, 2024
fd9e231
feat: Add pytest configuration options to pyproject.toml
DavideGalilei Dec 3, 2024
151eb39
docs: Add docstrings and improve code documentation
DavideGalilei Dec 3, 2024
c8ccff8
docs: Add docstrings to test functions for improved code documentation
DavideGalilei Dec 3, 2024
b524038
style: Format code with improved readability and consistent style
DavideGalilei Dec 3, 2024
4f780ed
refactor: Improve code quality with better docstrings, error messages…
DavideGalilei Dec 3, 2024
440305d
refactor: Remove unused imports and clean up type definitions
DavideGalilei Dec 3, 2024
ae12c90
refactor: Improve code quality with docstrings, type hints, and valid…
DavideGalilei Dec 3, 2024
2533ee0
refactor: Fix TTS method signature and documentation in Translator class
DavideGalilei Dec 3, 2024
d9471e0
docs: Add badges, language codes, error handling, and contributing gu…
DavideGalilei Dec 3, 2024
ea14253
👨‍💻 Update 2.0.0
DavideGalilei Dec 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
👨‍💻 Update 2.0.0
  • Loading branch information
DavideGalilei committed Dec 3, 2024
commit ea14253681ad4741c29056a4b2be1d44bacc58a4
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
version: [ "3.7", "3.8", "3.9", "3.10", "3.11" ]
version: [ "3.9", "3.10", "3.11", "3.12", "3.13" ]

steps:
- uses: "actions/checkout@v2"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.idea/
/gpytranslate.egg-info/
**/__pycache__
.aider*
.env
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Davide Galilei
Copyright (c) 2024 Davide Galilei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Empty file added examples/async/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion examples/async/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from gpytranslate import Translator


"""Example showing basic translation and language detection."""


async def main() -> None:
"""Demonstrate translation from Italian to English and language detection."""
t = Translator()
Expand Down
2 changes: 1 addition & 1 deletion examples/async/https_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from gpytranslate import Translator


"""Example showing how to use HTTPS proxy with the translator."""


async def main() -> None:
"""Demonstrate translation using an HTTPS proxy."""
t = Translator(proxies={"https://": "https://{proxy_ip_here}"})
Expand Down
2 changes: 1 addition & 1 deletion examples/async/socks5_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from gpytranslate import Translator


"""Example showing how to use SOCKS5 proxy with the translator."""


async def main() -> None:
"""Demonstrate translation using a SOCKS5 proxy."""
t = Translator(proxies={"socks5": "socks5://user:password@127.0.0.1:1080"})
Expand Down
2 changes: 1 addition & 1 deletion examples/async/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from gpytranslate import Translator


"""Example showing how to use text-to-speech functionality."""


async def main() -> None:
"""Demonstrate text-to-speech generation."""
translator = Translator()
Expand Down
Empty file added examples/sync/__init__.py
Empty file.
25 changes: 0 additions & 25 deletions examples/sync/example.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
"""
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License

Copyright (c) 2023 Davide Galilei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from gpytranslate import SyncTranslator

t = SyncTranslator()
Expand Down
25 changes: 0 additions & 25 deletions examples/sync/https_proxy.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
"""
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License

Copyright (c) 2023 Davide Galilei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from gpytranslate import SyncTranslator

t = SyncTranslator(proxies={"https://": "https://{proxy_ip_here}"})
Expand Down
33 changes: 2 additions & 31 deletions examples/sync/socks5_proxy.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
"""
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License

Copyright (c) 2023 Davide Galilei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from httpx_socks import SyncProxyTransport

from gpytranslate import SyncTranslator

t = SyncTranslator(
transport=SyncProxyTransport.from_url("socks5://user:password@127.0.0.1:1080")
)
t = SyncTranslator(proxies={"socks5": "socks5://user:password@127.0.0.1:1080"})
# Check out https://pypi.org/project/httpx-socks/
translation = await t.translate("Ciao Mondo!", targetlang="en")
translation = t.translate("Ciao Mondo!", targetlang="en")
# Hello World!
print(f"Translation: {translation.text}")
2 changes: 1 addition & 1 deletion gpytranslate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .exceptions import GpytranslateException, TranslationError
from .gpytranslate import Translator
from .sync import SyncTranslator
from .types import TranslatedObject
from .exceptions import GpytranslateException, TranslationError

__version__ = "1.5.1"
__all__ = [
Expand Down
12 changes: 6 additions & 6 deletions gpytranslate/gpytranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def __init__(
**options: Any,
) -> None:
"""Initialize the translator.

Args:
proxies: Optional proxy configuration dictionary
url: Translation API endpoint URL
tts_url: Text-to-speech API endpoint URL
tts_url: Text-to-speech API endpoint URL
headers: Custom headers or header generator function
**options: Additional options passed to httpx.AsyncClient
"""
Expand Down Expand Up @@ -215,13 +215,13 @@ async def tts(
targetlang: str = "en",
client: str = "at",
idx: int = 0,
prev: str = "input",
prev: str = "input",
chunk_size: int = 1024,
textlen: Optional[int] = None,
**extra: Any,
) -> Union[AsyncBufferedIOBase, io.BytesIO]:
"""Generate text-to-speech audio.

Args:
text: Text to convert to speech
file: Output file or buffer
Expand All @@ -232,10 +232,10 @@ async def tts(
chunk_size: Download chunk size
textlen: Override text length
**extra: Additional TTS parameters

Returns:
The output file/buffer with audio data

Raises:
TranslationError: If TTS generation fails
ValueError: If targetlang is invalid
Expand Down
Empty file added gpytranslate/py.typed
Empty file.
2 changes: 2 additions & 0 deletions gpytranslate/sync/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .sync_translator import SyncTranslator

__all__ = ["SyncTranslator"]
4 changes: 1 addition & 3 deletions gpytranslate/sync/sync_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ def translate(
raise TranslationError(e) from None

def detect(
self,
text: Union[str, List[Any], Dict[Any, Any]],
**kwargs: Any
self, text: Union[str, List[Any], Dict[Any, Any]], **kwargs: Any
) -> Union[str, List[str], Dict[Any, str]]:
if isinstance(text, str):
return self(text).lang
Expand Down
2 changes: 1 addition & 1 deletion gpytranslate/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Device:
@classmethod
def shift(cls) -> str:
"""Get next device string in rotation.

Returns:
str: Next device user agent string
"""
Expand Down
6 changes: 3 additions & 3 deletions gpytranslate/types/base_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def check(
dt: str,
) -> Union[TranslatedObject, Dict[K, TranslatedObject], List[TranslatedObject]]:
"""Check and validate translation API response.

Args:
text: Original input text
raw: Raw API response data
client: API client identifier
dt: Response data type

Returns:
Parsed translation result(s)

Raises:
TranslationError: If response validation fails
"""
Expand Down
16 changes: 9 additions & 7 deletions gpytranslate/types/translated_object.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""Translation result object implementation."""

from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List


@dataclass
class TranslatedObject:
"""A dataclass that holds translation results."""

raw: Dict[str, Any]
orig: str
text: str
text: str
orig_raw: List[str]
text_raw: List[str]
lang: str

def __str__(self) -> str:
"""Get string representation with the translated text.

Returns:
str: The translated text
"""
Expand All @@ -24,10 +26,10 @@ def __str__(self) -> str:
@classmethod
def from_raw_response(cls, raw: Dict[str, Any]) -> "TranslatedObject":
"""Create TranslatedObject from raw API response.

Args:
raw: Raw response dictionary from the translation API

Returns:
TranslatedObject: Parsed translation result
"""
Expand All @@ -37,5 +39,5 @@ def from_raw_response(cls, raw: Dict[str, Any]) -> "TranslatedObject":
text=" ".join(s["trans"] for s in raw["sentences"] if "trans" in s),
orig_raw=[s["orig"] for s in raw["sentences"] if "orig" in s],
text_raw=[s["trans"] for s in raw["sentences"] if "trans" in s],
lang=raw["src"]
lang=raw["src"],
)
Loading
Loading