python - Personal archive tool, looking for suggestions on improving the code -
i've written tool in python enter title, content, tags, , entry saved in pickle file. designed copy-paste functionality (you spot piece of code on net, copy it, , paste program), not handwritten content, though no problem.
i did because i'm scanning through pdf files, books, or net coding example of solution i'd seen before, , seemed logical have put content in, give title , tags, , whenever needed to.
i realize there sites online handle ex. http://snippets.dzone.com, i'm not online when code. admit didn't see if had written desktop app, project seemed fun thing here am.
it wasn't designed millions of entries in mind, use pickle file serialize data instead of 1 of database apis. query basic, title , tags , no ranking based on query.
there issue can't figure out, when @ list of entries there's try, except clause tries catch valid index (integer). if enter inavlid integer, ask enter valid one, doesn't seem able assign variable. if enter valid integer straightaway, there no problems , entry display.
anyway let me know guys think. coded python3.
main file:
#!usr/bin/python archive_functions import entry, choices, print_choice, entry_query import os def main(): choice = '' while choice != "5": os.system('clear') print("mo's archive, please select option") print('====================') print('1. enter entry') print('2. lookup entry') print('3. display entries') print('4. delete entry') print('5. quit') print('====================') choice = input(':') if choice == "1": entry = entry() entry.get_data() entry.save_data() elif choice == "2": queryset = input('enter title or tag query: ') result = entry_query('entry.pickle', queryset) if result: print_choice(result, choices(result)) else: os.system('clear') print('no match! please try query') pause = input('\npress [enter] continue...') elif choice == "3": queryset = 'all' result = entry_query('entry.pickle', queryset) if result: print_choice(result, choices(result)) elif choice == "4": queryset = input('enter title or tag query: ') result = entry_query('entry.pickle', queryset) if result: entry = result[choices(result)] entry.del_data() else: os.system('clear') print('no match! please try query') pause = input('\npress [enter] continue...') elif choice == "5": break else: input('please enter valid choice...') main() if __name__ == "__main__": main()
archive_functions.py:
#!/bin/usr/python import sys import pickle import os import re class entry(): def get_data(self): self.title = input('enter title: ') print('enter code, press ctrl-d end: ') self.code = sys.stdin.readlines() self.tags = input('enter tags: ') def save_data(self): open('entry.pickle', 'ab') f: pickle.dump(self, f) def del_data(self): open('entry.pickle', 'rb') f: data_list = [] while true: try: entry = pickle.load(f) if self.title == entry.title: continue data_list.append(entry) except: break open('entry.pickle', 'wb') f: pass open('entry.pickle', 'ab') f: data in data_list: data.save_data() def entry_query(file, queryset): '''returns list of objects matching query''' result = [] try: open(file, 'rb') f: entry = pickle.load(f) os.system('clear') if queryset == "all": while true: try: result.append(entry) entry = pickle.load(f) except: return result break while true: try: if re.search(queryset, entry.title) or re.search(queryset, entry.tags): result.append(entry) entry = pickle.load(f) else: entry = pickle.load(f) except: return result break except: print('no entries in file, please enter entry first') pause = input('\npress [enter] continue...') def choices(list_result): '''takes list of objects , returns index of selected object''' os.system('clear') index = 0 entry in list_result: print('{}. {}'.format(index, entry.title)) index += 1 try: choice = int(input('\nenter choice: ')) return choice except: pause = input('\nplease enter valid choice') choices(list_result) def print_choice(list_result, choice): '''takes list of objects , index , displays index of list''' os.system('clear') print('===================') print(list_result[choice].title) print('===================') line in list_result[choice].code: print(line, end="") print('\n\n') back_to_choices(list_result) def back_to_choices(list_result): print('1. entry list') print('2. main menu') choice = input(':') if choice == "1": print_choice(list_result, choices(list_result)) elif choice == "2": pass else: print('\nplease enter valid choice') back_to_choices(list_result)
in else
, call main
function again recursively. instead, i'd choice == "0"
, cause while loop request entry. avoids pointless recursion.
Comments
Post a Comment