0

I was looking through many posts here for a solution but maybe I was searching for the wrong phrase. I'm new to Python, my code problem is the following:

from yahoo_finance import Share
for line in open('fcts.txt'):
   yahoo = Share('YHOO')
   print (yahoo +'.' + line)

it should basically do the following but for every function inside fcts.txt:

from yahoo_finance import Share
yahoo = Share('YHOO')
print (yahoo.get_open())

while fcts.txt contains different functions like

get_open()
get_change()
...
3
  • I can't understand your question. What is in fcts.txt? Python functions? Commented Mar 7, 2017 at 11:22
  • Names of Python methods of a Share instance. Commented Mar 7, 2017 at 11:22
  • 3
    Why do you have sort of weird half-code in your txt file…? Doesn't seem very sane. Commented Mar 7, 2017 at 11:26

1 Answer 1

1

You can access methods by name like this:

from yahoo_finance import Share

with open('fcts.txt') as methodsFile:
  for methodName in methodsFile:
    yahoo = Share('YHOO')
    method = getattr(yahoo, methodName.strip())
    print (method())

But it looks rather crude to me.

Sign up to request clarification or add additional context in comments.

2 Comments

Your fcts.txt should contain lines like get_open and get_change without the parentheses, though. If you have parentheses in the file, use methodName.strip('() \n') instead.
Perhaps this answer should reinforce that while this technically works, it is completely insane to put your code in a data file. You want things the other way around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.