簡體   English   中英

創建一個配置文件,以保存python行為中的用戶名密碼url等值

[英]Create a config file to hold values like username password url in python behave

我正在嘗試創建一個包含用戶名密碼等信息的配置。

我創建了一個ini文件,其中包含:

[DEFAULT]
username: user
password: pass

然后我有一個配置映射類,如:

import configparser

class ConfigSectionMap:
    def __init__(self):
        my_config_parser = configparser.ConfigParser()
        my_config_parser.read('inilocation')
        print(my_config_parser.default_section)

    def config_map(self, section):
        dict1 = {}
        options = self.my_config_parser.options(section)
        for option in options:
            try:
                dict1[option] = self.my_config_parser.get(section, option)
                if dict1[option] == -1:
                    print("skip: %s" % option)
            except:
                print("exception on %s!" % option)
                dict1[option] = None
        return dict1

在我想要使用它的主要課程中,我這樣做:

from config_section_map import ConfigSectionMap

print(ConfigSectionMap.config_map(("DEFAULT")['password']))

運行時我收到一個錯誤:

TypeError:字符串索引必須是整數

我一直在關注文檔,但它不起作用: https//wiki.python.org/moin/ConfigParserExamples

或者如果有更簡單的方法請告訴我

編輯:

換到這個

print(ConfigSectionMap.config_map("DEFAULT")['password']) 

節目

TypeError: config_map() missing 1 required positional argument: 'section'

調用配置映射時出錯了。 配置映射采用一個部分,如“DEFAULT”。

你要做的是發送('DEFAULT')['密碼']。 但是('DEFAULT')求值為一個字符串,字符串索引只能取整數。

嘗試從索引開始只是您輸入的輸入錯誤。

您如何使用ConfigSectionMap存在問題。 就像現在一樣,您正在使用屬性引用,這是合法的,但不是使用config_map的預期方法。 在對config_map進行引用時(self, section) config_map()需要兩個參數(self, section) ,只傳遞一個參數。

你要么自己傳遞,要么你做一個實例。 通過調用ConfigSectionMap(),您將獲得一個在self中啟動了屬性的實例。

將代碼更改為以下代碼,您是否看到了區別?

from config_section_map import ConfigSectionMap

conf_object = ConfigSectionMap()

print(conf_object.config_map("DEFAULT")['password'])

['password']現在應用於config_map的返回結果,而不是它的參數。

解決問題options = self.my_config_parser.options(section) AttributeError: 'ConfigSectionMap' object has no attribute 'my_config_parser'

你必須在self中定義屬性,否則它將保留在__init__本地范圍內

class ConfigSectionMap:
    def __init__(self):
        self.my_config_parser = configparser.ConfigParser()
        self.my_config_parser.read('inilocation')
        print(self.my_config_parser.default_section)

    def config_map(self, section):
        dict1 = {}
        options = self.my_config_parser.options(section)
        for option in options:
            try:
                dict1[option] = self.my_config_parser.get(section, option)
                if dict1[option] == -1:
                    print("skip: %s" % option)
            except:
                print("exception on %s!" % option)
                dict1[option] = None
        return dict1

正如@officialaimm的評論指出的那樣,將DEFAULT嘗試更改配置命名為一個部分可能存在問題

[SomeThingElse]
username: user
password: pass

代替

在問題的最后部分給出另一個答案Or if there is an easier way please show me

OPTION1 = 'test'

將它保存在config.py

在代碼中

import config
getattr(config, 'OPTION1', 'default value if not found')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM