0

I need to execute the class name with functions inside it and execute the functions inside the class individually on run time by passing arguments.

Python code saved as test.py

import sys 
import threading
from threading import Thread

class test():
  def func1(self):
     print('hi im func1')
  def func2(self):
     print('hi im func2')
  def func3(self):
     print('hi im func3')

  def runall(self):
    if __name__ == '__main__':
        Thread(target=self.func1).start()
        Thread(target=self.func2).start()
        Thread(target=self.func3).start()

 if __name__ == '__main__':
    try:
        run = test()
        run.runall()
        globals()[sys.argv[1]]()
    except KeyError:
         raise KeyError('Invalid Function Name Passed in Argument! refer the code for valid Name.')
  1. Trying to execute all functions inside class:

    Runtime Execution: c:\ > python test.py test

    Passed but gave error on
    File "test.py", line 180, in globals()sys.argv[2] TypeError: 'test' object is not callable

  2. Trying to execute only particular functions inside the class

    Runtime Execution: c:\ > python test.py func1 Keyerror is getting thrown.

Can someone guide me on how to execute complete class and individual functions inside the class at runtime?

Mia
  • 448
  • 8
  • 22
  • What does compute() function do? Where's the definition? – ElmoVanKielmo Oct 17 '19 at 11:52
  • Sorry!! My mistake changed the name to test – Mia Oct 17 '19 at 12:01
  • It's not really clear what you want here. First you call `run.runall()` which unconditionally starts three threads, one for each of the methods in your class. Then you look up sys.argv` in `globals()` which is probably not what you want, and you call that. The threading seems to be irrelevant to your question. Please try to narrow down your problem to a [Minimal](https://stackoverflow.com/help/minimal-reproducible-example) example specifying exactly what behavior you expect. – Iguananaut Oct 17 '19 at 12:15
  • @Iguananaut, Thanks! , My Problem with code is, I need to run the class name and individual function name as run time arguments. Where I need some help! – Mia Oct 17 '19 at 12:17
  • You need to understand that run.runall() is called unconditionally in your code. Secondly methods of class test are not accessible directly by their names but run.func1() would work instead of func1(). Luckily for you members names of class test are keys of special dictionary, so run.__dict__[sys.argv[1]]() would work with method names as command line arguments. Instead of using test argument you should use runall argument. – ElmoVanKielmo Oct 18 '19 at 07:24

2 Answers2

1

The first step works by me (on python 3.7.2)

    > python3 test.py test
    hi im func1
    hi im func2
    hi im func3

However this is triggered by the run.runall() statement. What version of Python do you run? don't you have another test variable in your work-space ?

For the second point, solution inspired by here, you could get the individual methods of the class running like this:

 if __name__ == '__main__':
    try:
        run = test()
        run.runall()
        #globals()[sys.argv[1]]()
        getattr(test(), sys.argv[1])()

    except KeyError:
         raise KeyError('Invalid Function Name Passed in Argument! refer the code for valid Name.')

result:

> python3 test.py func1
hi im func1
hi im func2
hi im func3
hi im func1
rang
  • 36
  • 4
  • yes complete function run is working but I am stuck in individual function execution! I use python 3.7.2 – Mia Oct 17 '19 at 12:15
  • Thanks ! but It is executing all functions as well. Which should be avoided. Is there any way to make a call only the function execution. – Mia Oct 17 '19 at 12:27
  • Well your question title starts with "How can I execute all functions..." :) If it shall be avoided then just get rid of `run.runall()` (and the statement before that become useless) – rang Oct 17 '19 at 12:37
  • I had already added 'and' in the question as well which I thought will denote both ways of execution. – Mia Oct 18 '19 at 03:37
0

It worked for me Now I can Execute all Functions and separate functions individually. Thanks All For Your support and guide!!

  if __name__ == '__main__':
    try:
        if sys.argv[2]=='test':
             run = test()
             run.runall()
        else:
            getattr(test(), sys.argv[2])()

    except KeyError:
         raise KeyError('Invalid Function Name Passed in Argument! refer the code for valid Name.')
Mia
  • 448
  • 8
  • 22