I have a simplified code here.(Original code is too much to put up)
import time
class Stock:
def __init__(self, Asset):
self.Asset = Asset
self.i_init = 0
self.i_Ana = 0
def Initiate_sequence(self):
self.i_init +=1
"""One Execute Once"""
print("Initiate Program")
def Analysis(self):
"""Need to Execute every 60 Seconds"""
self.i_Ana += 1
print(self.Asset)
print(f"Init {self.i_init}")
print(f"Ana {self.i_Ana}")
I want to execute the program like this (This execute with no problem):
stock_one = Stock("stock_one")
stock_one.Initiate_sequence()
stock_two = Stock("stock_two")
stock_two.Initiate_sequence()
stock_three = Stock("stock_three")
stock_three.Initiate_sequence()
while True:
stock_one.Analysis()
stock_two.Analysis()
stock_two.Analysis()
time.sleep(10)
But, there more than 3 stocks, so I need "for loop" to iterate over this Assets list
I did:
Assets = ["stock_one", "stock_two", "stock_three"]
for asset in Assets:
asset = Stock(asset)
asset.Initiate_sequence() # Initialize all the assets
while True: # Loop each asset analysis module every 10 seconds
for asset in Assets:
asset.Analysis()
time.sleep(10)
Now it gives error
Initiate Program
Initiate Program
Initiate Program
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-420b1a82343b> in <module>
6 while True: # Loop each asset analysis module every 10 seconds
7 for asset in Assets:
----> 8 asset.Analysis()
9 time.sleep(10)
10
AttributeError: 'str' object has no attribute 'Analysis'
Any idea how to fix this? Much appriciated.
With Daniel's answer I did this
Assets = ["stock_one", "stock_two", "stock_three"]
stocks = map(Stock, Assets)
while True:
for stock in stocks:
stock.Analysis()
time.sleep(10)
For some reason it stuck, and does not keep executing for no reason, Any ideas?