创建一个配置文件以保存 python 中的用户名密码 url 等值
Create a config file to hold values like username password url in python behave
我正在尝试创建一个包含用户名密码等信息的配置。
我创建了一个包含以下内容的 ini 文件:
[DEFAULT]
username: user
password: pass
然后我有一个配置映射 class,例如:
导入配置分析器
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
在我的主要 class 中,我想使用它:
from config_section_map import ConfigSectionMap
print(ConfigSectionMap.config_map(("DEFAULT")['password']))
当 运行 我收到一个错误:
类型错误:字符串索引必须是整数
我一直在关注文档,但它不起作用:https://wiki.python.org/moin/ConfigParserExamples
或者如果有更简单的方法请告诉我
编辑:
改成这个
print(ConfigSectionMap.config_map("DEFAULT")['password'])
演出
TypeError: config_map() missing 1 required positional argument: 'section'
您调用配置映射时出错。配置映射需要一个部分,如 "DEFAULT"。
您正在尝试发送 ('DEFAULT')['password']。但是 ('DEFAULT') 求值为字符串,而字符串索引只能取整数。
尝试以索引开头只是您输入的错误。
您使用 ConfigSectionMap 的方式存在问题。就像现在一样,您正在使用属性引用,这是合法的但不是使用 config_map 的预期方式。 config_map()
需要两个参数 (self, section)
在引用 config_map 时你只传递了一个参数。
你要么传递自己,要么创建一个实例。通过调用 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')
我正在尝试创建一个包含用户名密码等信息的配置。
我创建了一个包含以下内容的 ini 文件:
[DEFAULT]
username: user
password: pass
然后我有一个配置映射 class,例如:
导入配置分析器
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
在我的主要 class 中,我想使用它:
from config_section_map import ConfigSectionMap
print(ConfigSectionMap.config_map(("DEFAULT")['password']))
当 运行 我收到一个错误:
类型错误:字符串索引必须是整数
我一直在关注文档,但它不起作用:https://wiki.python.org/moin/ConfigParserExamples
或者如果有更简单的方法请告诉我
编辑:
改成这个
print(ConfigSectionMap.config_map("DEFAULT")['password'])
演出
TypeError: config_map() missing 1 required positional argument: 'section'
您调用配置映射时出错。配置映射需要一个部分,如 "DEFAULT"。
您正在尝试发送 ('DEFAULT')['password']。但是 ('DEFAULT') 求值为字符串,而字符串索引只能取整数。
尝试以索引开头只是您输入的错误。
您使用 ConfigSectionMap 的方式存在问题。就像现在一样,您正在使用属性引用,这是合法的但不是使用 config_map 的预期方式。 config_map()
需要两个参数 (self, section)
在引用 config_map 时你只传递了一个参数。
你要么传递自己,要么创建一个实例。通过调用 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')