
import time

import bs4
import requests

class SousChef(object):
    """Organise underlings to determine column values."""
    def __init__(self):
        self.soup = None
        self.callbacks = dict()

    def soupify(self, handle):
        """Produce a tag soup from a HTML file."""
        self.soup = bs4.BeautifulSoup(handle)

    def open(self, filename):
        """Open an HTML file and process it."""
        with open(filename) as html_file:
            self.soupify(html_file)

    def rowify(self, matcher):
        """Find sampled portions of tag soup."""
        return matcher(self.soup)

    def register(self, **callbacks):
        """Add one or more callbacks by keyword arguments."""
        self.callbacks.update(callbacks)

    def process(self, row_pattern):
        """TODO: magic."""
        rows = self.rowify(row_pattern)
        for row in rows:
            columns = dict((key, f(row))
                           for key, f in self.callbacks.iteritems())


class DeskClerk(object):
    """
    Fill out form inputs and save resulting reports.
    """
    def __init__(self, base_url, cooldown=2.0, init=True):
        self.base_url = base_url
        self.session = requests.session()
        self.cooldown = cooldown
        if init:
            self.session.get(base_url)

    def submit(self, form, data):
        """Auto-fill a form "intelligently"."""
        form_url = self.base_url + form
        r = self.session.post(form_url, data)
        return r.content

    def repeat(self, form, repeat_field, repeat_values, static_data=None):
        """Repeatedly submit a form."""
        if static_data is not None:
            data = dict(static_data)
        else:
            data = dict()
        for count, value in enumerate(repeat_values):
            data.update({repeat_field: value})
            self.submit(form, data)
            if count < len(repeat_values) - 1:
                time.sleep(self.cooldown)



@@ -1721,16 +1721,44 @@
  dict()%0A
+        def repeater():%0A    
         
@@ -1811,24 +1811,28 @@
             
+ 
+   
 data.update(
@@ -1862,24 +1862,34 @@
             
+    yield 
 self.submit(
@@ -1892,32 +1892,36 @@
 mit(form, data)%0A
+    
             if c
@@ -1967,16 +1967,20 @@
         
+  
+  
 time.sle
@@ -1997,10 +1997,36 @@
 oldown)%0A
+        return repeater()%0A
 %0A%0A

Make DeskClerk.repeat return a generator