day21作业

Posted on 2019-10-11 16:48  猪宝店幼儿园  阅读(70)  评论(0编辑  收藏  举报
import hashlib
import time
import pickle
import os
import setting
class MySQL:
    def __init__(self, host, port):
        self.__host = host
        self.__port = port
        self.__id = self.create_id()
    @staticmethod
    def create_id():
        m = hashlib.md5()
        m.update(str(time.clock()).encode('utf-8'))
        return m.hexdigest()
    @classmethod
    def creae_mysql(cla):
        return cla(setting.HOST, setting.PORT)
    def save(self):
        path = os.path.join(setting.DB_PATH, '%s.pk' % self.__id)
        if os.path.exists(path):
            raise ImportError('该id已存在')
        else:
            with open(path, 'wb')as f:
                pickle.dump(self, f)
    def get_obj_by_id(self):
        path = os.path.join(setting.DB_PATH, '%s.pk' % self.__id)
        if not os.path.exists(path):
            raise ImportError('该id不存在')
            # print('该id不存在')
        else:
            with open(path, 'rb')as f:
                return pickle.load(f)
m1 = MySQL('h1', 'p1')
m2 = MySQL.creae_mysql()
m2.save()
print(m2.get_obj_by_id().__dict__)
m1.save()
m1.get_obj_by_id()
class Circle:
    def __init__(self,radius):
        self.__radius=radius
    @property
    def perimeter(self):
        return 2*(3.14)*self.__radius
    @property
    def area(self):
        return (3.14)*(self.__radius**2)

c1=Circle(3)
print(c1.area)
print(c1.perimeter)
import abc

class Phone(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def phone_name(self):
        pass

    @abc.abstractmethod
    def phone_price(self):
        '子类必须定义写功能'
        pass

class huawei(Phone):
    def phone_name(self):
        print('huaweimate30pro')

    def phone_price(self):
        print('5000')


class xiaomi(Phone):
    def phone_name(self):
        print('xiaomi9pro')
    def phone_price(self):
        print('4000')

i1 = huawei()
h1 = xiaomi()

i1.phone_name()
h1.phone_name()
i1.phone_price()
h1.phone_price()