Improve query speed for a Django queryset `filter` or `exclude` kwargs dict

    WARNING: Wtthout `trgm`, this only improves the speed of exclude filters by 0.4%

    Arguments:
      filter_dict (dict): kwargs for  Django ORM queryset `filter` and `exclude` queries
      trgm (bool): whether to assume the Django ORM trigram (djorm-trgm) extension is available 

    Examples:
      >>> # This is nothing different than what Django already does:
      >>> optimize_filter_dict({'name__in': ['Smith', 'Jones', 'Smith']}) == {'name__in': set(('Smith', 'Jones'))}  
      True
      >>> # This is an optimjization that Django doesn't do yet, probably because it actually slows `filter` queries down by 0.4%!:
      >>> # However, it does speed up an `objects.exclude` query by about 1%:
      >>> optimize_filter_dict({'name__in': ['Smith']}) == {'name': 'Smith'}
      True
      >>> # This is the only optimization that actually does significant good, but it requires `djorm-trgm`
      >>> optimize_filter_dict({'name__contains': 'ith'}) == {'name__similar': 'ith', 'name__contains': 'ith'}
      True

def optimize_filter_dict(filter_dict, trgm=True):
    """Improve query speed for a Django queryset `filter` or `exclude` kwargs dict

    WARNING: Wtthout `trgm`, this only improves the speed of exclude filters by 0.4%

    Arguments:
      filter_dict (dict): kwargs for  Django ORM queryset `filter` and `exclude` queries
      trgm (bool): whether to assume the Django ORM trigram (djorm-trgm) extension is available 

    Examples:
      >>> # This is nothing different than what Django already does:
      >>> optimize_filter_dict({'name__in': ['Smith', 'Jones', 'Smith']}) == {'name__in': set(('Smith', 'Jones'))}  
      True
      >>> # This is an optimjization that Django doesn't do yet, probably because it actually slows `filter` queries down by 0.4%!:
      >>> # However, it does speed up an `objects.exclude` query by about 1%:
      >>> optimize_filter_dict({'name__in': ['Smith']}) == {'name': 'Smith'}
      True
      >>> # This is the only optimization that actually does significant good, but it requires `djorm-trgm`
      >>> optimize_filter_dict({'name__contains': 'ith'}) == {'name__similar': 'ith', 'name__contains': 'ith'}
      True
    """
    optimized = {}
    for k, v in filter_dict.iteritems():
        if k.endswith('__in'):
            v = set(v)
            if len(v) == 1:
                optimized[k[:-4]] = tuple(v)[0]
            else:
                optimized[k] = v
        else:
            optimized[k] = v
    # This is the only optimization that actuall does some good
    if trgm:
        optimized_copy = dict(optimized)
        for k, v in optimized_copy.iteritems():
            if k.endswith('__contains'):
                optimized[k[:-10] + '__similar'] = v
            elif k.endswith('__icontains'):
                optimized[k[:-11] + '__similar'] = v
    return optimized