emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/pyim f574812 3/3: pyim-dcache-init-variable and pyim-dc


From: ELPA Syncer
Subject: [elpa] externals/pyim f574812 3/3: pyim-dcache-init-variable and pyim-dcache-reload-variable are macros now.
Date: Tue, 14 Dec 2021 22:57:31 -0500 (EST)

branch: externals/pyim
commit f5748123d102f458bd97a689b092e045ebd8dc34
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>

    pyim-dcache-init-variable and pyim-dcache-reload-variable are macros now.
    
        * tests/pyim-tests.el (pyim-tests-pyim-dcache-handle-variable): New test
    
        * pyim-dregcache.el (pyim-dregcache-init-variables): updated.
    
        * pyim-dhashcache.el (pyim-dhashcache-update-ishortcode2word)
        (pyim-dhashcache-update-shortcode2word)
        (pyim-dhashcache-update-code2word)
        (pyim-dhashcache-update-icode2word)
        (pyim-dhashcache-init-variables): updated.
    
        * pyim-dcache.el (pyim-dcache-init-variable): macro now.
        (pyim-dcache-reload-variable): macro now.
    
    原来使用 (symbol-value) 来实现两个功能,但在写测试代码的时候,容易误将当前使用的
    词库覆盖掉,因为:
    
    Note that if ‘lexical-binding’ is in effect, this returns the
    global value outside of any lexical scope.
---
 pyim-dcache.el      | 16 ++++++++--------
 pyim-dhashcache.el  | 34 +++++++++++++++++-----------------
 pyim-dregcache.el   |  2 +-
 tests/pyim-tests.el | 26 +++++++++++++-------------
 4 files changed, 39 insertions(+), 39 deletions(-)

diff --git a/pyim-dcache.el b/pyim-dcache.el
index e4361f4..1df6718 100644
--- a/pyim-dcache.el
+++ b/pyim-dcache.el
@@ -85,21 +85,21 @@ dcache 文件的方法让 pyim 正常工作。")
   "初始化 dcache 缓存相关变量."
   (pyim-dcache-call-api 'init-variables))
 
-(defun pyim-dcache-init-variable (variable &optional fallback-value)
+(defmacro pyim-dcache-init-variable (variable &optional fallback-value)
   "初始化 VARIABLE.
 
 如果 VARIABLE 的值为 nil, 则使用 ‘pyim-dcache-directory’ 中对应文
 件的内容来设置 VARIABLE 变量, 如果此时 VARIABLE 取值还是 nil, 那
 么就将 VARIABLE 的值设置为 FALLBACK-VALUE."
-  (unless (symbol-value variable)
-    (set variable (or (pyim-dcache-get-value variable)
-                      fallback-value
-                      (make-hash-table :test #'equal)))))
+  `(unless ,variable
+     (setf ,variable (or (pyim-dcache-get-value ',variable)
+                         ,fallback-value
+                         (make-hash-table :test #'equal)))))
 
-(defun pyim-dcache-reload-variable (variable)
+(defmacro pyim-dcache-reload-variable (variable)
   "从 `pyim-dcache-directory' 重新读取并设置 VARIABLE 的值."
-  (set variable (or (pyim-dcache-get-value variable)
-                    (make-hash-table :test #'equal))))
+  `(setf ,variable (or (pyim-dcache-get-value ',variable)
+                       (make-hash-table :test #'equal))))
 
 (defun pyim-dcache-get-value (variable)
   "从 `pyim-dcache-directory' 中读取与 VARIABLE 对应的文件中保存的值."
diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el
index 091b9ad..0d530ad 100644
--- a/pyim-dhashcache.el
+++ b/pyim-dhashcache.el
@@ -116,15 +116,15 @@
      `(lambda ()
         ,@(pyim-dhashcache-async-inject-variables)
         (require 'pyim-dhashcache)
-        (pyim-dcache-init-variable 'pyim-dhashcache-icode2word)
-        (pyim-dcache-init-variable 'pyim-dhashcache-iword2count)
+        (pyim-dcache-init-variable pyim-dhashcache-icode2word)
+        (pyim-dcache-init-variable pyim-dhashcache-iword2count)
         (pyim-dcache-save-variable
          'pyim-dhashcache-ishortcode2word
          (pyim-dhashcache-update-ishortcode2word-1
           pyim-dhashcache-icode2word
           pyim-dhashcache-iword2count)))
      (lambda (_)
-       (pyim-dcache-reload-variable 'pyim-dhashcache-ishortcode2word)))))
+       (pyim-dcache-reload-variable pyim-dhashcache-ishortcode2word)))))
 
 (defun pyim-dhashcache-update-ishortcode2word-1 (icode2word iword2count)
   "`pyim-dhashcache-update-ishortcode2word' 内部函数."
@@ -158,15 +158,15 @@
      `(lambda ()
         ,@(pyim-dhashcache-async-inject-variables)
         (require 'pyim-dhashcache)
-        (pyim-dcache-init-variable 'pyim-dhashcache-code2word)
-        (pyim-dcache-init-variable 'pyim-dhashcache-iword2count)
+        (pyim-dcache-init-variable pyim-dhashcache-code2word)
+        (pyim-dcache-init-variable pyim-dhashcache-iword2count)
         (pyim-dcache-save-variable
          'pyim-dhashcache-shortcode2word
          (pyim-dhashcache-update-shortcode2word-1
           pyim-dhashcache-code2word
           pyim-dhashcache-iword2count)))
      (lambda (_)
-       (pyim-dcache-reload-variable 'pyim-dhashcache-shortcode2word)))))
+       (pyim-dcache-reload-variable pyim-dhashcache-shortcode2word)))))
 
 (defun pyim-dhashcache-update-shortcode2word-1 (code2word iword2count)
   "`pyim-dhashcache-update-shortcode2word' 的内部函数"
@@ -273,8 +273,8 @@ DCACHE 是一个 code -> words 的 hashtable.
             (pyim-dhashcache-generate-word2code-dcache-file dcache 
,word2code-file))
           (pyim-dcache-save-value-to-file ',dicts-md5 ,code2word-md5-file))
        (lambda (_)
-         (pyim-dcache-reload-variable 'pyim-dhashcache-code2word)
-         (pyim-dcache-reload-variable 'pyim-dhashcache-word2code)
+         (pyim-dcache-reload-variable pyim-dhashcache-code2word)
+         (pyim-dcache-reload-variable pyim-dhashcache-word2code)
          (pyim-dhashcache-update-shortcode2word force)
          (setq pyim-dhashcache-update-code2word-running-p nil))))))
 
@@ -338,8 +338,8 @@ code 对应的中文词条了。
      `(lambda ()
         ,@(pyim-dhashcache-async-inject-variables)
         (require 'pyim-dhashcache)
-        (pyim-dcache-init-variable 'pyim-dhashcache-icode2word)
-        (pyim-dcache-init-variable 'pyim-dhashcache-iword2count)
+        (pyim-dcache-init-variable pyim-dhashcache-icode2word)
+        (pyim-dcache-init-variable pyim-dhashcache-iword2count)
         (maphash
          (lambda (key value)
            (puthash key (pyim-dhashcache-sort-words value 
pyim-dhashcache-iword2count)
@@ -350,7 +350,7 @@ code 对应的中文词条了。
          pyim-dhashcache-icode2word)
         nil)
      (lambda (_)
-       (pyim-dcache-reload-variable 'pyim-dhashcache-icode2word)
+       (pyim-dcache-reload-variable pyim-dhashcache-icode2word)
        (pyim-dhashcache-update-ishortcode2word force)))))
 
 (defun pyim-dhashcache-upgrade-icode2word ()
@@ -388,12 +388,12 @@ code 对应的中文词条了。
 
 (defun pyim-dhashcache-init-variables ()
   "初始化 dcache 缓存相关变量."
-  (pyim-dcache-init-variable 'pyim-dhashcache-iword2count)
-  (pyim-dcache-init-variable 'pyim-dhashcache-code2word)
-  (pyim-dcache-init-variable 'pyim-dhashcache-word2code)
-  (pyim-dcache-init-variable 'pyim-dhashcache-shortcode2word)
-  (pyim-dcache-init-variable 'pyim-dhashcache-icode2word)
-  (pyim-dcache-init-variable 'pyim-dhashcache-ishortcode2word))
+  (pyim-dcache-init-variable pyim-dhashcache-iword2count)
+  (pyim-dcache-init-variable pyim-dhashcache-code2word)
+  (pyim-dcache-init-variable pyim-dhashcache-word2code)
+  (pyim-dcache-init-variable pyim-dhashcache-shortcode2word)
+  (pyim-dcache-init-variable pyim-dhashcache-icode2word)
+  (pyim-dcache-init-variable pyim-dhashcache-ishortcode2word))
 
 (defun pyim-dhashcache-save-personal-dcache-to-file ()
   ;; 用户选择过的词
diff --git a/pyim-dregcache.el b/pyim-dregcache.el
index 96c1f08..09a561b 100644
--- a/pyim-dregcache.el
+++ b/pyim-dregcache.el
@@ -329,7 +329,7 @@ DICT-FILES 是词库文件列表. DICTS-MD5 是词库的MD5校验码.
 (defun pyim-dregcache-init-variables ()
   "初始化 cache 缓存相关变量."
   (pyim-dcache-init-variable
-   'pyim-dregcache-iword2count
+   pyim-dregcache-iword2count
    ;; dregcache 引擎也需要词频信息,第一次使用 dregcache 引擎的时候,
    ;; 自动导入 dhashcache 引擎的词频信息,以后两个引擎的词频信息就
    ;; 完全分开了。
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 8c2105a..dc0c8b0 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -626,25 +626,25 @@
     (should (equal (gethash "ni-hao" (pyim-dcache-get-value-from-file file))
                    '("你好")))))
 
-(defvar pyim-tests-dcache-variable nil)
 (ert-deftest pyim-tests-pyim-dcache-handle-variable ()
   (let ((pyim-dcache-directory
-         (file-name-as-directory (make-temp-name "pyim-dcache-"))))
+         (file-name-as-directory (make-temp-name "pyim-dcache-")))
+        my/test:1)
 
-    (pyim-dcache-save-variable 'pyim-tests-dcache-variable "hello")
-    (should (equal (pyim-dcache-get-value 'pyim-tests-dcache-variable) 
"hello"))
+    (pyim-dcache-save-variable 'my/test:1 "hello")
+    (should (equal (pyim-dcache-get-value 'my/test:1) "hello"))
 
-    (setq pyim-tests-dcache-variable "hi")
-    (pyim-dcache-reload-variable 'pyim-tests-dcache-variable)
-    (should (equal pyim-tests-dcache-variable "hello"))
+    (setq my/test:1 "hi")
+    (pyim-dcache-reload-variable my/test:1)
+    (should (equal my/test:1 "hello"))
 
-    (setq pyim-tests-dcache-variable "hi")
-    (pyim-dcache-init-variable 'pyim-tests-dcache-variable)
-    (should (equal pyim-tests-dcache-variable "hi"))
+    (setq my/test:1 "hi")
+    (pyim-dcache-init-variable my/test:1)
+    (should (equal my/test:1 "hi"))
 
-    (setq pyim-tests-dcache-variable nil)
-    (pyim-dcache-init-variable 'pyim-tests-dcache-variable)
-    (should (equal pyim-tests-dcache-variable "hello"))))
+    (setq my/test:1 nil)
+    (pyim-dcache-init-variable my/test:1)
+    (should (equal my/test:1 "hello"))))
 
 ;; ** pyim-dhashcache 相关单元测试
 (ert-deftest pyim-tests-pyim-dhashcache-get-shortcodes ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]