Created
November 4, 2012 18:43
-
-
Save jcarbaugh/4012993 to your computer and use it in GitHub Desktop.
High five, bro!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import logging | |
import os | |
import random | |
import sys | |
import time | |
import urllib | |
import urllib2 | |
logger = logging.getLogger("highfive") | |
def beep_handler(): | |
""" Beep console (stdout) on high five. | |
""" | |
sys.stdout.write('\a') | |
sys.stdout.flush() | |
def stdout_handler(): | |
""" Write high five to stdout. | |
""" | |
sys.stdout.write("High five, bro!\n") | |
def webhook_handler(url): | |
def actual_handler(): | |
data = {'message': 'high five'} | |
data = urllib.urlencode(data) | |
try: | |
resp = urllib2.urlopen(url, data) | |
logger.debug("webhook status: %s %s" % (resp.getcode(), resp.msg)) | |
except urllib2.HTTPError, he: | |
logger.debug("webhook failure: %s %s" % (he.code, he.reason)) | |
return actual_handler | |
def run(handlers, seconds, probability): | |
""" Executes handler functions on given frequency, | |
if probability condition is met. | |
""" | |
logger.debug("high five frequency (seconds): %d, with probability: %0.6f" % (seconds, probability)) | |
while True: # loop FOREVER | |
time.sleep(seconds) # sleep until next attempt | |
gen = random.random() # generate the number | |
logger.debug("generated: %0.6f, probability: %0.6f" % (gen, probability)) | |
if gen <= probability: | |
logger.debug("condition met, commencing high fives") | |
for func in handlers: | |
func() # execute each handler function | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(description='Give some high fives.') | |
parser.add_argument("-b", "--beep", dest="beep", action="store_true", | |
help="beep on console on high five") | |
parser.add_argument("-c", "--console", dest="console", action="store_true", | |
help="display high fives in console (stdout)") | |
parser.add_argument("-d", "--debug", dest="debug", action="store_true", | |
help="write debug output") | |
parser.add_argument("-p", type=float, dest="probability", default=0.1, | |
help="probability of high five, default = 0.1") | |
parser.add_argument("-s", type=int, dest="seconds", default=60, | |
help="number of seconds between high five attempts, default = 60") | |
parser.add_argument("--seed", dest="seed", | |
help="random number generator seed") | |
parser.add_argument("-w", "--webhook", dest="webhook", metavar="URL", | |
help="POST to URL on high five") | |
args = parser.parse_args() | |
if args.seed: | |
# seed random number generator, if specified | |
random.seed(args.seed) | |
if args.debug: | |
# write debug output to console | |
logging.basicConfig(level=logging.DEBUG) | |
# select handler functions based on arguments | |
handlers = [] | |
if args.beep: | |
handlers.append(beep_handler) | |
if args.console: | |
handlers.append(stdout_handler) | |
if args.webhook: | |
handlers.append(webhook_handler(args.webhook)) | |
# run it! | |
run(handlers, args.seconds, args.probability) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment