<commit_before>import re


INDEX_REGEXP = re.compile("(?P<base>.+)\[(?P<index>\d+)\]$")

def parse_post_data(postdict):
    """
    Parse POST data into a params structure.
    """
    # parse the postdict into a conventient, sorted
    # array of tuples:
    # [(name, value), (name, value)]
    post = []
    for k, v in postdict.iteritems():
        if not k.startswith(("$", "@", "%")):
            continue
        post.append((k, v))
    post.sort()
    paramstruct = _initialise_param_structure(post)
    params = _populate_param_structure(
            post, paramstruct)
    return params


def _initialise_param_structure(post):
    """
    Build the initial parameter structure.  TODO: Make this
    less hideous.
    """
    cleaned = dict()
    for name, value in post:
        last = None
        index = None
        lastindex = None
        parts = name.split(".")
        if len(parts) < 3:
            continue
        parts.pop(0)
        parts.pop(0)
        curr = cleaned
        for part in parts:
            partcopy = part
            indexmatch = INDEX_REGEXP.search(part)
            if indexmatch:
                part, _index = indexmatch.groups()<commit_msg>Allow using a namespace when parsing OCR parameters from POST data<commit_after>import re


INDEX_REGEXP = re.compile("(?P<base>.+)\[(?P<index>\d+)\]$")

def parse_post_data(postdict, prefix="options"):
    """
    Parse POST data into a params structure.
    """
    # parse the postdict into a conventient, sorted
    # array of tuples:
    # [(name, value), (name, value)]
    post = []
    for k, v in postdict.iteritems():
        if not k.startswith(("$" + prefix, "@" + prefix, "%" + prefix)):
            continue
        post.append((k, v))
    post.sort()
    paramstruct = _initialise_param_structure(post)
    params = _populate_param_structure(
            post, paramstruct)
    return params


def _initialise_param_structure(post):
    """
    Build the initial parameter structure.  TODO: Make this
    less hideous.
    """
    cleaned = dict()
    for name, value in post:
        last = None
        index = None
        lastindex = None
        parts = name.split(".")
        if len(parts) < 3:
            continue
        parts.pop(0)
        parts.pop(0)
        curr = cleaned
        for part in parts:
            partcopy = part
            indexmatch = INDEX_REGEXP.search(part)
            if indexmatch:
                part, _index = indexmatch.groups()
{'fraction_non_alphanumeric': 0.11843168957154405, 'fraction_numerical': 0.002425222312045271, 'mean_word_length': 2.09375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '908109', 'n_tokens_mistral': 744, 'n_tokens_neox': 732, 'n_words': 205}