温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python使用反射进行oracle数据库监控

发布时间:2020-05-28 15:09:13 来源:亿速云 阅读:348 作者:鸽子 栏目:编程语言

需求:

因为最近在看python脚本脚本获取oracle数据库数据进行oracle数据库监控,需要用到反射的方式做,去通过传参调用不同函数去获取不同的数据库状态(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()

说明:输入相应方法名会调用不用的方法

python类中的特殊方法

__call__() 方法会当作一个函数去执行

hasattr() 方法会判断在类中是否存在方法

getattr() 方法会去调用执行相应的函数名方法(解决了过多的IF判断的问题)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI