Skip to content

Instantly share code, notes, and snippets.

@ntlk
Last active August 29, 2015 13:58
Show Gist options
  • Save ntlk/10010576 to your computer and use it in GitHub Desktop.
Save ntlk/10010576 to your computer and use it in GitHub Desktop.
Counting appearances of Hannibal Lecter on screen
import sys
import datetime
import json
from time import strftime
from termcolor import colored
print('Hannibal Lecter appearance counter')
title = input('Please enter the film title: ')
recording = False
capture_started_at = ''
occurrences = []
def title_to_filename(title):
title = title.replace(' ', '_')
date = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
filename = "%(title)s_%(date)s.txt" % locals()
return filename
def save_occurrence():
global recording
recording = False
time_total = (datetime.datetime.now() - capture_started_at).total_seconds()
occurrences.append(time_total)
def finish():
total = sum(occurrences)
count = len(occurrences)
print_to_file(total, count, occurrences)
print('Tallying up the results!')
print("Registered %(count)s occurrences, totalling %(total)s seconds." % locals())
sys.exit()
def print_to_file(total, count, items):
f = open(title_to_filename(title), 'a')
json.dump({'total': total, 'count': count, 'items': items}, f, indent=2)
f.close()
def start_recording():
global recording, capture_started_at
capture_started_at = datetime.datetime.now()
recording = True
print(colored('Hannibal is on! Press ENTER to mark when he disappears.', 'blue'))
def stop_recording():
save_occurrence()
print(colored('Hannibal occurrence captured!', 'green'))
print('Press ENTER to record the next one, or type \'the end\' to finish.')
def error_message():
print(colored('Sorry, I didn\'t understand that. Press ENTER or type \'the end\' to finish.', 'red'))
print('Press ENTER to record an occurrence.')
for line in sys.stdin:
if line == '\n':
if recording:
stop_recording()
else:
start_recording()
elif line == 'the end\n':
if recording:
stop_recording()
finish()
else:
error_message()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment