# setting.py
import os
DB_PATH = os.path.dirname(__file__)
HOST_NAME = "127.0.0.1"
PORT = 3362
import time
import hashlib
import os
import pickle
import setting
import math
import abc
class MySQL():
def __init__(self, host, port):
self.host = host
self.port = port
self.id = self.create_id()
def create_id(self):
md5 = hashlib.md5()
value_for_id = self.host + str(self.port) + str(time.time())
md5.update(value_for_id.encode("utf-8"))
return md5.hexdigest()
def save(self):
file = os.path.join(setting.DB_PATH, self.id)
if os.path.exists(file):
print("文件已经存在")
raise FileExistsError
with open(file, "wb") as f:
pickle.dump(self, f)
@staticmethod
def get_obj_by_id(id):
file = os.path.join(setting.DB_PATH, id)
with open(file, "rb") as f:
return pickle.load(f)
@classmethod
def from_conf(cls):
return cls(setting.HOST_NAME, setting.PORT)
db = MySQL.from_conf()
db.save()
id = "5b84d6b4aece19b2ce38f78a6d620f17"
db1 = MySQL.get_obj_by_id(id)
print(db1.id)
class Circle():
def __init__(self, radius):
self.__radius = radius
@property
def circumference(self):
return math.pi * self.__radius * self.__radius
@property
def area(self):
return 2 * math.pi * self.__radius
c = Circle(4)
print(c.area)
print(c.circumference)
class Phone(metaclass=abc.ABCMeta):
@abc.abstractmethod
def call(self):
return self.call()
@abc.abstractmethod
def charge(self):
return self.charge()
class Apple(Phone):
def call(self):
print("iphone is calling")
def charge(self):
print("iphone is charging")
class HuaWei(Phone):
def call(self):
print("Huawei is calling")
def charge(self):
print("Huawei is charging")
a1 = Apple()
a1.charge()
a1.call()
h1 = HuaWei()
h1.charge()
h1.call()