Home > Blockchain >  Concatenate Instance attribute using class with input
Concatenate Instance attribute using class with input

Time:01-19

I am super new to python so new to OOP and class (I am originally MATLAB user as an engineer...) so please teach me as much as possible. Anyways I am trying to do the following.

  1. Create a class called Stock - something like below

    class Stock :
          def __init__(self,estimate,earning)
              self.estimate = estimate       # estimation of quarterly earnings
              self.earning = earning         # actual quarterly earnings
    
    JPM(JP Morgan stock name) = Stock(11.7,10.9)
    
  2. However, the estimate and earning values are reported every quarter and I want to create a numerical vector for each. The idea is like below, but of course it does not work.

    JPM.estimate(1) = 11.9   # the second quarter earnings value at index 1 of the estimate
    JPM.estimate(2) = 12.1   # the third quarter earnings value at index 2 of the estimate
    JPM.estimate(3) = XX.XX  # and so on.
    

    Using .estimate(#) is just to show what I want to do. Using .append() or other methods you would like to teach me is fine.

The reason I am trying to do it this way is because I need 3 vectors for one stock(and I have about 1000 stocks so at the end I would have 3000 vectors to take care of). So I am planning on creating an instance of a stock and having 3 vectors as instance attributes. (Hopefully I got the terminology right.)

  1. earnings vector
  2. estimate vector
  3. the date those earnings were reported.

Am I using the class function wrong(as it was never intended to be used this way?) or what can I do to achieve such concatenation for instance attributes as the data are received from web scraping?

CodePudding user response:

It is not at all clear what you are trying to do with the Stock Class, but if all you want to do is create a list of stock price and earnings organized by date, you could do the following :

from collections import namedtuple, defaultdict

# Create a easily referenced tuple for defining staock data
StockData = namedtuple('StockData', ['date', 'earn', 'est']) 

class Stock:
    def __init__(self, data: StockData) -> None:
        self._quotes = defaultdict()
        self._quotes[data.date] = (data.earn, data.est)

    def add(self, data: StockData) -> None:
        self._quotes[data.date] = (data.earn, data.est)
        
    def value(self, date: str) -> tuple:
        # return tuple of (Earnings, Estimate) for date if it exists, else KeyError
        return self._quotes[date]

    def __repr__(self):
        return str(self._quotes)  

To load the stock class with data, you can do something along the lines of:

stk = Stock(StockData('1/20/2021', 123.5, 124.0))
stk.add(StockData('6/23/2021', 132.7, 119.4))

print(stk) yields:

defaultdict(None, {'1/20/2021': (123.5, 124.0), '6/23/2021': (132.7, 119.4)})

and, stk.value('1/20/2021')​ yields (123.5, 124.0)

  •  Tags:  
  • Related