Skip to content

Instantly share code, notes, and snippets.

@prophile
Last active August 29, 2015 14:05
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 prophile/dda01466a36978c6942d to your computer and use it in GitHub Desktop.
Save prophile/dda01466a36978c6942d to your computer and use it in GitHub Desktop.
from collections import namedtuple
import cgi
import sys
import yaml
DATA = """
- type: checkbox
description: Did the robot move?
key: robot_moved
- type: natural
description: How many golds did the robot collect?
key: golds
- type: natural
description: How many silvers did the robot collect?
key: silver
- type: radio
description: Slot 0
key: slot0
"""
Entry = namedtuple('Entry', 'key entry compute')
def check(label, attr):
return Entry(key = lambda name: cgi.escape(label),
entry = lambda name, zone: '<input type="checkbox" name="{}_{}">'.format(name, zone),
compute = lambda name, zone, data: {attr: '{}_{}'.format(name, zone) in data})
def natural(label, attr):
return Entry(key = lambda name: cgi.escape(label),
entry = lambda name, zone: '<input type="number" name="{}_{}" value="0">'.format(name, zone),
compute = lambda name, zone, data: {attr: int(data.get('{}_{}'.format(name, zone), 0))})
def radio(label, attr):
return Entry(key = lambda name: cgi.escape(label) + "<br><input type=\"radio\" name=\"{}\" value=\"none\" checked>".format(name),
entry = lambda name, zone: '<input type="radio" name="{}" value="{}">'.format(name, zone),
compute = lambda name, zone, data: {attr: data.get(name, 'none') == str(zone)})
def full(label):
return Entry(key = lambda name: cgi.escape(label))
TYPE_DIR = {'checkbox': check, 'natural': natural, 'radio': radio}
BUILTIN_ENTRIES = [{'type': 'checkbox',
'description': 'Was the team present for the match?',
'key': 'present'},
{'type': 'checkbox',
'description': 'Was the team disqualified from the match?',
'key': 'disqualified'}]
option_entries = BUILTIN_ENTRIES + yaml.load(DATA)
options = [TYPE_DIR[entry['type']](entry['description'], entry['key']) for entry in option_entries]
NZONES = 4
def generate_html(options):
yield '<table>'
yield ' <tr>'
# Key
yield ' <th></th>'
# Zones
for n in range(NZONES):
yield ' <th>{}</th>'.format('Zone {}'.format(n))
yield ' </tr>'
for k, option in enumerate(options):
yield ' <tr>'
name = 'entry-{}'.format(k)
# Key
yield ' <td>'
yield ' ' + option.key(name)
yield ' </td>'
for n in range(NZONES):
yield ' <td>'
yield ' ' + option.entry(name, n)
yield ' </td>'
yield ' </tr>'
yield '</table>'
def handle_input(options, data, teams):
configuration = {}
for zone, team in enumerate(teams):
team_data = {'zone': zone}
for k, option in enumerate(options):
name = 'entry-{}'.format(k)
status = option.compute(name, zone, data)
team_data.update(status)
configuration[team] = team_data
return configuration
for line in generate_html(options):
print(line)
data = {'entry-0_0': '',
'entry-0_1': '',
'entry-0_2': '',
'entry-1_2': '',
'entry-2_0': '2',
'entry_2_1': '1',
'entry_2_2': '2',
'entry_2_3': '0',
'entry-5': '3'}
yaml.dump(handle_input(options, data, ('AAA', 'BBB', 'CCC', 'DDD')),
sys.stdout,
default_flow_style=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment