Python之路,day8-Python基础
***面向对象的好处***
更容易扩展、提高代码使用效率,使你的代码组织性更强,更清晰
更适合复杂项目的开发
封装
把功能的实现细节封装起来,只暴露调用接口
继承
多态
接口继承
定义
类----》模板
对象---》实例化的模板
属性
私有属性 __private
公有属性 存在类的内存里,所有势力共享
成员属性 ---》实例变量
方法 ---》函数
构造函数
析构函数:实例销毁时,自动执行
静态方法
类方法
属性方法
1 class Flight(object): 2 '''我是类的注释''' 3 def __init__(self, name): 4 self.flight_name = name 5 6 def checking_status(self): 7 print("checking flight %s status " % self.flight_name) 8 return 1 9 @property 10 def flight_status(self): 11 status = self.checking_status() 12 if status == 0: 13 print("flight got canceled...") 14 elif status == 1: 15 print("flight is arrived...") 16 elif status == 2: 17 print("flight has departured already...") 18 else: 19 print("cannot confirm the flight status...,please check later") 20 21 @flight_status.setter # 修改 22 def flight_status(self, status): 23 status_dic = { 24 0: "canceled", 25 1: "arrived", 26 2: "departured" 27 } 28 print("\033[31;1mHas changed the flight status to \033[0m", status_dic.get(status)) 29 30 def __call__(self, *args, **kwargs): 31 print('--call:',args,kwargs) 32 33 34 35 @flight_status.deleter # 删除 36 def flight_status(self): 37 print("status got removed...") 38 39 40 f = Flight("CA980") 41 # f.flight_status 42 # f.flight_status = 2 # 触发@flight_status.setter 43 # del f.flight_status # 触发@flight_status.deleter 44 f('123','2343') 45 print(f.__doc__) 46 print(f.__module__) 47 print(f.__class__)
***异常处理***
一、基本异常处理结构
try:
代码块
except Exception as e:
将错误信息输出写入日志文件
二、复杂结构
try:
。。。
except:
。。。
else:
。。。
finally
。。。
三、异常对象
try:
代码块
except Exception as e:#python内部将错误信息封装到e对象中
将错误信息输出写入日志文件
四、 异常种类
Exception,能将所有的异常都捕获
。。。
其他的处理方法,只能处理某一情况
try:
代码块
except Exception as e:#python内部将错误信息封装到e对象中
将错误信息输出写入日志文件
try:
int('asdd')
list = [1,2]
list[1]
except IndexError as e:
pass
except Exception as e:
pass
else:
print('else')
finally:
print('finally')
======>Exception,其他错误的关系
继承关系
五、主动触发异常
六、断言
assert条件
七、自定义异常
反射
hashattr(容器,名称)#
app.py
1 import os 2 import sys 3 4 BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__))) 5 sys.path.append(BASE_DIR) 6 from controller import account 7 8 action = input('>>') 9 if hasattr(account,action): 10 func = getattr(account,action) 11 result =func() 12 else: 13 result = '404' 14 print(result)
account.py
1 def login(): 2 return '请输入永户名密码' 3 def logout(): 4 return '跳转回登陆界面'
***socket***
server.py
1 import socket 2 import os 3 import sys 4 server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 5 server.bind(('0.0.0.0',8000)) 6 server.listen(5) 7 print('start listen') 8 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 9 sys.path.append(BASE_DIR) 10 new_size = 0 11 while True: 12 conn,client_addr = server.accept() 13 print(conn,client_addr) 14 num = 0 15 while True: 16 try: 17 data = conn.recv(1024) 18 if num == 0: 19 filename = data.decode() 20 if num == 1: 21 total_size = int(data.decode()) 22 if num >= 2: 23 print(filename,'这什么情况') 24 with open(filename,'ab') as f: 25 new_size = os.path.getsize(r'%s' % filename) 26 f.write(data) 27 print('recv from client:',filename) 28 conn.send(b'got your message') 29 num += 1 30 except Exception as e: 31 break
client.py
1 import socket 2 import os 3 import sys 4 import re 5 # BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__))) 6 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 7 sys.path.append(BASE_DIR) 8 print(sys.path) 9 print(BASE_DIR) 10 client = socket.socket() 11 client.connect(('localhost',8000)) 12 while True: 13 input_str = input('[put filename]>>').strip() 14 input_list = re.split(' ',input_str) 15 msg = input_list[1] 16 if input_list[0] != 'put': 17 print('command error:',input_list[0],'input [put filename] command') 18 continue 19 if r'\\' not in msg: 20 if os.path.exists(msg): 21 filename = msg 22 else: 23 print('not found file') 24 continue 25 else: 26 filename = msg 27 length_file = os.path.getsize(r'C:\Users\heshaochuan\PycharmProjects\py_s15\day8\反射\test.py') 28 length_file_str = '%s'%length_file 29 if len(filename) == 0:continue 30 client.send(filename.encode()) 31 print('send',filename) 32 data = client.recv(1024) 33 if data: 34 client.send(length_file_str.encode()) 35 data1 = client.recv(1024) 36 if data1: 37 with open(filename, 'rb') as f: 38 for i in f: 39 client.send(i) 40 data2 = client.recv(1024) 41 print('recv from server:',data2)