• day21作业


    1、定义MySQL类

    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方法用来从文件中反序列化出对象
    
    # 功能主题代码
    
    import settings
    import uuid
    import pickle
    import os
    
    
    class MySQL:
        def __init__(self, host, port):
            self.host = host
            self.port = port
            self.id = self.create_id
    
        @property
        def create_id(self):
            return str(uuid.uuid1())
    
        @property
        def is_exists(self):
            tag = True
            files = os.listdir(settings.DB_PATH)
            for file in files:
                file_path = os.path.join(settings.DB_PATH, file)
                # print(file_path)
                with open(file_path, 'rb') as fr:
                    obj = pickle.load(fr)
                    if self.host == obj.host and self.port == obj.port:
                        tag = False
                        break
            return tag
    
        def save(self):
            if self.is_exists:
                path = os.path.join(settings.DB_PATH, str(self.id))
                with open(path, 'wb')as fw:
                    pickle.dump(self, fw)
                print('保存成功!')
            else:
                print('对象已存在')
    
        def get_obj_by_id(id):
            path = os.path.join(settings.DB_PATH, id)
            if os.path.exists(path):
                with open(path, 'rb')as fr:
                    obj = pickle.load(fr)
                    return obj
            else:
                print('文件不存在')
    
        @property
        def from_conf(self):
            return settings.HOSTS, settings.PORT
    
    
    mysql1 = MySQL("127.0.0.1","3306")
    id = mysql1.create_id
    print(id)
    mysql1.save()
    obj = MySQL.get_obj_by_id('eeebefac-ec27-11e9-840a-701ce72cb960')
    print(obj)
    

    2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放

    import math
    
    
    class Circle:
        def __init__(self, radius):
            self.__radius = radius
    
        @property
        def area(self):
            return math.pi * self.__radius ** 2
    
        @property
        def perimeter(self):
            return 2 * math.pi * self.__radius
    
    
    c = Circle(10)
    # print(c._Circle__radius)
    
    print(c.area)
    print(c.perimeter)
    
    '''
    314.1592653589793
    62.83185307179586
    '''
    

    3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类

    import abc
    
    
    class Phone(metaclass=abc.ABCMeta):
        @abc.abstractmethod
        def call(self):
            print('打电话')
    
        @abc.abstractmethod
        def send_msg(self):
            print('发消息')
    
        @abc.abstractmethod
        def play_games(self):
            print('玩游戏')
    
    
    class Smartisan_OS(Phone):
        def call(self):
            print('打电话')
    
        def send_msg(self):
            print('发信息')
    
        def play_games(self):
            print('玩游戏')
    
    p1 = Smartisan_OS()
    p1.call()
    
    
  • 相关阅读:
    python 01
    Node.js 基础库
    Node 编程规范
    Linux_异常_08_本机无法访问虚拟机web等工程
    inux_异常_07_ftp查看不到文件列表
    Linux_异常_04_ftp: command not found...
    Linux_异常_03_Failed to restart iptables.service: Unit not found.
    Linux_异常_02_WinSCP上传文件时显示Permission denied
    Linux_异常_01_CentOS7无法ping 百度
    Linux_配置_02_配置dns
  • 原文地址:https://www.cnblogs.com/setcreed/p/11656958.html
Copyright © 2020-2023  润新知