Просмотр исходного кода

Merge pull request #365 from awwaawwa/bug_fix

Byaidu 1 год назад
Родитель
Сommit
246e3a1678
3 измененных файлов с 9 добавлено и 7 удалено
  1. 3 0
      pdf2zh/cache.py
  2. 1 2
      pdf2zh/translator.py
  3. 5 5
      test/test_translator.py

+ 3 - 0
pdf2zh/cache.py

@@ -45,6 +45,9 @@ class TranslationCache:
         return obj
 
     def __init__(self, translate_engine: str, translate_engine_params: dict = None):
+        assert (
+            len(translate_engine) < 20
+        ), "current cache require translate engine name less than 20 characters"
         self.translate_engine = translate_engine
         self.replace_params(translate_engine_params)
 

+ 1 - 2
pdf2zh/translator.py

@@ -78,8 +78,7 @@ class BaseTranslator:
                 return cache
 
         translation = self.do_translate(text)
-        if not (self.ignore_cache or ignore_cache):
-            self.cache.set(text, translation)
+        self.cache.set(text, translation)
         return translation
 
     def do_translate(self, text):

+ 5 - 5
test/test_translator.py

@@ -50,17 +50,17 @@ class TestTranslator(unittest.TestCase):
         self.assertNotEqual(first_result, second_result)
 
         # Test cache with ignore_cache=True
-        no_cache_result = translator.translate(text, ignore_cache=True)
-        self.assertNotEqual(first_result, no_cache_result)
+        no_cache_result1 = translator.translate(text, ignore_cache=True)
+        self.assertNotEqual(first_result, no_cache_result1)
 
         translator.ignore_cache = True
-        no_cache_result = translator.translate(text)
-        self.assertNotEqual(first_result, no_cache_result)
+        no_cache_result2 = translator.translate(text)
+        self.assertNotEqual(no_cache_result1, no_cache_result2)
 
         # Test cache with ignore_cache=False
         translator.ignore_cache = False
         cache_result = translator.translate(text)
-        self.assertEqual(second_result, cache_result)
+        self.assertEqual(no_cache_result2, cache_result)
 
         # Test cache with another parameter
         translator.add_cache_impact_parameters("test2", "value2")