Dump a limitted number of json.dump-able objects to the indicated file

    rate (int): Number of queries per 15 minute twitter window

def limitted_dump(cursor=None, twitter=None, path='tweets.json', limit=450, rate=TWITTER_SEARCH_RATE_LIMIT, indent=-1):
    """Dump a limitted number of json.dump-able objects to the indicated file

    rate (int): Number of queries per 15 minute twitter window
    """
    if not twitter:
        twitter = get_twitter()
    cursor = cursor or 'python'
    if isinstance(cursor, basestring):
        cursor = get_cursor(twitter, search=cursor)
    newline = '\n' if indent is not None else ''
    if indent < 0:
        indent = None
    # TODO: keep track of T0 for the optimal "reset" sleep duration
    with (open(path, 'w') if not isinstance(path, file) else path) as f:
        f.write('[\n')
        for i, obj in enumerate(cursor):
            f.write(json.dumps(obj, indent=indent))
            if i < limit - 1:
                f.write(',' + newline)
            else:
                break
            remaining = int(twitter.get_lastfunction_header('x-rate-limit-remaining'))
            if remaining > 0:
                sleep(1. / rate)
            else:
                sleep(15 * 60)
        f.write('\n]\n')