我已經寫在你輸入一個標題蟒蛇,內容,然後標籤的工具,然後將條目保存在一個泡菜文件。它主要是爲複製粘貼功能而設計的(您可以在網絡上找到您喜歡的一段代碼,將其複製並粘貼到程序中),而不是真正用於手寫內容,儘管它沒有問題。個人存檔工具,尋找建議,對改進代碼
我主要做的,因爲我總是通過我的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)
對於命令行工具,有一個叫做「cmd」的模塊,我已經使用了幾次。它可能能夠讓你的生活更輕鬆。 – 2010-07-26 19:57:07