首页 > 解决方案 > 我想用 Ruby 脚本编写一个 python 代码,怎么做?

问题描述

我在 Python 脚本 1.py 中有以下代码,它在运行时执行 read_config.rb ruby​​ 脚本。

from Naked.toolshed.shell import execute_rb, muterun_rb
success = execute_rb('read_config.rb')

现在这是一个 read_config.rb Ruby 文件

from my-vpc-netapp-only import Variables      # Python line of code to be executed 
require 'erb'
require 'yaml'

region= 'ap-south-1'
k8sver='1.18'
vpccidr= '192.168.0.0/16'
vpcid = Variables.VPC_ID
pubsubnet1a = Variables.PUBSUBNET1A
pubsubnet1b = Variables.PUBSUBNET1B
prisubnet1a = Variables.PRISUBNET1A
prisubnet1b = Variables.PRISUBNET1B

template = ERB.new File.read 'managedcluster.yaml.erb'

File.open('managedcluster.yaml', 'w') do |f|
  f.write template.result(binding)
end

在这个 Ruby 文件中,我从另一个 python 脚本中导入类变量。可以进行哪些更改以使脚本在没有任何错误的情况下执行?

标签: pythonruby-on-railsrubypython-3.8

解决方案


据我了解,my-vpc-netapp-only它是一个正在运行的应用程序,这就是为什么您需要使用导入等效项自动获取变量值的原因。否则,您可以从my-vpc-netapp-only.py. 在那个假设中,这是一个写作问题

PUBSUBNET1A
PUBSUBNET1B
PRISUBNET1A
PRISUBNET1B

每当文件在运行时发生更改时my-vpc-netapp-only.py,您就可以在 ruby​​ 脚本中读取它以制作 yaml 文件。


推荐阅读