zoukankan      html  css  js  c++  java
  • day9-Python学习笔记(二十)数据库备份,修改父类的方法

    数据库备份,修改父类的方法

    import os,datetime
    class BakDb(object):
    def __init__(self,ip,username,passwd,port=3306,path='/tmp/db_bak'):
    self.ip = ip
    self.username = username
    self.passwd = passwd
    self.port = port
    self.path = path
    self.path_exist()
    self.bak_db()
    def path_exist(self):
    if not os.path.isdir(self.path): #不存在的话就创建
    os.mkdir(self.path)
    def bak_db(self):
    filename = str(datetime.date.today())+'.sql'
    abs_file = os.path.join(self.path,filename) #变成绝对路径
    command = '''
    mysqldump -u{username} -p{passwd} -P{port} -h{ip} -A > {filename}
    '''.format(username=self.username,
    passwd=self.passwd,
    port=self.port,
    filename=abs_file,
    ip=self.ip)
    os.system(command)
    print('done!数据库备份完成!')


    #db1 = BakDb('192.168.12.1','root','123456')


    修改父类的方法

    import redis
    class Coon(object):
    #基类
    def __init__(self,host,passwd,port):
    self.host= host
    self.passwd=passwd
    self.port= port

    class CoonMySql(Coon):
    def __init__(self,host,passwd,port,username,db,charset='utf8'):
    #Coon.__init__(self,host,passwd,port) #z在调用父类的构造方法,咱们自己手动调用父类的方法
    super(CoonMySql,self).__init__(host,passwd,port) #super会自动找到父类,然后调用下面的方法
    self.username=username
    self.db=db
    self.charset=charset
    def coon_mysql(self):
    print(self.host,self.host)





    class CoonRedis(Coon):
    def conn(self,host,passwd,port):
    Coon.conn(self, host, passwd, port)
    redis.Redis(host=self.host,password=self.passwd,port=self.port)




  • 相关阅读:
    《浪潮》影评
    《白日梦想家》影评笔记
    mac清理磁盘方法
    java性能监控器VisualVM
    python部署工具fabric
    linux缓存nscd
    ajax取返回值的方法
    python中__name__ = '__main__' 的作用
    使用nsswitch控制linux dns解析顺序
    SQLAlchemy中filter()和filter_by()的区别
  • 原文地址:https://www.cnblogs.com/flynn0825/p/8519405.html
Copyright © 2011-2022 走看看