Python实现的任务管理助手

wuchangjian2021-11-04 21:22:19编程学习
import os

#storage
validlist = []#有效任务队列
tasks = {}#当前任务-优先级
rank = []#当前已排序任务
#当前任务列
currentlist = "study"
#######

#内存--外部存储连接函数
def get_data(filename,saver):#从filename中向saver写入数据
    if type(saver) == dict:
        file = open("Storage\\"+filename+".txt", 'a')
        file.close()
        file = open("Storage\\"+filename+".txt",encoding = "utf-8")
        for i in file:
            line = i.split()
            saver[line[0]] = line[1]
            rank.append(line[0])
        return

    if type(saver) == list:###这一部分的重构可以优化 但是我懒啊!!!
        with open("Storage\\"+filename+".txt",encoding = "utf-8") as file:
            for i in file:
                line = i.strip()
                saver.append(line)
        return


def save_data(filename,saver):#从saver向filename中写入数据
    if type(saver) == dict:
        file = open("Storage\\"+filename+".txt", 'a')
        file.close()
        with open("Storage\\"+filename+".txt","w",encoding = "utf-8")as file:
            for d in saver:
                file.write(d + ' ' + str(saver[d]) + '\n')
        return    
    if type(saver) == list:###这一部分的重构可以优化 但是我懒啊!!!
        with open("Storage\\"+filename+".txt","w",encoding = "utf-8")as file:
            for l in saver:
                file.write(l+'\n')
        return 

def jump(targetlist):#切换list
    global currentlist
    global tasks
    global rank
    save_data(currentlist,tasks)
    tasks = {}
    rank = []
    get_data(targetlist,tasks)
    currentlist = targetlist
    return
    

###############################################

##############################################CPUandIO

def private_operate(instruction):#核心指令处理器
    global currentlist
    global rank
    global tasks
    if instruction == "help":
        print("Valid instructions:")
        print("ls:print all the tasks")
        print("add:add one task")
        print("help:print instrucions")
        print("remove:remove one task")
        print("eog:I will give you a word")
        print("move:move to another list")
        print("addlist:add a new list")
        print("lslist:print all the lists")
        print("loc:print the list you in")
        return

    if instruction == "fixed":
        print("currentlist "+currentlist)
        print("tasks:")
        print(tasks)
        print("rank:")
        print(rank)
        print("valid:")
        print(validlist)
        return

    if instruction == "sort":
        rank.sort(key = lambda el:int(tasks[el]),reverse=True)
        return




ins = {"ls","add","help","remove","move","addlist","lslist","loc"};#用户可用指令
def operate(instruction):#交互指令处理器
    global currentlist
    global rank
    global tasks

    #调用private_operate层:
    if instruction == "fixed":
        private_operate("fixed")
        return


    #用户可见外层:
    if not(instruction in ins):
        print("INVALID INSTRUCTION!!!")
        return
    #####################只读
    if instruction == "ls":
        private_operate("sort")

        for s in rank:
            print(s+ ' level:'+str(tasks[s]))
        return

    if instruction == "help":
        private_operate("help")
        return

    if instruction == "lslist":
        print(validlist)
        return

    if instruction == "loc":
        print(currentlist)
        return

    #####################只写
    if instruction == "add":
        task = input("Name of the task:")
        level = input("Level of the task:")

        is_data_valid = 0
        try:
            level = int(level)
        except:
            print("Please enter a number as level!")
            return
            

        rank.append(task)
        tasks[task] = level

        

        save_data(currentlist,tasks)
        return

    if instruction == "remove":
        print("Valid tasks:")
        operate("ls")
        task = input("Name of the task:")
        if tasks.get(task)!=None:
            del tasks[task]
            rank.remove(task)

            save_data(currentlist,tasks)
        else:
            print("NOT EXIST!!!")
        return

    if instruction == "move":
        print("Valid list:")
        for i in validlist:
            print(i.strip())
        targetlist = input("Enter the task list you want to go:")
        if targetlist not in validlist:
            print("NOT EXIST!!!")
            return
        if targetlist == currentlist:
            print("YOU ARE ALREADY IN IT!!!")
            return
        #################数据转移
        jump(targetlist)
        print("You are in "+targetlist+" now")
        operate("ls")
        return
    
    if instruction == "addlist":
        listname = input("Name of the list:")
        validlist.append(listname)
        save_data("lists",validlist)
    


         

    
    


################################################



###Shell用户交互窗口
get_data("lists",validlist)#开始使用前先获取有效任务队列    #######接口不一  给我修!
get_data(currentlist,tasks)
print("Manager_0.3 Developed by Eric~")#这个是我做的!!!!

private_operate("help")
while True:
    instruction = input("Manager_0.3>>")
    operate(instruction)
    print("==============================================")
    print("==============================================")
    print("\n")

注意配置运行环境:

要在manager.py所在的目录下增加Storage文件夹,里面要有一个lists.txt 其中添加一行study。

反正很快这个也要被替换成新的助手了。。应该不会再更新,为了避免我把它删掉就先贴上来

相关文章

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。