-1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Anon Omiss
  • 117
  • 1
  • 1
  • 6
  • 1
    If you would like your code reviewed, I suggest you use [Code Review Stack Exchange](https://codereview.stackexchange.com/). – martineau Jun 01 '21 at 17:32
  • Thanks. Not at this stage no, I've just been coding on my own and thought some other people's input might be a good idea as I have no one at the moment to bounce ideas off or talk things through. So many of the search results on Python problems brings me to Stack Overflow, so I've spent plenty of time reading stack overflow and have for many other troubleshooting problems I've had in the past for web design etc., and not just Python. I just haven't been posting questions or answers... My first post is clearly not well received, so perhaps I may not post much here... Thanks – Anon Omiss Jun 01 '21 at 21:41

1 Answers1

2

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.

eternal_student
  • 626
  • 4
  • 18
  • Right on that's great. That will be much better for the KeyError problem. Thanks for the help. :) – Anon Omiss Jun 01 '21 at 17:57
  • If you like my answer, could you please `accept` it? (You should see a check-mark underneath the downvote arrow) – eternal_student Jun 01 '21 at 18:27
  • 1
    Yes, I will. I just want to see if anyone will post regarding the index problem... I shouldn't have put two questions in the same post that's my bad. – Anon Omiss Jun 01 '21 at 20:31
  • Im guessing I can just the len option in a for loop so Im sure Ill be able to figure it out... Just wanted to get some different opinions or ideas as Im working alone and dont want bloated archaic code, but I phrased my question incorrectly and this may not be the best forum for that... Never the less the .get solution is great and I will now mark this checked... Thanks – Anon Omiss Jun 01 '21 at 20:36
  • I don't see a good description on that "index problem" you mention. Can you create a new question for it? – eternal_student Jun 02 '21 at 13:19
  • Hi, yes thanks for the help... I could but I ended up figuring it out anyways. I moved into Spyder and was able to figure every issue out I had and now the program can just be imported and it returns my analysis... Now Im dealing with another problem of companies being too new or too small to have the data for the particular api's and data feeds I'm using which is causing a different error... If I can't figure it out I will post a question and make it better than this one... Thanks – Anon Omiss Jun 03 '21 at 15:00