Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 最佳实践:初始化类属性_Python_Python 3.x - Fatal编程技术网

Python 最佳实践:初始化类属性

Python 最佳实践:初始化类属性,python,python-3.x,Python,Python 3.x,我正在用python 3.7.9开发一个小程序,用于管理一个或多个所有者的银行帐户。 我希望能够在任何时候查询每个账户,以了解可用金额。 我已经列出了初始值​​在yaml文件中 config.yaml banks: - name: HSBC accounts: - distinct_id: abcd1234 owner: name: jason mail: jason.musk@gmail.com p

我正在用python 3.7.9开发一个小程序,用于管理一个或多个所有者的银行帐户。 我希望能够在任何时候查询每个账户,以了解可用金额。 我已经列出了初始值​​在yaml文件中

config.yaml

banks:
  - name: HSBC
    accounts:
      - distinct_id: abcd1234
        owner:
          name: jason
          mail: jason.musk@gmail.com
        products:
          - name: product1
            currencies:
            - currency: EUR
              total: 1009
            - currency: USD
              total: 809.9
          - name: product2
            currencies:
            - currency: CNY
              total: 47966
            - currency: USD
              total: 9.90
我将代码划分为不同的类,每个类对应于配置文件的一部分或子部分

main.py

import yaml


class Currency:
    def __init__(self, currency, total):
        self.currency = currency
        self.total = total


class Product:
    def __init__(self, **kwargs):
        self.name = kwargs["name"]
        for asset in kwargs["currencies"]:
            currency = asset["currency"]
            total = asset["total"]
            setattr(self, str(currency), Currency(currency, total))


class Owner:
    def __init__(self, **kwargs):
        self.name = kwargs["name"]
        self.mail = kwargs["mail"]


class Account:
    def __init__(self, **kwargs):
        self.distinct_id = kwargs["distinct_id"]
        self.owner = Owner(**kwargs["owner"])
        for product in kwargs["products"]:
            setattr(self, str(product["name"]), Product(**product))


class Bank:
    def __init__(self, **kwargs):
        self.name = kwargs["name"]
        for account in kwargs["accounts"]:
            setattr(self, str(account["distinct_id"]), Account(**account))


class Configuration:
    def __init__(self, **kwargs):
        # self.HSBC = None         # TODO: What I would like to make dynamic based on my config file ***
        # ...                      # TODO: What I would like to make dynamic based on my config file ***
        # self.AnotherBank = None  # TODO: What I would like to make dynamic based on my config file ***
        for bank in kwargs["banks"]:
            setattr(self, str(bank["name"]), Bank(**bank))


def load_configurations(path):
    with open(path, "r") as stream:
        try:
            return yaml.safe_load(stream)
        except yaml.YAMLError as e:
            print(e)


config_global = load_configurations("config.yaml")
env = Configuration(**config_global)
现在,当我想查询产品上的可用余额时,我运行以下命令

print(env.HSBC.abcd1234.product1.EUR.total)
它工作得很好,但是我的IDE不知道“HSBC”属性(也不知道下面的属性)

因此,我必须在“Configuration”类中手动输入该属性,以便我的IDE可以为我提供它。这是不实际的,尤其是当我需要在配置文件中更改银行名称或需要添加其他银行的名称时


是否可以根据配置文件在类中动态添加一个或多个属性,以便IDE重新接收并提出它们(在我的情况下是PyCharm,但这只是一个细节)?

它怎么知道?您可能会更好地使用环境银行(“汇丰”)账户(“abcd1234”)…之类的属性,而不是任意属性。感谢您返回@jornsharpe,我的方法不太好。如果我理解正确,我将不得不通过方法而不是属性?是的,使用方法。或者您可以实现一个并拥有
env[“HSBC”]
,但我认为方法版本更明确一点,我看到了,非常感谢@jornsharpe!