• day 21 作业


    定义MySQL类

    • 对象有id、host、port三个属性
    • 定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一
    • 提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化
    • 为对象定制方法,save和get_obj_by_id,save能自动将对象序列化到文件中,文件路径为配置文件中 DB_PATH,文件名为id号,保存之前验证对象是否已经存在,若存在则抛出异常,get_obj_by_id方法用来从文件中反序列化出对象
    Copyfrom 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 self.is_exists:
                raise PermissionError('对象已存在')
            file_path = f'{settings.DB_PATH}{os.sep}{self.id}'
            pickle.dump(self, open(file_path, 'wb'))
    
        @property
        def is_exists(self):
            tag = False
            files = os.listdir(settings.DB_PATH)
    
            for file in files:
                file_abspath = f'{settings.DB_PATH}{os.sep}{file}'
                obj = pickle.load(open(file_abspath, 'rb'))
                if self.host == obj.host and self.port == obj.port:
                    tag = True
                    break
    
            return tag
    
        @staticmethod
        def get_obj_by_id(id):
            file_path = f'{settings.DB_PATH}{os.sep}{id}'
            return pickle.load(open(file_path, 'rb'))
    
        @staticmethod
        def create_id():
            return str(uuid.uuid1())
    
        @classmethod
        def from_conf(cls):
            print(cls)
            return cls(settings.HOST, settings.PORT)
    
    
    # conn = MySQL.from_conf()
    # conn.save()
    
    
    
    obj = MySQL.get_obj_by_id('504ab0f6-ec21-11e9-b9e2-d053497faa26')
    print(obj.host)
    

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

    Copyfrom math import pi
    
    
    class Round:
        def __init__(self, radius):
            self.__radius = radius
    
        def perimeter(self):
            return round(2 * pi * self.__radius, 2)
    
        def area(self):
            return round(pi * (self.__radius ** 2), 2)
    
    
    r = Round(1)
    print(r.area())
    print(r.perimeter())
    print(r._Round__radius)
    

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

    Copyimport abc
    
    
    # 抽象类/父类
    class CellPhone(metaclass=abc.ABCMeta):
    
        @abc.abstractmethod
        def calling(self):
            pass
    
        @abc.abstractmethod
        def send_msg(self):
            pass
    
        
    # 现实类/子类
    class SmartPhone(CellPhone):
    
        def calling(self):
            print('calling...')
    
        # def send_msg(self):
        #     print('sending msg...')
    
        def app_download(self):
            print('downloading app...')
    
    
    iphone = SmartPhone()
    iphone.calling()
    iphone.send_msg()
    iphone.app_download()
    
  • 相关阅读:
    [kuangbin带你飞]专题十六 KMP & 扩展KMP & ManacherK
    [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher J
    [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher I
    pat 1065 A+B and C (64bit)(20 分)(大数, Java)
    pat 1069 The Black Hole of Numbers(20 分)
    pat 1077 Kuchiguse(20 分) (字典树)
    pat 1084 Broken Keyboard(20 分)
    pat 1092 To Buy or Not to Buy(20 分)
    pat 1046 Shortest Distance(20 分) (线段树)
    pat 1042 Shuffling Machine(20 分)
  • 原文地址:https://www.cnblogs.com/colacheng0930/p/11658175.html
Copyright © 2020-2023  润新知