0

Question: How to call a different functions within stat_frac_sales() based on its argument stat_requested?

I want to calculate a financial ratio that will have different numerators (earnings, gross profit, etc...) but always the same denominator (sales). It would look like something like that:

def stat_frac_sales(company, input1, input2, stat_requested):
    stat_data =  # call a function here based on the value of stat_requested
    sales = import_sales(company) # Sales function that will not vary

    ratio= stat_data/sales

    return ratio

For instance, if stat_requested = 'FCT', then the function FCT() should be called.

For example FCT and EBIT function look like:

def FCF(company,  input1)
   ...some code
   return FCF

def EBIT(company, input2)
   ...some different code
   return EBIT

So if the user call FCF (stat_data='FCF'), it would look something like that:

def stat_frac_sales(company, input1, input2, stat_requested):
    stat_data = FCF(company, input1) # <-- like so but unclear as to how to make this dynamic, where the function called here changes based on stat_requested
    sales = import_sales(company) # Sales function that will not vary*

    ratio= stat_data/sales

    return ratio

Hope this is clear. Any idea on how to do that will be greatly appreciated.

Ralf
  • 16,086
  • 4
  • 44
  • 68
Maxime
  • 13
  • 2
  • sorry I am not following your question. If you want a user-input then simply just use stat_data = input('enter a earning, gross profit, etc value'), but then you don't need to pass in a stat_data argument into your stat_frac_sales function. Whenever you call your stat_frac_sales function in your code, the user will be prompted with a input – PydPiper Jul 22 '18 at 14:54
  • Realise this was not clear. Hope this is better now. Thanks a lot for your help! Much appreciated – Maxime Jul 22 '18 at 17:32
  • Your code is clearer now, but I am still not following what the error/problem is? As long as your function are defined above stat_frac_sales, they are fair game to be used downstream by your stat_frac_sales function. Also as long as FCF, EBIT, and import_sales returns a int or float then you should be good to go – PydPiper Jul 22 '18 at 17:45
  • 1
    Thanks for your patience PydPiper. I wasn't sure about the best way to dynamically adapt the code depending on what the user sets up for stat_requested. For instance, if stat_requested=FCF, the code should become stat_data =FCF(company, start, end, input1). I thought about implementing a "if, elif" but not sure how this scales if you have 50+ possible ratios. Would look something like: if stat_data =='FCF': stat_data =FCF(company, start, end, input2) elif stat_data =='EBIT': stat_data =EBIT(company, start, end, input1) ...etc.... Maybe a dictionary might help. Thanks a lot! – Maxime Jul 22 '18 at 18:59
  • oh! yeah for sure, you are on the right path with dictionary. I'll type something up in a sec, but for now I think you have a typo in your bottom code def stat_frac_sales(company, input1, input2, stat_data): I think you mean to say stat_requested instead of stat_data – PydPiper Jul 22 '18 at 19:20
  • You're absolutely right. Thanks for spotting it! – Maxime Jul 22 '18 at 19:22
  • 1
    so not sure how to remove the question from [hold] but ill just type the answer in here: def stat_frac_sales(company, input1, input2, stat_requested): funcs = {'FCF': FCF, 'EBIT': EBIT} stat_data = funcs[stat_requested](company, input1) sales = import_sales(company) # Sales function that will not vary* ratio= stat_data/sales return ratio – PydPiper Jul 22 '18 at 19:23
  • @PydPiper the question has been removed from the hold queue; you can now post your solution as an answer. – alkasm Jul 23 '18 at 08:58

0 Answers0