def print_all(self): '打印所有备忘录' if len(self.memo_list) == 0: print(ColorMe('备忘录中没有记录,请去添加纪录').red()) for memo in self.memo_list: print( ColorMe( f'{memo.id} {memo.date:5} -- {memo.name}--{memo.thing}'). green())
def add(self): '添加一条数据,存储到Memo对象中' try: memo = None memo = Memo(*self.add_input()) print(memo.name, memo.date, memo.thing) # 处理x.id 自增加,利用最后一条memo的id + 1 if len(self.memo_list) == 0: memo.id = 0 else: memo.id = int(self.memo_list[-1]._id) + 1 self.memo_list.append(memo) print(ColorMe('添加成功').red()) except Exception as e: print(ColorMe('添加失败,请重新输入').red(), e)
def add_input(self): '处理添加的输入,返回一个列表[name,time,thing]' input_memo = input('请输入事件 eg(1.1-小8-学习python):').strip() input_list = input_memo.split('-') if len(input_list) == 3: return input_list else: print(ColorMe('输入有误').red())
def search_name(self): '搜索thing,根据输入来进行模糊匹配,查询备忘录' words_input = input('请输入姓名').strip() for memo in self.memo_list: if memo.name == words_input: print( ColorMe( f'{memo.id}--{memo.date:5}--{memo.name}-{memo.thing}'). yellow())
def search_date(self): '根据时间搜索备忘录' input_time = input('请输入一个时间eg(1.1 OR 11.22)') for memo in self.memo_list: if input_time == memo.date: print( ColorMe( f'{memo.id}--{memo.date:5}--{memo.name}-{memo.thing}'). yellow())
def modify(self): '修改某条备忘录' self.print_all() try: index_input = int(input('请输入需要修改的编号').strip()) flag = True # for 循环结束标志位 for memo in self.memo_list: if not flag: # 在while退出的时候,就不再for循环了,节约计算时间。 break if index_input == memo.id: while 1: words_input = input( '请输入要修改的字段(name或date或thing) q返回:').strip() if words_input == 'thing': new_thing = input('请输入一个新thing') memo.thing = new_thing print('修改成功') return True elif words_input == 'date': new_date = input('请输入一个新date').strip() memo.date = new_date print('修改成功') return True elif words_input == 'name': new_name = input('请输入一个新name').strip() memo.name = new_name print('修改成功') return True elif words_input == 'q': flag = False break else: print(ColorMe('输入有误').red()) else: print(ColorMe('没有该编号,请重新输入').red()) except Exception as e: print('输入有误,请重新输入')
def delete(self, num: int): '删除一条记录' try: if len(self.memo_list) == 0: print('已经没了,你还删') else: for memo in self.memo_list: if memo.id == num: self.memo_list.remove(memo) print(ColorMe('删除成功').red()) except Exception as e: print('删除出错,请重新输入')
def print_menu(self): '打印菜单' for k, v in self.menus.items(): print(ColorMe(f' {k} {v}').blue())
def save(self): '保存memo_list到文件中' with open('memo.pkl', 'wb') as f: f.write(pickle.dumps(self.memo_list)) print(ColorMe('文件保存成功').red())
is_add = True while (is_add): in_memo = input('请直接输入需要添加的事件(示例:早上叫我起床):') one = {} time_dict = {'中午': '12', '早上': '8', '下午': '17'} if in_memo.find('点') != -1: one['time'] = in_memo[in_memo.find('点') - 1:in_memo.find('点')] one['thing'] = in_memo[in_memo.find('点') + 1:] elif in_memo.find('中午') != -1: one['time'] = time_dict['中午'] one['thing'] = in_memo[in_memo.find('中午') + 2:] elif in_memo.find('早上') != -1: one['time'] = time_dict['早上'] one['thing'] = in_memo[in_memo.find('早上') + 2:] elif in_memo.find('下午') != -1: one['time'] = time_dict['下午'] one['thing'] = in_memo[in_memo.find('下午') + 2:] else: print('输入错误') all_memo.append(one) num = 0 for x in all_memo: time = ColorMe(x['time'] + '点').blue() thing = ColorMe(x['thing']).red() num += 1 print(f'第{num}条->时间:{time},事件:{thing}') print(f'共添加{len(all_memo)}条任务', end='') print('(y:继续添加,任意键退出)') is_add = input().strip() == 'y' print('再见'.center(30, '-'))
RE_NOON = re.compile(r'中午|早上|上午|晚上') while (is_add): info = input('请输入事件:') if RE_TIME.findall(info): in_time = RE_TIME.findall(info)[0] in_thing = info[info.find('点') + 1:] elif RE_NOON.findall(info): in_time = time_dict[RE_NOON.findall(info)[0]] in_thing = info[info.find('时') + 1:] else: print('输入格式有误,请重新输入!') continue print('待办列表'.center(30, '-')) one = {} one['时间'] = in_time one['事件'] = in_thing all_memo.append(one) num = 0 for m in all_memo: num += 1 print(num, end='\t') print(ColorMe().blue('时间:'), ColorMe().blue(str(m['时间'])), end='\t') print(ColorMe().red('事件:'), ColorMe().red(str(m['事件']))) print(f'共{len(all_memo)}条待办事项。', end='') print('(y:继续添加,n:退出)') is_add = input().strip() == 'y'
# 51memo.py # test # author: wangchen from color_me import ColorMe # 用字典存储备忘录信息 ''' { 'date':in_date, 'thing':in_thing, 'time':in_time } ''' title = ColorMe('51备忘录'.center(30, '-')).yellow() print(title) memo_list = [] user = input(ColorMe('请输入用户名:').yellow()) print(f'欢迎,{user}') choose = 'y' all_time = 0 chos_str = ColorMe('请选择备忘录模式,1.自由模式、2.固定模式').yellow() m = input(chos_str) if m == '1': time_dict = {'早上': '08点', '中午': '12点', '晚上': '20点'} excp_str = ColorMe('请用时间+事件来添加备忘录。例如:08点去上班。或中午去吃饭').yellow() print(excp_str) while choose == 'y':
one = {} if s.find('点')==True: one['时间']=s[s.find('点')-1:s.find('点')+1] one['事件']=s[s.find('点')+1:] else: one['时间']=s[s.find('点')-2:s.find('点')+1] one['事件']=s[s.find('点')+1:] print('待办列表记录'.center(40, '-')) # for self in range(1,5): # aa=s # aa=ColorMe(s).color_str # print(aa) # break aa=ColorMe(s).red() print(aa) all_memo.append(one) num = 0 for m in all_memo: num += 1 print(f'{num},{m}') print(f'共{len(all_memo)}条待办事项') print('(y:继续添加,n: 退出)') is_add = input().strip() == 'y'