Search code examples
pythonstockkeyerrorstockquotes

Is there a cleaner way to write this code and how would you get around keyerrors and list index errors?


I'm fairly new to actually coding Python.

I'm working on a stock program for myself as my first Python challenge.

After 3000+ lines of code and a 9-second delay to print my analysis, I have finished the basic code to analyze one stock.

I'm working in Jupyter Notebook at the moment and all of my code is in the same workbook, so not importing modules or workbooks.

Now I was able to write the whole code for one company that has been on the index for a while, however, I am running into some problems with newer companies returning None values and Index not in the list.

I've been able to solve every problem thus far however it may be time to seek some input, as I'm fairly certain my code is already bloated and there has to be a lot better way to write it than what I currently have.

For instance, I have code like this

stock_eps = get_earnings_history('STOCK')
stock_latest_eps4 = stock_eps[4]
stock_latest_eps4_rate = stock_latest_eps4["epsactual"]
stock_latest_eps4_date = stock_latest_eps4["startdatetime"]

stock_eps is a list.

I'm using this to:

  1. print the earning per share and the date and
  2. to return a final integer that I can later use in if statements and calculations.

I need the end variable to be an integer or float which is why it looks like this.

While this works great for a company that has enough years on the index, for those that don't it returns a list index out of range.

I need this to be a base template for every company regardless of what it returns but with a solution that doesn't return the error so that I don't have to fix every single file based on the company.

So I would need each variable to have an actual figure in it that doesn't break.

On the KeyError problem I did the following:

For instance, I am importing some data such as:

stock.info

which returns a dictionary, sweet.

Then I'm doing something like this

mapping = stock.info
stock_sec = mapping['sector'] #Company Sector

So I'm running into a problem with newer companies not having a key-value integer such as say Trailing P/E

I solved it by doing this.

if isinstance(stock_tep, type(None)):
    stock_tep = "-1"
else:
    pass

This works however I've then created a variable with a 0 or -1 value which isn't accurate and therefore can skew the analysis as many variables are used later for if statements where if 0 it equals a certain number which might not be accurate. I can solve this later by adding more code when I want to use that variable but there has to be a better way to deal with this issue?


Solution

  • When examining a dictionary which might or might not have a certain key, you can avoid KeyError by using get method:

    stock_latest_eps4_date = stock_latest_eps4["startdatetime"]
    

    vs

    stock_latest_eps4_date = stock_latest_eps4.get("startdatetime")
    

    vs

    stock_latest_eps4_date = stock_latest_eps4.get("startdatetime", default_value)
    

    If dictionary stock_latest_eps4 contains key startdatetime, all three will have exact same effect.

    If stock_latest_eps4 does not contains key startdatetime, then the first one will raise exception, the second one will give you None, and the last one will give you the value of default_value variable.