为什么代码在使用函数时会改变行为?

2024-09-30 18:31:04 发布

您现在位置:Python中文网/ 问答频道 /正文

背景: 我从两个数据库中获取数据并比较值,当它们不同时,我将它们打印出来。另外,如果“diagnostic\u dest”的值不包含Oracle\u base path,那么它也会被打印出来。现在的挑战是没有命令行参数的代码工作完美,一旦我使用命令行的输出似乎错过了一些迭代。见下文附件

使用下面的标准代码:

import cx_Oracle
import smtplib
import os, sys
import string
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
conn_string = cx_Oracle.connect('hr/orpassword@xe') 
cursor1=conn_string.cursor()
cursor2=conn_string.cursor()
cursor1.execute("SELECT p.name,p.value FROM   v$parameter p  ORDER BY p.name")
results1 = cursor1.fetchall()
cursor2.execute("SELECT p.name,p.value FROM   v_param p  ORDER BY p.name")
results2 = cursor2.fetchall()
orabase=''
with open('file.txt', 'w') as f:
    # print('hello world', file=f)
    for index in range(0,len(results1)):
       if (results1[index][0]=='audit_file_dest'):
          orabase=results1[index][1].rsplit("\ADMIN")[0]
       if (results2[index][0]=='diagnostic_dest' and results2[index][1].__contains__(orabase)):
          continue
       elif (results2[index][0]=='diagnostic_dest'):
          print ('Diagnostic_dest does not contain ORACLE_BASE',orabase, file=f)
          print("####################################################################", file=f)
       if (results2[index] == results1[index]):
          continue
       print ('Drift found on ',conn_string.tnsentry," - ",results1[index],":",conn_string.tnsentry," - ",results2[index], file=f)

输出:

Diagnostic_dest does not contain ORACLE_BASE C:\ORACLEXE\APP\ORACLE
####################################################################
Drift found on  xe  -  ('diagnostic_dest', 'C:\\ORACLEXE\\APP\\ORACLE') : xe  -  ('diagnostic_dest', '+FRA')
Drift found on  xe  -  ('shared_pool_size', '0') : xe  -  ('shared_pool_size', '5')
Drift found on  xe  -  ('streams_pool_size', '0') : xe  -  ('streams_pool_size', '8')

使用下面的命令行参数代码:

import cx_Oracle
import os, sys
import string



def DriftsCheck(puser,ppswd,psid,duser,dpswd,dsid):
    conn_string1=cx_Oracle.connect(puser+'/'+ppswd+'@'+psid)
    conn_string2=cx_Oracle.connect(duser+'/'+dpswd+'@'+dsid)

    cursor1=conn_string1.cursor()
    cursor2=conn_string2.cursor()
    # try :
    cursor1.execute("SELECT p.name,p.value FROM   v$parameter p  ORDER BY p.name")
    results1 = cursor1.fetchall()
    cursor2.execute("SELECT p.name,p.value FROM   v_param p  ORDER BY p.name")
    results2 = cursor2.fetchall()
    orabase=''
    with open('file.txt', 'w') as f:
    # print('hello world', file=f)
        for index in range(0,len(results1)):
           if (results1[index][0]=='audit_file_dest'):
              orabase=results1[index][1].rsplit("\ADMIN")[0]
           if (results2[index][0]=='diagnostic_dest' and results2[index][1].__contains__(orabase)):
              continue
           elif (results2[index][0]=='diagnostic_dest'):
              print ('Diagnostic_dest does not contain ORACLE_BASE',orabase, file=f)
              print("####################################################################", file=f)
           if (results2[index] == results1[index]):
              continue
           print ('Drift found on ',conn_string1.tnsentry," - ",results1[index],":",conn_string2.tnsentry," - ",results2[index], file=f)

           return
    # except:
       # print ('An errors was encountered')        

if __name__ == '__main__':
    puser= sys.argv[1]
    ppswd= sys.argv[2]
    psid= sys.argv[3]
    duser= sys.argv[4]
    dpswd= sys.argv[5]
    dsid= sys.argv[6]
    DriftsCheck(puser,ppswd,psid,duser,dpswd,dsid)

命令行输出的输出:

$ python python_tut/DriftsCheck.py hr orpassword xe hr orpassword xe
Drift found on  xe  -  ('diagnostic_dest', 'C:\\ORACLEXE\\APP\\ORACLE') : xe  -  ('diagnostic_dest', '+FRA')

Tags: nameimportstringindexifsysconnfile