2010-07-26 68 views
0

我已经写在你输入一个标题蟒蛇,内容,然后标签的工具,然后将条目保存在一个泡菜文件。它主要是为复制粘贴功能而设计的(您可以在网络上找到您喜欢的一段代码,将其复制并粘贴到程序中),而不是真正用于手写内容,尽管它没有问题。个人存档工具,寻找建议,对改进代码

我主要做的,因为我总是通过我的PDF文件,书籍扫描,或净对于我已经见过的解决方案的一些编码例,它只是似乎合乎逻辑有一些地方你可以只放入内容,给它一个标题和标签,并且只要你需要查看就可以了。

我知道有网站在线处理这个前。 http://snippets.dzone.com,但我并不总是在线时,我的代码。我也承认,我真的没有看看是否有人写过桌面应用程序,这个项目看起来像是一件有趣的事情。

它不是设计与数百万记的条目,所以我只是用咸菜文件序列化数据,而不是数据库API之一。查询也是非常基本的,只有标题和标签并且没有基于查询的排名。

存在,我想不通,当你在条目列表有一个尝试,除了where子句中它试图赶上一个有效的指数(整数)的问题。如果输入一个inavlid整数,它会要求你输入一个有效的整数,但它似乎不能将它分配给变量。如果你直接输入一个有效的整数,那么没有问题,并且条目将显示。无论如何,让我知道你们的想法。这是编码为python3。

主要文件:

#!usr/bin/python 

from 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 an option") 
     print('====================') 
     print('1. Enter an entry') 
     print('2. Lookup an entry') 
     print('3. Display all entries') 
     print('4. Delete an 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 another query') 
       pause = input('\npress [Enter] to 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 another query') 
       pause = input('\npress [Enter] to continue...') 

     elif choice == "5": 
      break 

     else: 
      input('please enter a 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 a title: ') 
     print('enter the code, press ctrl-d to end: ') 
     self.code = sys.stdin.readlines() 
     self.tags = input('enter tags: ') 

    def save_data(self): 
     with open('entry.pickle', 'ab') as f: 
      pickle.dump(self, f) 

    def del_data(self): 
     with open('entry.pickle', 'rb') as f: 
      data_list = [] 
      while True: 
       try: 
        entry = pickle.load(f) 
        if self.title == entry.title: 
         continue 
        data_list.append(entry) 
       except: 
        break 
     with open('entry.pickle', 'wb') as f: 
      pass 
     with open('entry.pickle', 'ab') as f: 
      for data in data_list: 
       data.save_data() 

def entry_query(file, queryset): 
    '''returns a list of objects matching the query''' 
    result = [] 
    try: 
     with open(file, 'rb') as 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 an entry first') 
     pause = input('\nPress [Enter] to continue...') 

def choices(list_result): 
    '''takes a list of objects and returns the index of the selected object''' 
    os.system('clear') 
    index = 0 
    for entry in list_result: 
     print('{}. {}'.format(index, entry.title)) 
     index += 1 
    try: 
     choice = int(input('\nEnter choice: ')) 
     return choice 
    except: 
     pause = input('\nplease enter a valid choice') 
     choices(list_result) 


def print_choice(list_result, choice): 
    '''takes a list of objects and an index and displays the index of the list''' 
    os.system('clear') 
    print('===================') 
    print(list_result[choice].title) 
    print('===================') 
    for 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. Back to entry list') 
    print('2. Back to Main Menu') 
    choice = input(':') 
    if choice == "1": 
     print_choice(list_result, choices(list_result)) 
    elif choice == "2": 
     pass 
    else: 
     print('\nplease enter a valid choice') 
     back_to_choices(list_result) 
+0

对于命令行工具,有一个叫做“cmd”的模块,我已经使用了几次。它可能能够让你的生活更轻松。 – 2010-07-26 19:57:07

回答

1

else,调用main功能再次递归。相反,我会做类似choice == "0",这只会导致while循环请求另一个条目。这避免了无意义的递归。