0

私は変数でいっぱいのライブラリを持っています(私は手で編集したくないです)、それは次のようになります:

def get_variables(gateway):
    variables={
        'gateway':gateway, 
        'license_test_data':gateway['license_data'],
        'license_for_application':gateway['license_for_application'],
        'Valid_Login_Test_Vairables':{'valid_ssh_command':['ssh '+gateway['DeviceUnderTestUserid']+'@'+gateway['DeviceUnderTestIP'],'password:']}
        #around 3000 more just like this or worse
       }
return variables

'gateway'変数を置き換えずに、この情報をPythonデータ構造にインポートする必要があります。言い換えれば、私は次のことができるようになりたいと思っています。

print vars['Valid_Login_Test_Vairables']['valid_ssh_command']

まさにこれを手に入れよう

'ssh +gateway['DeviceUnderTestUserid']+'@'+gateway['DeviceUnderTestIP'],'password:']

代わりに私はこれで終わります:

print vars['Valid_Login_Test_Vairables']['valid_ssh_command']

"ssh gateway['DeviceUnderTestUserid']@gateway['DeviceUnderTestIP']", 'password:'

私が試しているのはこれです:

import varfile
import dummy.gateway
vars=varfile.get_variables(dummy.gateway)

私のdummy.gatewayは次のようになっています:

gateway['license_test_data'] = "gateway['license_test_data']"
gateway['license_for_application'] = "gateway['license_for_application']"
gateway['license_for_setup'] = "gateway['license_for_setup']"

変数ファイルの正確な内容を有用なデータ構造にするにはどうすればよいですか?

4

1 に答える 1

1

あなたの質問を理解するのはやや難しいと思いましたが、正しく理解できた場合、以下はあなたが持っているものからあなたが望んでいたものを生成します.

# used as a stand-in for your 'import dummy.gateway'
dummy_gateway = {}
dummy_gateway['license_test_data'] = "gateway['license_test_data']"
dummy_gateway['license_for_application'] = "gateway['license_for_application']"
dummy_gateway['license_for_setup'] = "gateway['license_for_setup']"
dummy_gateway['DeviceUnderTestUserid'] = "gateway['DeviceUnderTestUserid']"
dummy_gateway['DeviceUnderTestIP'] = "gateway['DeviceUnderTestIP']"

# your example code corrected and reformatted to be slightly more readable
def get_variables(gateway):
    variables={
        'gateway':gateway,
        'license_test_data':gateway['license_test_data'],
        'license_for_application':gateway['license_for_application'],
        'Valid_Login_Test_Vairables':{
            'valid_ssh_command':
                ['ssh '+gateway['DeviceUnderTestUserid']+'@'+
                    gateway['DeviceUnderTestIP'],
                 'password:']
            }
        #around 3000 more just like this or worse
       }
    return variables

vars = get_variables(dummy_gateway)
print vars['Valid_Login_Test_Vairables']['valid_ssh_command']

結果は、list次の 2 つの文字列で構成されます。

["ssh gateway['DeviceUnderTestUserid']@gateway['DeviceUnderTestIP']",'password:']

お役に立てれば。

PS BTW名前は標準ライブラリの組み込みPython関数varsの名前と競合するため、変数の1つの名前にそれを使用することは、この場合のように、それが機能する場合でも一般的に悪いプログラミング慣行と見なされますそれが行くように。

于 2011-12-02T21:33:02.907 に答える