# 作业1
class Authentic:
def __init__(self, name, age):
self.name = name
self.age = age
def register(self):
print('register')
def login(self):
print('login')
l = [('登录', 'login'), ('注册', 'register')]
# 循环这个列表
# 显示 序号 用户要做的操作
# 用户输入序号
# 你通过序号找到对应的login或者register方法
# 先实例化
# 调用对应的方法,完成登录或者注册功能
import sys
for i in enumerate(l):
print(i[0] + 1, i[1][0])
while True:
mode = input("Pls input ur operation(id num)>>>")
if int(mode) > 1 or int(mode) < 2 :
break
print('Illegal input.')
User = Authentic('alex', '83')
mode = int(mode)
if hasattr(User, l[mode-1][1]):
if callable(getattr(User, l[mode-1][1])):
getattr(User, l[mode-1][1])()
# 作业2
class User:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print('eat')
def sleep(self):
print('sleep')
# 用户输入用户名密码性别
# 实例化对象
# 用户任意输入内容 : 不能用异常处理
# 如果输入的是属性名 打印属性值
# 如果输入的是方法名 调用fangfa
# 如果输入的什么都不是 不做操作
user_input = input('Pls input ur name, age, gender>>>')
user_input = user_input.strip().split(' ')
user_obj = User(user_input[0], user_input[1], user_input[2])
choose = input('Pls input ur mode>>>')
if hasattr(user_obj, choose):
if callable(getattr(user_obj, choose)):
getattr(user_obj, choose)()
else: print(getattr(user_obj, choose))
# 作业3
# 注册之后,重启所有的用户丢失
# 一次执行的注册行为,在之后所有执行中都能够正常登录
# 两个登录程序和面向对象的内容整理在一起,两个都要明白,都要记住
class User:
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
class Account:
def __init__(self):
# 用户列表,数据格式:[user对象,user对象,user对象]
self.user_list = []
def login(self):
"""
用户登录,输入用户名和密码然后去self.user_list中校验用户合法性
"""
for count in range(3):
if not self.user_list:
print('Pls register first.')
return
username = input('username:')
password = input('password:')
for i in self.user_list:
if username == i.name and password == i.pwd:
print('Login success.')
return
print('Incorrect account or password.')
def register(self):
"""
用户注册,没注册一个用户就创建一个user对象,然后添加到self.user_list中,表示注册成功。
"""
while True:
while True:
username = input('username:')
password = input('password:')
password_again = input('password again:')
if password == password_again:
break
print('Entered passwords differ.')
if not self.user_list:
new_user = User(username, password)
self.user_list.append(new_user)
return
for i in self.user_list:
if username != i.name:
new_user = User(username, password)
self.user_list.append(new_user)
return
print('This username has been registered.')
def run(self):
"""
主程序
"""
mode_list = ['login', 'register']
while True:
while True:
for i in enumerate(mode_list):
print(i[0] + 1, i[1])
choose = input('Pls choose ur mode(input mode num, input q to quit) >>>')
if choose.upper() == 'Q':
return
elif choose.isdecimal():
if int(choose) == 1 or int(choose) == 2:
break
print('Illegal input.')
choose = int(choose)
if hasattr(self, mode_list[choose - 1]):
if callable(getattr(self, mode_list[choose - 1])):
getattr(self, mode_list[choose - 1])()
if __name__ == '__main__':
obj = Account()
obj.run()
# 作业4
# 写一个自定义模块,里面有你自己实现的mypickle和myjson,我只需要给你传递一个参数 'pickle'还是'json'
import pickle
import json
class My_pickle:
def __init__(self, path):
self.path = path
self.__count = 1
def my_dump(self, obj):
with open(self.path, mode='ab') as file_handler:
pickle.dump(obj, file_handler)
def my_load(self):
with open(self.path, mode='rb') as file_handler:
for i in range(self.__count):
ret = pickle.load(file_handler)
self.__count += 1
return ret
class My_json:
def __init__(self, path):
self.path = path
def my_dump(self, element):
with open(self.path, encoding='utf-8', mode='a') as file_handler:
json.dump(element, file_handler)
def my_load(self):
with open(self.path, encoding='utf-8', mode='r') as file_handler:
ret = json.load(file_handler)
return ret
import sys
test = (1, 2, 3)
path = input('Input path >>>')
mode = input('json or pickle? >>>')
mode = 'My_' + mode
if hasattr(sys.modules['__main__'], mode):
if callable(getattr(sys.modules['__main__'], mode)):
obj = getattr(sys.modules['__main__'], mode)(path)
obj.my_dump(test)