From 448f56473d808b6bf8e63466597e954f399b14aa Mon Sep 17 00:00:00 2001 From: Stephan Gabler Date: Mon, 14 May 2012 21:44:30 +0200 Subject: [PATCH 1/2] simplify date parsing and remove some whitespace --- jrnl.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jrnl.py b/jrnl.py index 6da09640..b3603c6e 100755 --- a/jrnl.py +++ b/jrnl.py @@ -175,7 +175,7 @@ class Journal: for line in journal.split(os.linesep): if line: try: - new_date = datetime.fromtimestamp(time.mktime(time.strptime(line[:date_length], self.config['timeformat']))) + new_date = datetime.strptime(line[:date_length], self.config['timeformat']) # make a journal entry of the current stuff first if new_date and current_entry: entries.append(current_entry) @@ -459,8 +459,8 @@ if __name__ == "__main__": elif args.tags: # get all tags # Astute reader: should the following line leave you as puzzled as me the first time # I came across this construction, worry not and embrace the ensuing moment of enlightment. - tags = [tag - for entry in journal.entries + tags = [tag + for entry in journal.entries for tag in set(entry.tags) ] # To be read: [for entry in journal.entries: for tag in set(entry.tags): tag] From 7c3a6d02221d87c956136c28ee0a3453941474f7 Mon Sep 17 00:00:00 2001 From: Stephan Gabler Date: Mon, 14 May 2012 21:57:32 +0200 Subject: [PATCH 2/2] don't ignore the blank lines #15 --- jrnl.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/jrnl.py b/jrnl.py index b3603c6e..0348af8c 100755 --- a/jrnl.py +++ b/jrnl.py @@ -173,18 +173,19 @@ class Journal: current_entry = None for line in journal.split(os.linesep): - if line: - try: - new_date = datetime.strptime(line[:date_length], self.config['timeformat']) - # make a journal entry of the current stuff first - if new_date and current_entry: - entries.append(current_entry) - # Start constructing current entry - current_entry = Entry(self, date=new_date, title=line[date_length+1:]) - except ValueError: - # Happens when we can't parse the start of the line as an date. - # In this case, just append line to our body. - current_entry.body += line + try: + # try to parse line as date => new entry begins + new_date = datetime.strptime(line[:date_length], self.config['timeformat']) + + # parsing successfull => save old entry and create new one + if new_date and current_entry: + entries.append(current_entry) + current_entry = Entry(self, date=new_date, title=line[date_length+1:]) + except ValueError: + # Happens when we can't parse the start of the line as an date. + # In this case, just append line to our body. + current_entry.body += line + os.linesep + # Append last entry if current_entry: entries.append(current_entry)