繁体   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