2014-10-06 67 views
1

我在網上搜索了許多與TypeError有關的答案,並多次掃描了我的代碼,但我似乎無法看到我失蹤的第三個參數是什麼。我使用Python 2.7使用的simpy 3類型錯誤 - Python Simpy

我的代碼如下:

import simpy 
    import random 

    RANDOM_SEED = 42 
    NUM_SERVERS = 1 
    MTBF = 10 
    MTTR = 5 
    TOTAL_ENGINES = 6 
    TOTAL_SPARES = 3 
    TOTAL_IN_USE = TOTAL_ENGINES - TOTAL_SPARES 
    SIM_TIME = 100 

    class Working(object): 

     def __init__ (self, env, num, repair_facility, spares_inventory, downtime): 
      self.env = env 
      self.repair_facility = repair_facility 
      self.spares_inventory = spares_inventory 
      self.downtime = downtime 
      self.name = 'Engine %d' % (num + 1) 
      print('%s at %.2f' % (self.name, self.env.now)) 
      self.env.process(self.run()) 

     def run(self): 
      yield self.env.timeout(random.expovariate(1.0/MTBF)) 
      print('%s at %.2f' % (self.name, self.env.now)) 

      downtime_start = self.env.now 
      spare = yield self.spares_inventory.get() 
      self.downtime.append(self.env.now - downtime_start) 

      print('%s at %.2f' % (spare.name, self.env.now)) 
      print('%d' % len(spares_inventory.items)) 

      with self.repair_facility.request() as req: 
       yield req 
       print('%s begins repair at %.2f' % (self.name, self.env.now)) 

       yield self.env.timeout(random.expovariate(1.0/MTTR)) 

       yield self.spares_inventory.put(self) 
       print('%s at %.2f' % (self.name, self.env.now)) 

      print('%d' % len(spares_inventory.items)) 

    def main(): 
     env = simpy.Environment() 
     repair_facility = simpy.Resource(env, capacity = NUM_SERVERS) 
     spares_inventory = simpy.Container(env, capacity = TOTAL_ENGINES, init = TOTAL_SPARES) 
     downtime = [] 
     working = [Working(env, i, repair_facility, spares_inventory, downtime) for i in range(TOTAL_IN_USE)] 

     env.run(SIM_TIME) 

    if __name__ == '__main__': 
     main() 

這是我不斷收到錯誤:

回溯(最近通話最後一個):

 File "", line 61, in <module> 
     main() 
     File "", line 55, in main 
     env.run(SIM_TIME) 
     File "", line 120, in run 
     self.step() 
     File "", line 213, in step 
     raise event._value 
     TypeError: __init__() takes exactly 3 arguments (2 given) 

任何幫助都是非常感激,非常感謝

回答

2

你忘了一些額外的信息在你的追蹤;在您引用的回溯點之上,有幾條線條表示:

Traceback (most recent call last): 
    File "/data/evertr/sw/lib/python2.7/site-packages/simpy/events.py", line 312, in _resume 
    event = self._generator.send(event._value) 
    File "simptest.py", line 31, in run 
    spare = yield self.spares_inventory.get() 
TypeError: __init__() takes exactly 3 arguments (2 given) 

The above exception was the direct cause of the following exception: 

其次是您的回溯。

因此,您可以看到self.spares_inventory.get()呼叫是真正的罪魁禍首。令人討厭的是,這個方法實際上是一個隱藏的類實例化(很多棘手的事情發生在simpy後面,我注意到了),這就是爲什麼你看到__init__()警告。

基本上,您需要提供一個amountself.spares_inventory.get()(這裏有,無論好壞,沒有方便的默認值1)。

因此改變,要

spare = yield self.spares_inventory.get(1) 

可以解決你的問題。這些新的錯誤遵循相同的結構:追溯,後面跟着The above exception was the direct cause of the following exception,後面跟着另一個(不太相關的)追蹤)。然後你會發現其他錯誤。