[英]Using Configparser to create objects of a class?
I'm a little stumped on how to do this.我对如何做到这一点有点困惑。
Let's say I have a employees.ini file that looks like this:假设我有一个如下所示的 employees.ini 文件:
[amber]
sex=female
age=29
location=usa
income=60000
debt=300
[john]
sex=male
age=19
location=usa
income=19000
debt=nan
I have a for loop to access each piece of information and assign to a variable.我有一个 for 循环来访问每条信息并分配给一个变量。
from configparser import ConfigParser
config=ConfigParser()
config.read('employees.ini')
for section in config.sections():
name=section
sex=config[section]['sex']
age=config[section]['age']
location=config[section]['location']
income=config[section]['income']
debt=config[section]['debt']
I also have a class where each section can be accepted as an object:我还有一个 class,其中每个部分都可以作为 object 接受:
class Users:
def __init__(self, name, sex, age, location, debt):
self.__name=name
self.__sex=sex
self.__age=age
self.__location=location
self.__income=income
self.__debt=debt
def foo(self):
do a thing
def bar(self):
do a different thing ...
My hope was to now be able to access amber.foo and john.bar.我希望现在能够访问 amber.foo 和 john.bar。 However, I'm struggling on how to pass the variables out of the for loop into the class before they are overwritten by the next iteration of the loop.
但是,我正在努力解决如何将变量从 for 循环中传递到 class 中,然后再被循环的下一次迭代覆盖。 I feel like I might be overthinking this one.
我觉得我可能想多了这个。
My thought was this would make the code much more user friendly as a bulk of the code could be left untouched and only the.ini would need to be updated when a new user is needed.我的想法是这将使代码更加用户友好,因为大部分代码可以保持不变,并且在需要新用户时只需要更新.ini。
Thanks for any help you could give.感谢您提供的任何帮助。
I would add a class method to parse the configuration file data into a new object.我将添加一个 class 方法来将配置文件数据解析为新的 object。
class User:
def __init__(self, name, sex, age, location, debt):
self.__name=name
self.__sex=sex
self.__age=age
self.__location=location
self.__income=income
self.__debt=debt
@classmethod
def from_config(cls, name, config):
return cls(name, config['sex'], config['age'], config['location'], config['debt']
def foo(self):
do a thing
def bar(self):
do a different thing ...
Now the details of how to actually create an instance of User
are abstracted away in the class itself;现在,如何实际创建
User
实例的细节在 class 本身中被抽象出来; the code that iterates through the configuration need only pass the relevant data to the class method.遍历配置的代码只需将相关数据传递给 class 方法。
from configparser import ConfigParser
config=ConfigParser()
config.read('employees.ini')
users = [User.from_config(section, config[section]) for section in config.sections()]
Since your class uses the key names of configuration file as parameter names, you could just unpack the dict and use __init__
directly instead of defining a class method.由于您的 class 使用配置文件的键名作为参数名称,您可以直接解压缩 dict 并使用
__init__
而不是定义 class 方法。
from configparser import ConfigParser
config=ConfigParser()
config.read('employees.ini')
users = [User(section, **config[section]) for section in config.sections()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.