2017-06-27 23 views
-3

我有一些變量和一個這樣的字典。從字典中創建一個對象作爲生成器的列表

# 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' 

等等等等。需要記住的重要一點是,所有這些類都在我的項目中的不同文件/模塊中聲明。

+3

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [在主題](http://stackoverflow.com/help/on-topic)和[如何提問](http://stackoverflow.com/help/how-to-ask)適用於此處。 StackOverflow不是一個設計,編碼,研究或教程服務。 – Prune

回答

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)) 
相關問題