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()
  • 相关阅读:
    NOIP模拟 回文序列
    NOIP模拟 最佳序列
    BZOJ 2783 树
    BZOJ 3631 松鼠的新家
    洛谷 6道简单图论
    NOI模拟 颜色
    NOIP模拟 Game
    BZOJ1415 聪聪与可可
    Codeforces Round #443 (Div. 2) C: Short Program
    20171028洛谷NOIP模拟
  • 原文地址:https://www.cnblogs.com/onda/p/7009133.html
Copyright © 2011-2022 走看看