Skip to content

Instantly share code, notes, and snippets.

@rickumali
Last active August 29, 2021 00:39
Show Gist options
  • Save rickumali/1c7bb9d6bb2bf43b63115db0eb192045 to your computer and use it in GitHub Desktop.
Save rickumali/1c7bb9d6bb2bf43b63115db0eb192045 to your computer and use it in GitHub Desktop.
This Python randomly selects multiple diary files from my $HOME/diary directory, and displays them for me to read. The $HOME/diary directory contains files with the pattern YYYYMMDD.txt.
#!/usr/bin/python
import argparse
import os
import random
import re
import subprocess
from datetime import date
parser = argparse.ArgumentParser(description='Reads random diary entries', epilog='If --old or --today or --thisday present, any day arguments are ignored')
group = parser.add_mutually_exclusive_group()
group.add_argument('--old', help='Reads entries between 1970-1999', action='store_true')
group.add_argument('--today', help='Reads entries with today\'s month and day', action='store_true')
group.add_argument('--thisday', help='Reads entries with specified\'s month and day', nargs=2, metavar=('M','D'), type=int)
parser.add_argument('--monday', help='Reads Monday entries', action='store_true')
parser.add_argument('--tuesday', help='Reads Tuesday entries', action='store_true')
parser.add_argument('--wednesday', help='Reads Wednesday entries', action='store_true')
parser.add_argument('--thursday', help='Reads Thursday entries', action='store_true')
parser.add_argument('--friday', help='Reads Friday entries', action='store_true')
parser.add_argument('--saturday', help='Reads Saturday entries', action='store_true')
parser.add_argument('--sunday', help='Reads Sunday entries', action='store_true')
parser.add_argument('--debug', help='Debug mode', action='store_true')
parser.add_argument('-c', '--count', type=int, default=8, help='Count of entries to show', metavar='N')
args = parser.parse_args()
diary_dir = os.path.expanduser('~/diary')
file_list = os.listdir(diary_dir)
def make_datetime(s):
m = re.match('(?P<year>\d\d\d\d)(?P<month>\d\d)(?P<day>\d\d).txt', s)
if (m != None):
year = int(m.group('year'))
month = int(m.group('month'))
day = int(m.group('day'))
return(date(year,month,day))
else:
return None
dates = [make_datetime(f) for f in file_list]
def make_filename(d):
return f'{d.year}{d.month:02}{d.day:02}.txt'
def in_20th_century(d):
if d == None:
return False
if (d.year < 2000):
return True
else:
return False
def same_month_day(d):
if d == None:
return False
today = date.today()
if (today.month == d.month) and (today.day == d.day):
return True
else:
return False
def same_as_this_day(d, month, day):
if d == None:
return False
if (month == d.month) and (day == d.day):
return True
else:
return False
def day_args_specified(args):
return args.monday or args.tuesday or args.wednesday or args.thursday or args.friday or args.saturday or args.sunday
def same_wday(d, args):
if d == None:
return False
if args.monday and d.weekday() == 0:
return True
if args.tuesday and d.weekday() == 1:
return True
if args.wednesday and d.weekday() == 2:
return True
if args.thursday and d.weekday() == 3:
return True
if args.friday and d.weekday() == 4:
return True
if args.saturday and d.weekday() == 5:
return True
if args.sunday and d.weekday() == 6:
return True
return False
filtered_list = []
if args.old:
filtered_list = [make_filename(d) for d in dates if in_20th_century(d)]
elif args.today:
filtered_list = [make_filename(d) for d in dates if same_month_day(d)]
elif args.thisday:
filtered_list = [make_filename(d) for d in dates if same_as_this_day(d, args.thisday[0], args.thisday[1])]
elif day_args_specified(args):
filtered_list = [make_filename(d) for d in dates if same_wday(d, args)]
else:
filtered_list = file_list
if len(filtered_list) > args.count:
sampled_list = random.sample(filtered_list, args.count)
else:
# Avoids "ValueError: Sample larger than population"
sampled_list = filtered_list
sampled_list.sort()
command_string=['less'] + sampled_list
if (args.debug):
print(' '.join(command_string))
else:
subprocess.call(command_string, cwd=diary_dir)
print('Reread: ')
print(' '.join(command_string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment