{"id":155669,"date":"2020-09-07T14:07:33","date_gmt":"2020-09-07T06:07:33","guid":{"rendered":"http:\/\/4563.org\/?p=155669"},"modified":"2020-09-07T14:07:33","modified_gmt":"2020-09-07T06:07:33","slug":"python-%e6%8e%92%e5%88%97%e7%bb%84%e5%90%88%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=155669","title":{"rendered":"Python \u6392\u5217\u7ec4\u5408\u751f\u6210"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u6392\u5217\u7ec4\u5408\u751f\u6210               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zckun <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8001\u54e5\u4eec, \u8bf7\u6559\u4e00\u4e2a\u95ee\u9898, \u662f\u5173\u4e8e\u6392\u5217\u7ec4\u5408\u7684, \u524d\u51e0\u5929\u548c\u8001\u54e5\u4eec\u8ba8\u8bba\u4e86\u4e00\u4e0b, \u80fd\u662f\u80fd\u89e3\u51b3, \u4f46\u611f\u89c9\u4e0d\u592a\u65b9\u4fbf \u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5e94\u8be5\u5c31\u660e\u767d\u4e86.<\/p>\n<pre><code>&lt;school type=\"primary|middle|university\"&gt;     &lt;class number=\"1\"\/&gt; &lt;\/school&gt; <\/code><\/pre>\n<p>to<\/p>\n<pre><code>&lt;school type=\"primary\"&gt;     &lt;class number=\"1\"\/&gt; &lt;\/school&gt;  &lt;school type=\"middle\"&gt;     &lt;class number=\"1\"\/&gt; &lt;\/school&gt;  &lt;school type=\"university\"&gt;     &lt;class number=\"1\"\/&gt; &lt;\/school&gt; <\/code><\/pre>\n<p>\u76ee\u524d\u662f\u6211\u8fd9\u6837\u505a\u7684, \u7528\u4e86\u4e00\u4e2a\u6a21\u7248 template<\/p>\n<pre><code>&lt;school type=\"{school_type}\"&gt;     &lt;class number=\"{class_number}\"\/&gt; &lt;\/school&gt; <\/code><\/pre>\n<p>\u4ee3\u7801\u548c\u4e0a\u9762\u7684\u5b66\u6821\u4f8b\u5b50\u6709\u70b9\u4e0d\u540c\uff0c\u56e0\u4e3a\u6211\u7684\u5b8c\u6574\u9700\u6c42\u662f\u8fd9\u6837\u7684<\/p>\n<pre><code>&lt;!-- base --&gt; &lt;cross&gt;     &lt;rule1 hint=\"rule 1\" name=\"long\" val1=\"30|60\" val2=\"90|120\" condition=\"1\"&gt;         &lt;necessary_rule val1=\"60\" val2=\"120\" condition=\"1\"\/&gt;     &lt;\/rule1&gt; &lt;\/cross&gt;  &lt;!-- generator --&gt; &lt;!-- 1 --&gt; &lt;cross&gt;     &lt;rule1 hint=\"rule 1\" name=\"long\" val1=\"30\" val2=\"90\" condition=\"1\"&gt;         &lt;necessary_rule val1=\"60\" val2=\"120\" condition=\"1\"\/&gt;     &lt;\/rule1&gt; &lt;\/cross&gt;  &lt;!-- 2 --&gt; &lt;cross&gt;     &lt;rule1 hint=\"rule 1\" name=\"long\" val1=\"30\" val2=\"120\" condition=\"1\"&gt;         &lt;necessary_rule val1=\"60\" val2=\"120\" condition=\"1\"\/&gt;     &lt;\/rule1&gt; &lt;\/cross&gt;  &lt;!-- 3 --&gt; &lt;cross&gt; &lt;rule1 hint=\"rule 1\" name=\"long\" val1=\"60\" val2=\"90\" condition=\"1\"&gt;     &lt;necessary_rule val1=\"60\" val2=\"120\" condition=\"1\"\/&gt; &lt;\/rule1&gt; &lt;\/cross&gt;  &lt;!-- 4 --&gt; &lt;cross&gt; &lt;rule1 hint=\"rule 1\" name=\"long\" val1=\"60\" val2=\"120\" condition=\"1\"&gt;     &lt;necessary_rule val1=\"60\" val2=\"120\" condition=\"1\"\/&gt; &lt;\/rule1&gt; &lt;\/cross&gt;  <\/code><\/pre>\n<p>\u4ee3\u7801<\/p>\n<pre><code>import xml.etree.ElementTree as ET import itertools  from typing import Dict, Optional   def producer(rule, root_tag: Optional[str] = ''):     rule_tag = rule.tag     rule_temp = {}     rule_attrib = rule.attrib     for k, v in rule_attrib.items():         k = f'{root_tag}{rule_tag}_{k}'         if 'val' in v:             v = rule_attrib.get(v)         rule_temp[k] = v.split('|')     return rule_temp   def params_parser():     tree = ET.parse('rule.xml')     rule_params = {}     root = tree.getroot()     for strategy in root:         for rule in strategy:             rule_params.update(**producer(rule))             for child_rule in rule:                 rule_params.update(**producer(child_rule, root_tag=f'{rule.tag}_'))      return rule_params   def rule_generator(rule_params: Dict):     with open('rule_template.xml', encoding='utf-8') as f:         template = f.read()      for values in itertools.product(*rule_params.values()):         params = dict(zip(rule_params.keys(), values))         data = template.format(**params)         print(data)   def main():     rule_params = params_parser()     rule_generator(rule_params)   if __name__ == '__main__':     main() <\/code><\/pre>\n<p>\u6211\u7684\u601d\u8def\u662f\u53d6\u51fa\u6240\u6709 tag \u7684 attrib\uff0c\u7136\u540e\u66ff\u6362\u5230\u6a21\u7248. \u4e0a\u9762\u7684\u505a\u6cd5\u80fd\u89e3\u51b3\u6211\u7684\u95ee\u9898\uff0c\u4f46\u8fd9\u6837\u6bcf\u6b21\u90fd\u8981\u5f04\u4e00\u4e2a\u6a21\u7248\u6211\u89c9\u5f97\u5f88\u9ebb\u70e6\uff0c\u6709\u6ca1\u6709\u66f4\u7b80\u6d01\u7684\u529e\u6cd5<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3298745\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reter <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bd5\u4e00\u4e0b\u7528 html \u6a21\u677f\u5e93\u751f\u6210\uff1f\u6bd4\u5982 moko \u6216 jinja<br \/>\u5e94\u8be5\u4e24\u4e2a\u5faa\u73af\u5c31\u80fd\u5b8c\u6210                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3298746\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zckun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @reter \u3002\u3002\u4e00\u65f6\u6ca1\u60f3\u5230 jinja                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3298747\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : uti6770werty <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u642d\u8f66\u8bf7\u6559\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u662f\u4ee5 <br \/>testStr = string.Template(&#8216;$who is $role&#8217;) <br \/>\u5982\u679c string \u91cc\u9762\u6709\u8fc7\u767e\u4e2a$\u6a21\u677f\u53d8\u91cf\uff0c <br \/>\u662f\u5426\u53ef\u4ee5\u4ee5\u5b57\u5178\u7684\u65b9\u5f0f\u4f20\u5165\u66ff\u6362\uff1f <br \/>testDict = {&#8216;who&#8217;:&#8217;Tom,&#8217;role&#8217;:&#8217;cat&#8217;} <br \/>\u8fd9\u6837\u53bb\u5b8c\u6210\u5462\uff1f \u4e5f\u4e0d\u60f3\u542f\u7528 jinja \u76f8\u5bf9\u6bd4\u8f83\u201c\u91cd&#8221;\u5229\u5668\u53bb\u5b8c\u6210\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3298748\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zckun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @uti6770werty \u7f16\u7a0b\u73e0\u7391 26 \u9875 3.2 \u683c\u5f0f\u4fe1\u51fd\u7f16\u7a0b\u548c\u4f60\u8fd9\u7c7b\u4f3c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3298749\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @uti6770werty #3 ![image.png]( https:\/\/i.loli.net\/2020\/09\/14\/XuNsEegSWKLpzx4.png)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3298750\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii #5 s.substitute(d) \u4e5f\u662f\u53ef\u4ee5\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3298751\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @uti6770werty #3<br \/>\u53ef\u4ee5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u6392\u5217\u7ec4\u5408\u751f\u6210 \u8cc7\u6df1\u5927&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155669"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155669"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155669\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155669"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}