Forráskód Böngészése

feat: combined_decorator

Zhang Li 11 hónapja
szülő
commit
cb2b0440e3
2 módosított fájl, 25 hozzáadás és 8 törlés
  1. 23 1
      cores/post_decorators.py
  2. 2 7
      cores/post_hander.py

+ 23 - 1
cores/post_decorators.py

@@ -111,4 +111,26 @@ def rule6_decorator(f, *args, **kwargs):
                     predict_line[2] = '营养素参考值'+r[1]
     except IndexError as e:
         print('rule6_decorator', e)
-    return predict_line
+    return predict_line
+
+
+decorators = []
+
+
+def register_decorator(decorator):
+    decorators.append(decorator)
+
+
+def combined_decorator(func):
+    for decorator in reversed(decorators):
+        func = decorator(func)
+    return func
+
+
+register_decorator(rule1_decorator)
+register_decorator(rule2_decorator)
+register_decorator(rule3_decorator)
+register_decorator(rule4_decorator)
+register_decorator(rule5_decorator)
+register_decorator(rule6_decorator)
+

+ 2 - 7
cores/post_hander.py

@@ -1,6 +1,6 @@
 import re
 from typing import List
-from .post_decorators import *
+from .post_decorators import combined_decorator
 
 class PostHandler:
     def __init__(self, predict_html):
@@ -39,12 +39,7 @@ class PostHandler:
         else:
             return self.predict_html
 
-    @rule6_decorator
-    @rule5_decorator
-    @rule4_decorator
-    @rule3_decorator
-    @rule2_decorator
-    @rule1_decorator
+    @combined_decorator
     def _format_predict_line(self, predict_line):
         return predict_line