def filterResults(allResults, reportKeys, optimizeKey=None):
<EOL>optimizeDict = dict()<EOL>allReportKeys = set()<EOL>_appendReportKeys(keys=allReportKeys, prefix='<STR_LIT>', results=allResults)<EOL>matchingKeys = _matchReportKeys(reportKeys, allReportKeys)<EOL>reportDict = dict()<EOL>for keyName in matchingKeys:<EOL><INDENT>value = _getReportItem(keyName, allResults)<EOL>reportDict[keyName] = value<EOL><DEDENT>if optimizeKey is not None:<EOL><INDENT>matchingKeys = _matchReportKeys([optimizeKey], allReportKeys)<EOL>if len(matchingKeys) == <NUM_LIT:0>:<EOL><INDENT>raise _BadKeyError(optimizeKey)<EOL><DEDENT>elif len(matchingKeys) > <NUM_LIT:1>:<EOL><INDENT>raise _BadOptimizeKeyError(optimizeKey, matchingKeys)<EOL><DEDENT>optimizeKeyFullName = matchingKeys[<NUM_LIT:0>]<EOL>value = _getReportItem(optimizeKeyFullName, allResults)<EOL>optimizeDict[optimizeKeyFullName] = value<EOL>reportDict[optimizeKeyFullName] = value<EOL><DEDENT>return(reportDict, optimizeDict)<EOL>
Given the complete set of results generated by an experiment (passed in
    'results'), filter out and return only the ones the caller wants, as
    specified through 'reportKeys' and 'optimizeKey'.

    A report key is a string of key names separated by colons, each key being one
    level deeper into the experiment results dict. For example, 'key1:key2'.


    Parameters:
    -------------------------------------------------------------------------
    results:             dict of all results generated by an experiment
    reportKeys:          list of items from the results dict to include in
                         the report. These can be regular expressions.
    optimizeKey:         Which report item, if any, we will be optimizing for. This can
                         also be a regular expression, but is an error if it matches
                         more than one key from the experiment's results.
    retval:  (reportDict, optimizeDict)
                reportDict: a dictionary of the metrics named by desiredReportKeys
                optimizeDict: A dictionary containing 1 item: the full name and
                      value of the metric identified by the optimizeKey