溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python使用反射進行oracle數(shù)據(jù)庫監(jiān)控

發(fā)布時間:2020-05-28 15:09:13 來源:億速云 閱讀:356 作者:鴿子 欄目:編程語言

需求:

因為最近在看python腳本腳本獲取oracle數(shù)據(jù)庫數(shù)據(jù)進行oracle數(shù)據(jù)庫監(jiān)控,需要用到反射的方式做,去通過傳參調(diào)用不同函數(shù)去獲取不同的數(shù)據(jù)庫狀態(tài)(python2)

#!/usr/bin/python
class Exucutsql(object):
    def status(self):
        print("active")
    def db(self):
        print("100M")
class Main(Exucutsql):
    def __init__(self):
        pass
    def __call__(self):
        method = raw_input("please input your method:")
        if hasattr(self,method):
            func = getattr(self, method)
            func()
        else:
            print("input method is not exits")
if __name__ == "__main__":
    Main()

說明:輸入相應(yīng)方法名會調(diào)用不用的方法

python類中的特殊方法

__call__() 方法會當作一個函數(shù)去執(zhí)行

hasattr() 方法會判斷在類中是否存在方法

getattr() 方法會去調(diào)用執(zhí)行相應(yīng)的函數(shù)名方法(解決了過多的IF判斷的問題)

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI