# coding:utf-8标签:反射,name,改查,db,user,path,增删,input,os From: https://www.cnblogs.com/HaiMan/p/16867573.html
import os
import sys
import json
root_dir = os.path.dirname(os.path.dirname(__file__))
sys.path.append(root_dir)
db_dir = os.path.join(root_dir, 'db')
if not os.path.exists(db_dir):
os.mkdir(db_dir)
class UserOperate:
def add_user(self):
user_name = input('Please input your username>>>:')
pass_word = input('Please input your password>>>:')
user_dict = {
'user_name': user_name,
'pass_word': pass_word,
}
user_real_db = os.path.join(db_dir, f'{user_name}.json')
if not os.path.exists(user_real_db):
with open(user_real_db, 'w', encoding='utf-8') as f:
json.dump(user_dict, f, ensure_ascii=False)
else:
print('username is exists!')
def del_user(self):
del_name = input('Please input you need delete name>>>:')
user_real_db = os.path.join(db_dir, f'{del_name}.json')
os.remove(user_real_db)
def update_user(self):
update_data = input('Please input you need update name>>>:')
user_real_db = os.path.join(db_dir, f'{update_data}.json')
with open(user_real_db, 'r', encoding='utf-8') as f:
user_dict = json.load(f)
print(user_dict)
new_update_data = input('Please input you need update name>>>:')
user_dict['user_name'] = new_update_data
print(user_dict)
def check_user(self):
user_data = input('Please input you need check name>>>:')
user_real_db = os.path.join(db_dir, f'{user_data}.json')
with open(user_real_db, 'r', encoding='utf-8') as f:
user_dict = json.load(f)
print(user_dict)
def system_run(self):
print('user system!')
while True:
target_operate = input('please input your operate>>>: ')
if hasattr(self, target_operate):
getattr(self, target_operate)()
else:
print('operate is not in class')
obj = UserOperate()
obj.system_run()