Skip to content

Instantly share code, notes, and snippets.

@aldnav
Created November 28, 2018 04:23
Show Gist options
  • Save aldnav/b5fed8de91eea99aafda910060e981e3 to your computer and use it in GitHub Desktop.
Save aldnav/b5fed8de91eea99aafda910060e981e3 to your computer and use it in GitHub Desktop.
Journal Program
class Journal:
# xRead from an input file
# xAdd entry to journal
# xView entries
# xSearch entries
# Edit entries
# Delete entries
# xExport entries
def __init__(self):
self.journal_id = 1
self.entries = []
def read_input(self):
lines = []
with open('./journal.txt') as f:
lines = f.readlines()
for line in lines:
date, text = line.split('|')
self.add_entry(date, text)
return lines
def add_entry(self, date, text):
self.entries.append({
'date': date,
'text': text
})
def view_entries(self):
print('Date\t | Text')
print('---' * 8)
for entry in self.entries:
date, text = entry.values()
print(date, '|', end=' ')
print(text)
def search_entries(self, query=''):
matched_entries = [entry for entry in self.entries if query in entry['text']]
print(matched_entries)
return matched_entries
def export_entries(self, file_name):
with open(file_name, 'w') as f:
f.writelines([
'{date}|{text}'.format_map(entry)
for entry in self.entries
])
def __str__(self):
return 'Journal ' + str(self.journal_id)
journal = Journal()
journal.read_input()
print(journal)
journal.view_entries()
journal.search_entries(query='birthday')
journal.export_entries(file_name='journal_backup.txt')
01-01-2018|New Year
10-06-2018|Woke up early
02-15-2019|20th birthday!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment