-1

I use globals() to create p1,p2,...variables. However when I call these variables, the code analysis gives me error hint saying those variables are non-existence. It seems like the code analysis does not recognize the variables not created explicitly. Although the code can run without any problem, but I still want to get ride of them. The code is as:

def save_new_element(self,ui_main_window,dp,pltr,selected_module):
    
    # assign factors for analysis
    for i in range(27):
        if i < 9: # comboBox 1~9
            globals()[f'p{i+1}'] = eval('ui_main_window.comboBox_%d_prsnlf' % (i+1)).currentText() 
            
        else: # comboBox 14~31
            globals()[f'p{i+1}'] = eval('ui_main_window.comboBox_%d_prsnlf' % (i+5)).currentText()
    
    conn = sqlite3.connect('data.db')
    
    c = conn.cursor()
    
    c.execute("SELECT 员工编号 FROM personel WHERE 员工编号 = (?)",(ui_main_window.lineEdit_9_prsnlf.text(),))
   
    t1 = c.fetchall()
   
    if len(t1) > 0: # record exists, delete first

        c.execute("DELETE from personel WHERE 员工编号 = (?)",(ui_main_window.lineEdit_9_prsnlf.text(),))
 
    c.execute("""INSERT INTO personel VALUES(?,?,?,?,?,?,?,?,
                                          ?,?,?,?,?,?,?,?,
                                          ?,?,?,?,?,?,?,?,
                                          ?,?,?,?,?,?,?,?,
                                          ?,?,?,?,?,?,?,?,?)
                """,
             (ui_main_window.lineEdit_1_prsnlf.text(),ui_main_window.lineEdit_2_prsnlf.text(),
             ui_main_window.lineEdit_3_prsnlf.text(),ui_main_window.lineEdit_4_prsnlf.text(),
             ui_main_window.lineEdit_5_prsnlf.text(),ui_main_window.lineEdit_6_prsnlf.text(),
             ui_main_window.lineEdit_7_prsnlf.text(),ui_main_window.lineEdit_8_prsnlf.text(),
             ui_main_window.lineEdit_9_prsnlf.text(),
             p1,p2,p3,p4,p5,p6,p7,p8,p9,
             ui_main_window.comboBox_10_prsnlf.currentText(),ui_main_window.comboBox_11_prsnlf.currentText(),
             ui_main_window.comboBox_12_prsnlf.currentText(),
             ui_main_window.comboBox_13_prsnlf.currentText(),
             p10,p11,p12,p13,p14,p15,p16,p17,p18,
             p19,p20,p21,p22,p23,p24,p25,p26,p27,
             ui_main_window.comboBox_32_prsnlf.currentText()))

    Rvalue,basic1,b1,b4,capacity1,pyhsi1,psych1,behave1 = dp.calculate_values_personel(p3,p4,p5,p6,p7,p8,p9,p2,p1,p10,p11,p12,p13,p14,p15,
                                                                                        p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27)
                                                          
    self.alertSounds(Rvalue)
    ui_main_window.AGwidget_prsnlf.updateValue(Rvalue)
    self.temp_stat_prsnlf.loc[0] = [Rvalue,basic1,b1,b4,capacity1,pyhsi1,psych1,behave1,Rvalue]
    pltr.plotRadar(ui_main_window,self,dp,selected_module) 
    ui_main_window.label_radar_prsnlf.setPixmap(QtGui.QPixmap(self.pic_dir_prsnlf))                  
    
    conn.commit()
    conn.close()

This picture can better illustrate what my problem is.

Jason Gao
  • 1
  • 2
  • 2
    "I use globals() to create p1,p2,...variables." you **really really shouldn't**. "the code analysis gives me error hint saying those variables are non-existence." Of course. "It seems like the code analysis does not recognize the variables not created explicitly" How could it? Listen, you cannot have your cake and eat it too. Either just ignore the static analysis tools or ideally, just don't use `globals()` to create variables. – juanpa.arrivillaga May 26 '22 at 03:06
  • 1
    Please include your code as text and not as an image. – ewong May 26 '22 at 03:07
  • 2
    I'm guessing whatever linter you are using does not expect you to do something that is such an anti-pattern. You should be using a proper data structure like a dictionary or a list for this. Instead of `p1`,`p2`, etc. you should have a list `p` and `p[0]`, `p[1]`, etc. – Mark May 26 '22 at 03:07
  • 2
    Looking at your code, you should simply be using a `list`. – juanpa.arrivillaga May 26 '22 at 03:07
  • 2
    You yourself need to know about the names of your variables, your code inherently 'knows' the names of your variables, but the second you start writing code that creates data describing the names of your variables, you've gone places you rarely need to go unless you're writing an IDE or a compiler or something of the sort. – Grismar May 26 '22 at 03:09
  • [Please do not upload images of code or errors when asking a question.](//meta.stackoverflow.com/q/285551) Also, make sure you understand the difference between exceptions raised by Python, and messages that come from your editing tools. – Karl Knechtel May 26 '22 at 03:26

0 Answers0