{"id":1142,"date":"2024-10-31T15:13:05","date_gmt":"2024-10-31T15:13:05","guid":{"rendered":"https:\/\/blue16.cn\/?p=1142"},"modified":"2024-10-31T15:15:32","modified_gmt":"2024-10-31T15:15:32","slug":"%e5%88%9d%e6%8e%a2yaml%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blue16.cn\/index.php\/2024\/10\/31\/1142\/","title":{"rendered":"\u521d\u63a2Yaml\u914d\u7f6e\u6587\u4ef6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7b80\u4ecb<\/h2>\n\n\n\n<p>YAML\uff08YAML Ain&#8217;t Markup Language\uff09\u662f\u4e00\u79cd\u4eba\u7c7b\u53ef\u8bfb\u7684\u6570\u636e\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u5e38\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u3002YAML\u7684\u8bed\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5982<strong>\u6807\u91cf<\/strong>\uff08scalar\uff09\u3001<strong>\u6620\u5c04<\/strong>\uff08mapping\uff09\u548c<strong>\u5e8f\u5217<\/strong>\uff08sequence\uff09\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5e38\u7528\u7684\u914d\u7f6e\u6587\u4ef6\uff1aproperties\u3001ini\u3001json\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bed\u6cd5\u683c\u5f0f<\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2ayaml\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server_id: \"1\"\nserver_name: \"Server1\"\nserver_ip: \"192.168.1.1\"\nnetwork: 100.0\nperformance: 1000\ncapabilities:\n  - server_id: \"1\"\n    capability_type: \"LLM_CHAT\"\n    location: \"LOCAL\"\n    name: \"Chat Capability\"\n  - server_id: \"2\"\n    capability_type: \"IMAGE_OCR\"\n    location: \"REMOTE\"\n    name: \"OCR Capability\"<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6807\u91cf\uff08Scalar\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5355\u4e2a\u503c\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u7b49\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a<code>server_id: \"1\"<\/code>\uff0c<code>server_name: \"Server1\"<\/code>\uff0c<code>network: 100.0<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6620\u5c04\uff08Mapping\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a\u6574\u4e2a\u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u6620\u5c04\uff0c\u5305\u542b\u591a\u4e2a\u952e\u503c\u5bf9\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5e8f\u5217\uff08Sequence\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6709\u5e8f\u7684\u503c\u5217\u8868\uff0c\u7528&nbsp;<code>-<\/code>&nbsp;\u8868\u793a\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff1a<code>capabilities<\/code>&nbsp;\u4e0b\u9762\u7684\u6bcf\u4e2a\u80fd\u529b\u90fd\u662f\u4e00\u4e2a\u6620\u5c04\uff0c\u8fd9\u4e9b\u6620\u5c04\u7ec4\u6210\u4e00\u4e2a\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bfb\u5199\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u7528Python\u5199\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yaml\n\n# \u5b9a\u4e49\u914d\u7f6e\u6570\u636e\nconfig = {\n    \"server_id\": \"1\",\n    \"server_name\": \"Server1\",\n    \"server_ip\": \"192.168.1.1\",\n    \"network\": 100.0,\n    \"performance\": 1000,\n    \"capabilities\": &#91;\n        {\n            \"server_id\": \"1\",\n            \"capability_type\": \"LLM_CHAT\",\n            \"location\": \"LOCAL\",\n            \"name\": \"Chat Capability\"\n        },\n        {\n            \"server_id\": \"2\",\n            \"capability_type\": \"IMAGE_OCR\",\n            \"location\": \"REMOTE\",\n            \"name\": \"OCR Capability\"\n        }\n    ]\n}\n\n# \u5c06\u914d\u7f6e\u6570\u636e\u5199\u5165 YAML \u6587\u4ef6\nwith open('config.yaml', 'w') as f:\n    yaml.dump(config, f, default_flow_style=False)\n\nprint(\"\u914d\u7f6e\u6587\u4ef6\u5df2\u4fdd\u5b58\")<\/code><\/pre>\n\n\n\n<p>\u7528Python\u8bfb\u53d6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yaml\n\n# \u4ece YAML \u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6570\u636e\nwith open('config.yaml', 'r') as f:\n    config = yaml.safe_load(f)\n\n# \u6253\u5370\u914d\u7f6e\u6570\u636e\nprint(config)\n\n# \u8bbf\u95ee\u7279\u5b9a\u7684\u914d\u7f6e\u9879\nserver_id = config&#91;'server_id']\nserver_name = config&#91;'server_name']\nserver_ip = config&#91;'server_ip']\nnetwork = config&#91;'network']\nperformance = config&#91;'performance']\ncapabilities = config&#91;'capabilities']\n\nprint(f\"Server ID: {server_id}\")\nprint(f\"Server Name: {server_name}\")\nprint(f\"Server IP: {server_ip}\")\nprint(f\"Network: {network}\")\nprint(f\"Performance: {performance}\")\nprint(\"Capabilities:\")\nfor capability in capabilities:\n    print(f\"  - Server ID: {capability&#91;'server_id']}, Type: {capability&#91;'capability_type']}, Location: {capability&#91;'location']}, Name: {capability&#91;'name']}\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u624b\u6413\u4e00\u4e2ayaml<\/h2>\n\n\n\n<p>\u5173\u952e\uff1a\u7528\u7a7a\u683c\u7f29\u8fdb\uff0c\u4e0d\u8981\u7528Tab\u3002<\/p>\n\n\n\n<p>\u4e00\u4e9b\u6838\u5fc3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7f29\u8fdb\u91cf<\/strong>\uff1a\u901a\u5e38\u4f7f\u75282\u4e2a\u62164\u4e2a\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\u3002\u4fdd\u6301\u4e00\u81f4\u7684\u7f29\u8fdb\u91cf\u53ef\u4ee5\u907f\u514d\u89e3\u6790\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u7a7a\u683c<\/strong>\uff1a\u4e0d\u8981\u4f7f\u7528\u5236\u8868\u7b26\uff08Tab\uff09\uff0c\u53ea\u4f7f\u7528\u7a7a\u683c\u3002<\/li>\n\n\n\n<li><strong>\u5192\u53f7\u548c\u7a7a\u683c<\/strong>\uff1a\u952e\u548c\u503c\u4e4b\u95f4\u5fc5\u987b\u6709\u4e00\u4e2a\u7a7a\u683c\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb YAML\uff08YAML Ain&#8217;t Markup Language\uff09\u662f\u4e00\u79cd\u4eba\u7c7b\u53ef\u8bfb\u7684\u6570\u636e\u5e8f\u5217\u5316\u683c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"class_list":["post-1142","post","type-post","status-publish","format-standard","hentry","category-study","category-study_note"],"_links":{"self":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts\/1142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1142"}],"version-history":[{"count":2,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts\/1142\/revisions"}],"predecessor-version":[{"id":1144,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts\/1142\/revisions\/1144"}],"wp:attachment":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}