Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active October 15, 2019 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisswarren/35b8e3177c58f47fbde600b0d30514de to your computer and use it in GitHub Desktop.
Save louisswarren/35b8e3177c58f47fbde600b0d30514de to your computer and use it in GitHub Desktop.
Daily logging made easier with a simple python script

Friday 11th October 2019

A typical Friday

Keywords: logging, programming, overcast

I made a simple script for writing daily logs. This could be useful for taking notes regarding daily progress. This could have been valuable while doing my PhD. Oh well.

The weather isn't great today. Not terrible, just not great.

#!/usr/bin/env python3
import sys
import datetime
usage = '''
mkentry [-]
mkentry [-] <day>
mkentry [-] <month> <day>
mkentry [-] <year> <month> <day>
mkentry --help
Make a journal entry for the given day in the past, writing either to the
appropriate file or stdout.
'''.strip()
template = '''
## {date}
##### Title
*Keywords: None*
Text here.
'''[1:]
def get_date(*xs):
now = datetime.date.today()
y = xs[-3] if len(xs) >= 3 else now.year
m = xs[-2] if len(xs) >= 2 else now.month
d = xs[-1] if len(xs) >= 1 else now.day
return datetime.date(y, m, d)
def human_day(date):
suffixes = {1: 'st', 21: 'st', 31: 'st',
2: 'nd', 22: 'nd',
3: 'rd', 23: 'rd'}
return str(date.day) + suffixes.get(date.day, 'th')
def date_nice_format(date):
fmt = '{0:%A} {human_day} {0:%B} {0:%Y}'
return fmt.format(date, human_day=human_day(date))
def make_entry(date):
return template.format(date=date_nice_format(date))
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--help':
print(usage)
sys.exit(0)
date = get_date(*map(int, sys.argv[1:]))
if date > datetime.date.today():
prompt = date.isoformat() + " is in the future. Continue? [y/N] "
if input(prompt).lower() not in ('y', 'yes'):
sys.exit(1)
try:
with open(date.isoformat() + '.md', 'x') as f:
f.write(make_entry(date))
except FileExistsError:
sys.exit("Entry already exists for {}.".format(date.isoformat()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment