从字典中创建对象列表作为生成器

时间:2017-06-27 17:13:49

标签: python python-3.x

我有一些变量和这样的字典。

# Shared data
env = 'qa'
appr = 'staff'
name = 'Joe'

{
    "E01": {
        "Work": "raw/091_c.txt",
        "Estimate": "raw/092_c.txt",
        "Unrelated": "raw/094_c.txt",
        "Related": "raw/095_c.txt",
        "Digital": "raw/093_c.txt",
        "Status": "raw/117_c.txt"
    },
    "S01": {
        "Work": "raw/158_c.txt",
        "Estimate": "raw/159_c.txt",
        "Unrelated": "raw/161_c.txt",
        "Related": "raw/162_c.txt",
        "Digital": "raw/160_c.txt",
        "Status": "raw/172_c.txt"
    },
    "S02": {
        "Work": "raw/211_c.txt",
        "Estimate": "raw/212_c.txt",
        "Unrelated": "raw/214_c.txt",
        "Related": "raw/215_c.txt",
        "Digital": "raw/213_c.txt",
        "Status": "raw/225_c.txt"
    }
}

我想创建一个3"工作"对象,3"估计"对象等,按相同的顺序使用生成器在不同的类中生成并迭代它们。

必须使用共享数据 - env,app和name初始化每个对象。 并且每个对象必须具有顶部字典叶" E01"," S01"等和路径值" raw / 091_c.txt"作为他们的财产。

以此为例:

Class Name: "Work"
Class Properties:
                    env = 'qa'
                    appr = 'staff'
                    name = 'Joe'
                    est = 'E01'
                    path = 'raw/091_c.txt'

Class Name: "Estimate"
Class Properties:
                    env = 'qa'
                    appr = 'staff'
                    name = 'Joe'
                    est = 'E01'
                    path = 'raw/092_c.txt'
等等等等。需要记住的一点是,所有这些类都在我的项目中的不同文件/模块中声明。

1 个答案:

答案 0 :(得分:0)

民间。我能够回答我自己的问题。干得好。任何更好的解决方案都是受欢迎的。

import importlib

# Shared data
test_params = {
    'env': 'qa',
    'appr': 'staff',
    'lname': 'Smith',
    'fname': 'Peter'
}

test_est_dict = {
    "E01": {
        "Work": "raw/091_c.txt",
        "Estimate": "raw/092_c.txt",
        "Unrelated": "raw/094_c.txt",
        "Related": "raw/095_c.txt",
        "Digital": "raw/093_c.txt",
        "Status": "raw/117_c.txt"
    },
    "S01": {
        "Work": "raw/158_c.txt",
        "Estimate": "raw/159_c.txt",
        "Unrelated": "raw/161_c.txt",
        "Related": "raw/162_c.txt",
        "Digital": "raw/160_c.txt",
        "Status": "raw/172_c.txt"
    },
    "S02": {
        "Work": "raw/211_c.txt",
        "Estimate": "raw/212_c.txt",
        "Unrelated": "raw/214_c.txt",
        "Related": "raw/215_c.txt",
        "Digital": "raw/213_c.txt",
        "Status": "raw/225_c.txt"
    }
}


class NoEstimateDictError(Exception):
    def __str__(self):
        return ('The estimate_dict property has not been assigned.')


class WebServiceEngine(object):
    """docstring for WebServiceEngine"""

    def __init__(self, **params):
        self.params = params
        self._estimate_dict = None

    @property
    def estimate_dict(self):
        return self._estimate_dict

    @estimate_dict.setter
    def estimate_dict(self, est_dict):
        self._estimate_dict = est_dict

    @property
    def generate(self):
        if self._estimate_dict is None:
            raise NoEstimateDictError
        for est, files in self.estimate_dict.items():
            for classname, path in files.items():
                my_module = importlib.import_module('estimatefiles')
                yield getattr(my_module, classname).from_kwargs(est=est, path=path, **self.params)


if __name__ == '__main__':
    wsengine = WebServiceEngine(**test_params)
    wsengine.estimate_dict = test_est_dict
    for obj in wsengine.generate:
        print(str(obj))