-2

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?

1 Answers1

3

You're trying to overwrite your loop variable asset, which doesn't work in Python.

If you check your variable Assets after casting asset = Stock(asset), you'll see that it's just the strings and not the Stock instances you were trying to create. That's the reason you get the 'str' object has no attribute 'Analysis' error.

Instead, I'd do the following:

# Variables and class methods/properties should be lower-case
# Only classes are upper case
assets = ["stock_one", "stock_two", "stock_three"]

stocks = list(map(Stock, assets))
# The line above is (mostly) equivalent to
# stocks = [Stock(asset) for asset in assets]
# or to
# stocks = []
# for asset in assets:
#   stocks.append(Stock(asset))

while True:
  for stock in stocks:
    stock.analysis()
  time.sleep(10)
Daniel Lenz
  • 3,334
  • 17
  • 36
  • for some reason it stuck, and does not keep executing for no reason, Any ideas? (This analysis module needs to execute all together and then sleep, I changed only a bit) – user14384765 Apr 21 '21 at 09:50
  • I had mistakenly put the sleep-statement in the inner loop, not the outer one (it's corrected now). Could that be the issue? – Daniel Lenz Apr 21 '21 at 09:59
  • I also noticed that the generator I used to create `stocks` doesn't work if we iterate over `stocks` multiple times (fixed now). – Daniel Lenz Apr 21 '21 at 10:01