I am trying to find an efficient and secure way to call different functions based on the transaction name the user enters. There are a 100+ different transactions. A 100 "IF" would do the job, however, I want to find a more efficent way to call the transaction. The "eval" would do it, but I read that this should not be used, as the user can enter any transaction name.
from operator import methodcaller
import sys
from PyQt5.QtWidgets import (QMainWindow,QToolBar,QLineEdit,
QLabel, QApplication)
def one():
print ("1")
def two():
print ("2")
def three():
print("3")
class main_menu(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
menuBar = self.menuBar()
self.ToolBar = QToolBar()
self.ToolBar.setMovable(False)
self.addToolBar(self.ToolBar)
self.tcode = QLineEdit(maxLength=5)
self.tcode.returnPressed.connect(self.tcode_action)
self.ToolBar.addWidget(QLabel(" Transaction : "))
self.ToolBar.addWidget(self.tcode)
def tcode_action(self):
## if self.tcode.text() == "one":
## one()
## if self.tcode.text() == "two":
## two()
## if self.tcode.text() == "three":
## three()
## eval(self.tcode.text()+"()")
def main(args):
app = QApplication(sys.argv)
mm = main_menu()
mm.show()
sys.exit(app.exec_())
if __name__=="__main__":
main(sys.argv)