[diary.py]
#!/user/bin/env python3
from collections import OrderedDict
import datetime
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 exits. """
db.connect()
db.create_tables([Entry], safe=True)
def menu_loop():
"""Show the menu"""
choice = None
while choice != 'q':
print("Enter 'q' to quit.")
for key, value in menu.items():
print('{}) {}'.format(key, value.__doc__))
choice = input('Action: ').lower().strip()
if choice in menu:
menu[choice]()
def add_entry():
"""Add an entry."""
def view_entries():
"""View previous entries."""
def delete_entry(entry):
"""Delete an entry."""
menu = OrderedDict([
('a', add_entry),
('v', view_entries),
])
if __name__ = '__main__':
initialize()
menu_loop()
#!/user/bin/env python3
The Shabang line: uses the env program to find whatever executable we’re about to tell it.
TextField() doesn’t have to have a length.
.now is a function, whereas:
datetime.datetime.now Gives us the date and time whenever the entry is created.
datetime.datetime.now() Gives us the date and time whenever we ran the script, which will make all entries have the same date time.
Docstring is the text in “”” located as the first thing in a function or class, useful for documentation purposes.
collections is a great standard library that has many useful container types.
value.__doc__ gets the doctstring a ttribute of the function.
OrderedDict([ (‘a’, add_entry), (‘v’, view_entries), ])
As its name suggests, this datatype represents an unusual dictionary that is ordered. It is built with a list of tuples of key-value pairs.
add_entry() vs add_entry
You can use functions as dictionary values.