-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiary.py
More file actions
106 lines (78 loc) · 2.47 KB
/
diary.py
File metadata and controls
106 lines (78 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
__author__ = 'Oliver'
import datetime
import os
import sys
from collections import OrderedDict
from peewee import *
db = SqliteDatabase('diary.db')
class Entry(Model):
content = TextField()
timestamp = DateTimeField(default=datetime.datetime.now)
class Meta:
database = db
def initialize():
"""Create the database and the table if they don't exist."""
db.connect()
db.create_tables([Entry], safe=True)
def menu_loop():
"""Show the menu"""
choice = None
while choice != 'q':
clear()
print ('Enter "q" to quit.')
for key, value in menu.items():
print('{}) {}'.format(key, value.__doc__))
choice = raw_input('Action: ').lower().strip()
if choice in menu:
clear()
menu[choice]()
def add_entry():
"""Add an entry"""
print("Enter your entry. Press <Ctrl> + D when finished.")
data = sys.stdin.read().strip()
if data:
if raw_input("Save entry? [y\\n]: ").lower() != 'n':
Entry.create(content=data)
print("Saved successfully!")
def view_entries(search_query=None):
"""View previous entries.
>>> a = [1, 2, 3]
>>> print a
[1, 2, 4]
"""
entries = Entry.select().order_by(Entry.timestamp.desc())
if search_query:
entries = entries.where(Entry.content.contains(search_query))
for entry in entries:
timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p')
clear()
print(timestamp)
print('=' * len(timestamp))
print(entry.content)
print("\n\n" + "-" * len(timestamp))
print('Press [n] for next entry')
print('Press [d] to delete current entry')
print('Press [q] to quit and return to main menu')
next_action = raw_input('Action: [N\\D\\Q] ').lower().strip()
if next_action == 'q':
break
elif next_action == 'd':
delete_entry(entry)
def search_entries():
"""Search entries for a string"""
view_entries(raw_input("Search query: "))
def delete_entry(entry):
"""Delete an entry"""
if raw_input("Are you sure you want to delete this entry? [y\\n]: ").lower() == 'y':
entry.delete_instance()
print("Entry was successfully deleted!")
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
menu = OrderedDict([
('a', add_entry),
('v', view_entries),
('s', search_entries)
])
if __name__ == "__main__":
initialize()
menu_loop()