Skip to content

Instantly share code, notes, and snippets.

@p7k
Last active Dec 27, 2015
Embed
What would you like to do?
a random dictator for ivan's experimental performance
#!/usr/bin/env python
# adjust these params
KNOBS = ['phase', 'flange', 'delay']
ACTIONS = [('up', 45), ('down', 45), ('..you decide..', 10)]
DELAY_MIN = 1 # seconds
DELAY_MAX = 3 # seconds
# script code
import random
import time
import os
import sys
try:
import pyfiglet
except ImportError:
from setuptools.command import easy_install
easy_install.main(['pyfiglet'])
print '\n\nRESTART ME !\n\n'
sys.exit(0)
def choice_weighted(seq):
total_prob = sum(item[1] for item in seq)
chosen = random.uniform(0, total_prob)
cumulative = 0
for item, probality in seq:
cumulative += probality
if cumulative > chosen:
return item
def choice_remove(seq):
return seq.pop(random.randint(0, len(seq)-1))
def print_fig(text, f=pyfiglet.Figlet(font='univers')):
print f.renderText(text)
def clear_screen():
os.system('clear')
def main():
while KNOBS:
try:
knob = choice_remove(KNOBS)
action = choice_weighted(ACTIONS)
delay = random.randint(DELAY_MIN, DELAY_MAX)
clear_screen()
print_fig('{0} {1}'.format(knob.upper(), action.upper()))
time.sleep(delay)
except KeyboardInterrupt:
break
finally:
if not KNOBS:
time.sleep(3)
clear_screen()
print_fig('STOP')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment