今日作业
'''
0、课堂代码理解,并敲两遍以上 (技术的牛逼是靠量的积累)
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方法用来从文件中反序列化出对象
import pickle
import os
import uuid
from day21 import settings
'''
settings.py内容
HOST='127.0.0.1'
PORT=3306
DB_PATH=r'D:pycharmlaorichangday21cof'
'''
DB_PATH=r'D:pycharmlaorichangday21cof'
class MYSOL:
def __init__(self,host,port):
self.id=self.create_id()
self.host=host
self.port=port
def create_id(self):
return str(uuid.uuid1())
def is_exict(self):
tag=True
files=os.listdir(DB_PATH)
for line in files:
files_abspath=os.path.join(DB_PATH,line)
# obj=pickle.load(open((files_abspath,'rb'))
with open(files_abspath,'rb') as fr:
obj=pickle.load(fr)
if self.port==obj.port and self.host==obj.host:
tag=False
break
return tag
def save(self):
if not self.is_exict():
# raise ('对象已存在')
print('对象已存在')
else:
files_abspath=os.path.join(DB_PATH,self.id)
pickle.dump(self,open(files_abspath,'wb'))
def get_obj_by_id(self):
files_abspath=os.path.join(DB_PATH,self.id)
print(pickle.load(open(files_abspath,'rb')).__dict__)
def from_conf(self):
return self(settings.HOST,settings.PORT)
conn=MYSOL.from_conf(MYSOL)
conn.save()
conn.get_obj_by_id()
2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放
参考答案(http://www.cnblogs.com/linhaifeng/articles/7340801.html#_label4)
import math
class Circle:
def __init__(self,r):
self.__r=r
def area(self):
return math.pi*self.__r**2
def perimeter(self):
return 2*math.pi*self.__r
c=Circle(10)
print(c.area())#314.1592653589793
print(c.perimeter())#62.83185307179586
3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类
import abc
class Phone(metaclass=abc.ABCMeta):
@abc.abstractmethod
def ff(self):
pass
@abc.abstractmethod
def dd(self):
pass
class shouji(Phone):
def ff(self):
print('showtime')
def dd(self):
print('call')
xiaomi=shouji()
xiaomi.dd()#call
xiaomi.ff()#showtime
4、着手编写选课系统作业:http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label15
'''