zoukankan      html  css  js  c++  java
  • python_day7 综合联系

    练习:定义MySQL类

      1.对象有id、host、port三个属性

      2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一

      3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化

      4.为对象定制方法,save和get,save能自动将对象序列化到文件中,文件名为id号,文件路径为配置文件中DB_PATH;get方法用来从文件中反序列化出对象

    ###################################

    ort pickle
    import hashlib
    import time
    import set1
    import os,sys
    PAT=(os.path.dirname(__file__))
    lib_path=PAT+r'/lib'
    class Mysql:
    def __init__(self,host,port):
    self.host=host
    self.port=port
    self.id=self.create_id()
    def __str__(self):
    return self.id
    def save(self):
    write_file=lib_path+r'/%s' %(self.id)
    # print(write_file)
    pickle.dump(self,open(write_file,'wb'))

    def get(self):
    write_file=lib_path+r'/%s' %(self.id)
    JG=pickle.load(open(write_file,'rb'))
    print(JG.host,JG.port,JG.id)
    @staticmethod
    def create_id():
    m5=hashlib.md5(str(time.clock()).encode('utf-8'))
    return m5.hexdigest()
    @classmethod
    def conn(cls):
    return cls(set1.Host,set1.Port)
    A1=Mysql('192.168.1.1','3306')
    A1.save()
    A1.get()
    A2=Mysql.conn()
    A2.save()
    A2.get()
  • 相关阅读:
    clearInterval,setInterval,clearTimeout,setTimeout
    input的onkeyup效果 超级简短代码
    yourphp点击刷新验证码
    收藏的插件
    js创建和获取cookie
    js blind使用
    js控制网页滚动条往下滚动
    document.body.scrollTop用法
    yourphp问题中心
    yourphp数据库介绍
  • 原文地址:https://www.cnblogs.com/onda/p/7009133.html
Copyright © 2011-2022 走看看