day_21作业

今日作业

'''
1、定义MySQL类(参考答案:http://www.cnblogs.com/linhaifeng/articles/7341177.html#_label5)
	  1.对象有id、host、port三个属性
	  
	  2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一
	  
	  3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化
	  
	  4.为对象定制方法,save和get_obj_by_id,save能自动将对象序列化到文件中,文件路径为配置文件中DB_PATH,文件名为id号,保存之前验证对象是否已经存在,若存在则抛出异常,;get_obj_by_id方法用来从文件中反序列化出对象'''


#settings.py内容
'''
HOST='127.0.0.1'
PORT=3306
DB_PATH=r'E:\CMS\aaa\db'
'''
from conf import settings
import uuid
import pickle
import os
class MySQL:
    def __init__(self,host,port):
        self.id=self.create_id()
        self.host=host
        self.port=port

    def save(self):
        if not self.is_exists:
            raise PermissionError('对象已存在')
        file_path=r'%s%s%s' %(settings.DB_PATH,os.sep,self.id)
        pickle.dump(self,open(file_path,'wb'))

    @property
    def is_exists(self):
        tag=True
        files=os.listdir(settings.DB_PATH)
        for file in files:
            file_abspath=r'%s%s%s' %(settings.DB_PATH,os.sep,file)
            obj=pickle.load(open(file_abspath,'rb'))
            if self.host == obj.host and self.port == obj.port:
                tag=False
                break
        return tag
    @staticmethod
    def get_obj_by_id(id):
        file_abspath = r'%s%s%s' % (settings.DB_PATH, os.sep, id)
        return pickle.load(open(file_abspath,'rb'))

    @staticmethod
    def create_id():
        return str(uuid.uuid1())

    @classmethod
    def from_conf(cls):
        print(cls)
        return cls(settings.HOST,settings.PORT)

# print(MySQL.from_conf) #<bound method MySQL.from_conf of <class '__main__.MySQL'>>
conn=MySQL.from_conf()
conn.save()

conn1=MySQL('127.0.0.1',3306)
conn1.save() #抛出异常PermissionError: 对象已存在


obj=MySQL.get_obj_by_id('7e6c5ec0-7e9f-11e7-9acc-408d5c2f84ca')
print(obj.host)
'''2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放
		参考答案(http://www.cnblogs.com/linhaifeng/articles/7340801.html#_label4)'''

import math


class Circle:
    def __init__(self, r):
        self.__r = r

    @property
    def area(self):
        return math.pi * self.__r ** 2

    @property
    def perimeter(self):
        return 2 * math.pi * self.__r

    @property
    def get_r(self):
        return self.__r


c = Circle(10)
print(c.get_r)
print(c.area)
print(c.perimeter)
'''3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类'''

import abc #利用abc模块实现抽象类

class Phone(metaclass=abc.ABCMeta):
    @abc.abstractmethod #定义抽象方法,无需实现功能
    def phone_name(self):
        pass

    @abc.abstractmethod #定义抽象方法,无需实现功能
    def phone_price(self):
        '子类必须定义写功能'
        pass

class iPhone(Phone): #子类继承抽象类,但是必须定义方法
    def phone_name(self):
        print('苹果手机 11')

    def phone_price(self):
        print('价格一万')


class huawei(Phone): #子类继承抽象类,但是必须定义方法
    def phone_name(self):
        print('华为手机 pr30')
    def phone_price(self):
        print('价格两万')

i1 = iPhone()
h1 = huawei()

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



posted @ 2019-10-11 15:39  mqb11  阅读(111)  评论(0)    收藏  举报