{"id":42066,"date":"2024-12-01T13:38:47","date_gmt":"2024-12-01T05:38:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42066\/"},"modified":"2024-12-01T13:38:47","modified_gmt":"2024-12-01T05:38:47","slug":"%e4%bd%bf%e7%94%a8-python-%e5%b0%86%e7%82%b9%e5%88%86%e9%9a%94%e5%80%bc%e8%bd%ac%e6%8d%a2%e4%b8%ba-go-%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42066\/","title":{"rendered":"\u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784<\/span><\/p>\n<h1>\u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 14:54:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u8fd9\u662f\u5bf9\u53ef\u4ee5\u66f4\u6539\u914d\u7f6e\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u8981\u6c42\uff08\u7279\u522b\u662f wso2 identity server\uff0c\u56e0\u4e3a\u6211\u6b63\u5728\u4f7f\u7528 go \u4e3a\u5176\u7f16\u5199 kubernetes \u8fd0\u7b97\u7b26\uff09\u3002\u4f46\u8fd9\u91cc\u786e\u5b9e\u4e0d\u76f8\u5173\u3002\u6211\u60f3\u521b\u5efa\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u5141\u8bb8\u8f7b\u677e\u7ba1\u7406\u5927\u91cf\u914d\u7f6e\u6620\u5c04\u4ee5\u751f\u6210 go \u7ed3\u6784\u3002\u8fd9\u4e9b\u914d\u7f6e\u6620\u5c04\u5728 .csv \u4e2d<\/p>\n<p>\u94fe\u63a5\u5230 .csv &#8211; my_configs.csv<\/p>\n<p>\u6211\u60f3\u8981\uff0c <strong>\u7f16\u5199\u4e00\u4e2a\u81ea\u52a8\u751f\u6210 go \u7ed3\u6784\u7684 python \u811a\u672c<\/strong>\uff0c\u8fd9\u6837\u5bf9\u5e94\u7528\u7a0b\u5e8f\u914d\u7f6e\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u5730\u6267\u884c python \u811a\u672c\u521b\u5efa\u76f8\u5e94\u7684 go \u7ed3\u6784\u6765\u66f4\u65b0\u3002\u6211\u6307\u7684\u662f\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u7684\u914d\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u66f4\u6539 csv \u4e2d\u7684 toml \u952e\u540d\u79f0\/\u53ef\u4ee5\u6dfb\u52a0\u65b0\u503c\u3002<\/p>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u5df2\u7ecf\u6210\u529f\u521b\u5efa\u4e86\u4e00\u4e2a python \u811a\u672c\uff0c<strong>\u51e0\u4e4e\u5b9e\u73b0\u4e86\u6211\u7684\u76ee\u6807<\/strong>\u3002\u811a\u672c\u662f\uff0c<\/p>\n<pre>import pandas as pd\n\ndef convert_to_dict(data):\n    result = {}\n    for row in data:\n        current_dict = result\n        for item in row[:-1]:\n            if item is not none:\n                if item not in current_dict:\n                    current_dict[item] = {}\n                current_dict = current_dict[item]\n    return result\n\ndef extract_json_key(yaml_key):\n    if isinstance(yaml_key, str) and '.' in yaml_key:\n        return yaml_key.split('.')[-1]\n    else:\n        return yaml_key\n\ndef add_fields_to_struct(struct_string,go_var,go_type,json_key,toml_key):\n    struct_string += str(go_var) + \" \" + str(go_type) + ' `json:\"' + str(json_key) + ',omitempty\" toml:\"' +str(toml_key) + '\"` ' + \"\\n\"\n    return struct_string\n\ndef generate_go_struct(struct_name, struct_data):\n    struct_name=\"configurations\" if struct_name == \"\" else struct_name\n    struct_string = \"type \" + struct_name + \" struct {\\n\"\n    yaml_key=df['yaml_key'].str.split('.').str[-1]\n    \n    # base case: generate fields for the current struct level    \n    for key, value in struct_data.items():\n        selected_rows = df[yaml_key == key]\n\n        if len(selected_rows) &gt; 1:\n            go_var = selected_rows['go_var'].values[1]\n            toml_key = selected_rows['toml_key'].values[1]\n            go_type=selected_rows['go_type'].values[1]\n            json_key=selected_rows['json_key'].values[1]\n        else:\n            go_var = selected_rows['go_var'].values[0]\n            toml_key = selected_rows['toml_key'].values[0]\n            go_type=selected_rows['go_type'].values[0]\n            json_key=selected_rows['json_key'].values[0]\n\n        # add fields to the body of the struct\n        struct_string=add_fields_to_struct(struct_string,go_var,go_type,json_key,toml_key)   \n\n    struct_string += \"}\\n\\n\"\n    \n    # recursive case: generate struct definitions for nested structs\n    for key, value in struct_data.items():\n        selected_rows = df[yaml_key == key]\n\n        if len(selected_rows) &gt; 1:\n            go_var = selected_rows['go_var'].values[1]\n        else:\n            go_var = selected_rows['go_var'].values[0]\n\n        if isinstance(value, dict) and any(isinstance(v, dict) for v in value.values()):\n            nested_struct_name = go_var\n            nested_struct_data = value\n            struct_string += generate_go_struct(nested_struct_name, nested_struct_data)\n    \n    return struct_string\n\n# read excel\ncsv_file = \"~\/downloads\/my_configs.csv\"\ndf = pd.read_csv(csv_file)\n\n# remove rows where all columns are nan\ndf = df.dropna(how='all')\n# create the 'json_key' column using the custom function\ndf['json_key'] = df['yaml_key'].apply(extract_json_key)\n\ndata=df['yaml_key'].values.tolist() # read the 'yaml_key' column\ndata = pd.dataframe({'column':data}) # convert to dataframe\n\ndata=data['column'].str.split('.', expand=true) # split by '.'\n\nnested_list = data.values.tolist() # convert to nested list\ndata=nested_list \n\nresult_json = convert_to_dict(data) # convert to dict (json)\n\n# the generated co code\ngo_struct = generate_go_struct(\"\", result_json)\n\n# write to file\nfile_path = \"output.go\"\nwith open(file_path, \"w\") as file:\n    file.write(go_struct)<\/pre>\n<p>\u95ee\u9898\u662f\uff08\u67e5\u770b csv \u7684\u4e0b\u9762\u90e8\u5206\uff09\uff0c<\/p>\n<pre>authentication.authenticator.basic\nauthentication.authenticator.basic.parameters\nauthentication.authenticator.basic.parameters.showAuthFailureReason\nauthentication.authenticator.basic.parameters.showAuthFailureReasonOnLoginPage\nauthentication.authenticator.totp\nauthentication.authenticator.totp.parameters\nauthentication.authenticator.totp.parameters.showAuthFailureReason\nauthentication.authenticator.totp.parameters.showAuthFailureReasonOnLoginPage\nauthentication.authenticator.totp.parameters.encodingMethod\nauthentication.authenticator.totp.parameters.timeStepSize<\/pre>\n<p>\u8fd9\u91cc\uff0c\u7531\u4e8e <code>basic<\/code> \u548c <code>totp<\/code> \u5b57\u6bb5 <code>parameters<\/code> \u91cd\u590d\uff0c\u56e0\u6b64\u811a\u672c\u4f1a\u6df7\u6dc6\u81ea\u8eab\u5e76\u751f\u6210\u4e24\u4e2a <code>totpparameters<\/code> \u7ed3\u6784\u3002\u9884\u671f\u7ed3\u679c\u662f\u5177\u6709 <code>basicparameters<\/code> \u548c <code>totpparameters<\/code> \u7ed3\u6784\u3002 csv \u7684 <code>yaml_key<\/code> \u5217\u4e2d\u5b58\u5728\u8bb8\u591a\u7c7b\u4f3c\u7684\u91cd\u590d\u5355\u8bcd\u3002<\/p>\n<p>\u6211\u77e5\u9053\u8fd9\u4e0e <code>go_var = selected_rows['go_var'].values[1]<\/code> \u4e2d\u7d22\u5f15\u88ab\u786c\u7f16\u7801\u4e3a 1 \u6709\u5173\uff0c\u4f46\u5f88\u96be\u4fee\u590d\u6b64\u95ee\u9898\u3002<\/p>\n<p>\u6709\u4eba\u53ef\u4ee5\u6307\u70b9\u6211\u4e00\u4e2a\u7b54\u6848\u5417\uff1f\u6211\u8ba4\u4e3a\uff0c<\/p>\n<ol>\n<li>\u9012\u5f52\u51fd\u6570\u7684\u95ee\u9898<\/li>\n<li>\u751f\u6210 json \u7684\u4ee3\u7801\u5b58\u5728\u95ee\u9898 \u53ef\u80fd\u662f\u6b64\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u3002<\/li>\n<\/ol>\n<p>\u8c22\u8c22\uff01<\/p>\n<p>\u6211\u4e5f\u5c1d\u8bd5\u8fc7\u4f7f\u7528 chatgpt\uff0c\u4f46\u662f\u7531\u4e8e\u8fd9\u4e0e\u5d4c\u5957\u548c\u9012\u5f52\u6709\u5173\uff0c\u56e0\u6b64 chatgpt \u63d0\u4f9b\u7684\u7b54\u6848\u4e0d\u662f\u5f88\u6709\u6548\u3002<\/p>\n<p><strong>\u66f4\u65b0<\/strong><\/p>\n<p>\u6211\u53d1\u73b0\u5305\u542b <code>properties<\/code>\u3001<code>pooloptions<\/code>\u3001<code>endpoint<\/code> \u548c <code>parameters<\/code> \u5b57\u6bb5\u7684\u884c\u5b58\u5728\u95ee\u9898\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u5728 <code>yaml_key<\/code> \u5217\u4e2d\u91cd\u590d\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u80fd\u591f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f46\u662f\uff0c\u6211\u5fc5\u987b\u5b8c\u5168\u4f7f\u7528\u4e00\u79cd\u65b0\u65b9\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\uff0c\u5373\u4f7f\u7528\u6811\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u904d\u5386\u5b83\u3002\u8fd9\u662f\u5176\u80cc\u540e\u7684\u4e3b\u8981\u903b\u8f91 &#8211; <\/p>\n<p>\u8fd9\u662f\u5de5\u4f5c\u7684python\u4ee3\u7801\u3002<\/p>\n<pre>import pandas as pd\nfrom collections import deque\n\nstructs=[]\nclass TreeNode:\n    def __init__(self, name):\n        self.name = name\n        self.children = []\n        self.path=\"\"\n\n    def add_child(self, child):\n        self.children.append(child)\n\ndef create_tree(data):\n    root = TreeNode('')\n    for item in data:\n        node = root\n        for name in item.split('.'):\n            existing_child = next((child for child in node.children if child.name == name), None)\n            if existing_child:\n                node = existing_child\n            else:\n                new_child = TreeNode(name)\n                node.add_child(new_child)\n                node = new_child\n    return root\n\ndef generate_go_struct(struct_data):\n    struct_name = struct_data['struct_name']\n    fields = struct_data['fields']\n    \n    go_struct = f\"type {struct_name} struct {{\\n\"\n\n    for field in fields:\n        field_name = field['name']\n        field_type = field['type']\n        field_default_val = str(field['default_val'])\n        json_key=field['json_key']\n        toml_key=field['toml_key']\n\n        tail_part=f\"\\t{field_name} {field_type} `json:\\\"{json_key},omitempty\\\" toml:\\\"{toml_key}\\\"`\\n\\n\"\n\n        if pd.isna(field['default_val']):\n            go_struct += tail_part\n        else:\n            field_default_val = \"\\t\/\/ +kubebuilder:default:=\" + field_default_val\n            go_struct += field_default_val + \"\\n\" + tail_part\n\n    go_struct += \"}\\n\\n\"\n    return go_struct\n\ndef write_go_file(go_structs, file_path):\n    with open(file_path, 'w') as file:\n        for go_struct in go_structs:\n            file.write(go_struct)\n\ndef create_new_struct(struct_name):\n    struct_name = \"Configurations\" if struct_name == \"\" else struct_name\n    struct_dict = {\n        \"struct_name\": struct_name,\n        \"fields\": []\n    }\n    \n    return struct_dict\n\ndef add_field(struct_dict, field_name, field_type,default_val,json_key, toml_key):\n    field_dict = {\n        \"name\": field_name,\n        \"type\": field_type,\n        \"default_val\": default_val,\n        \"json_key\":json_key,\n        \"toml_key\":toml_key\n    }\n    struct_dict[\"fields\"].append(field_dict)\n    \n    return struct_dict\n\ndef traverse_tree(root):\n    queue = deque([root])  \n    while queue:\n        node = queue.popleft()\n        filtered_df = df[df['yaml_key'] == node.path]\n        go_var = filtered_df['go_var'].values[0] if not filtered_df.empty else None\n        go_type = filtered_df['go_type'].values[0] if not filtered_df.empty else None\n\n        if node.path==\"\":\n            go_type=\"Configurations\"\n\n        # The structs themselves\n        current_struct = create_new_struct(go_type)\n        \n        for child in node.children:  \n            if (node.name!=\"\"):\n                child.path=node.path+\".\"+child.name   \n            else:\n                child.path=child.name\n\n            filtered_df = df[df['yaml_key'] == child.path]\n            go_var = filtered_df['go_var'].values[0] if not filtered_df.empty else None\n            go_type = filtered_df['go_type'].values[0] if not filtered_df.empty else None\n            default_val = filtered_df['default_val'].values[0] if not filtered_df.empty else None\n\n            # Struct fields\n            json_key = filtered_df['yaml_key'].values[0].split('.')[-1] if not filtered_df.empty else None\n            toml_key = filtered_df['toml_key'].values[0].split('.')[-1] if not filtered_df.empty else None\n            \n            current_struct = add_field(current_struct, go_var, go_type,default_val,json_key, toml_key)\n\n            if (child.children):\n                # Add each child to the queue for processing\n                queue.append(child)\n\n        go_struct = generate_go_struct(current_struct)\n        # print(go_struct,\"\\n\")        \n        structs.append(go_struct)\n\n    write_go_file(structs, \"output.go\")\n\ncsv_file = \"~\/Downloads\/my_configs.csv\"\ndf = pd.read_csv(csv_file) \n\nsample_data=df['yaml_key'].values.tolist()\n\n# Create the tree\ntree = create_tree(sample_data)\n\n# Traverse the tree\ntraverse_tree(tree)\n\n<\/pre>\n<p>\u975e\u5e38\u611f\u8c22\u60a8\u7684\u5e2e\u52a9\uff01<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784 \u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784 \u6765\u6e90\uff1astackoverflow 2024-04-22 14:54:34 0\u6d4f\u89c8 \u6536\u85cf \u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60Golang\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u4f7f\u7528 Python \u5c06\u70b9\u5206\u9694\u503c\u8f6c\u6362\u4e3a Go \u7ed3\u6784\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01 \u95ee\u9898\u5185\u5bb9 \u8fd9\u662f\u5bf9\u53ef\u4ee5\u66f4\u6539\u914d\u7f6e\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u8981\u6c42\uff08\u7279\u522b\u662f wso2 identity server\uff0c\u56e0\u4e3a\u6211\u6b63\u5728\u4f7f\u7528 go \u4e3a\u5176\u7f16\u5199 kubernetes \u8fd0\u7b97\u7b26\uff09\u3002\u4f46\u8fd9\u91cc\u786e\u5b9e\u4e0d\u76f8\u5173\u3002\u6211\u60f3\u521b\u5efa\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u5141\u8bb8\u8f7b\u677e\u7ba1\u7406\u5927\u91cf\u914d\u7f6e\u6620\u5c04\u4ee5\u751f\u6210 go \u7ed3\u6784\u3002\u8fd9\u4e9b\u914d\u7f6e\u6620\u5c04\u5728 .csv \u4e2d \u94fe\u63a5\u5230 .csv &#8211; my_configs.csv \u6211\u60f3\u8981\uff0c \u7f16\u5199\u4e00\u4e2a\u81ea\u52a8\u751f\u6210 go \u7ed3\u6784\u7684 python \u811a\u672c\uff0c\u8fd9\u6837\u5bf9\u5e94\u7528\u7a0b\u5e8f\u914d\u7f6e\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u5730\u6267\u884c python \u811a\u672c\u521b\u5efa\u76f8\u5e94\u7684 go \u7ed3\u6784\u6765\u66f4\u65b0\u3002\u6211\u6307\u7684\u662f\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u7684\u914d\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u66f4\u6539 csv \u4e2d\u7684 toml [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42066","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42066"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42066\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}