Jordan Reiter
Jordan Reiter

Reputation: 21032

Python list to bitwise operations

Is there a way to take a list of django query expresses (e.g. Q(first_name="Jordan"), where Q is django.db.models.Q) and bitwise OR them together?

In other words, I have something like this:

search_string = "various search terms"

And I want to do this:

search_params = [Q(description__icontains=term) for term in re.split(r'\W', search_string)]
search_params = something_magical(search_params)
results = Record.objects.filter(search_params)

where search_params now is equivalent to Q(description__icontains="various") | Q(description__icontains="search" | Q(description__icontains="terms"

I know it would be possible with a function like this:

def something_magical(lst):
    result = lst[0]
    for l in lst[1:]
        result |= l
    return result

So I'm wondering if this functionality is already built into Python (and I'm assuming it's more optimized than my function here).

Although I'm interested in it for this application, I'm also just interested in it theoretically.

Upvotes: 10

Views: 2216

Answers (1)

Jan Pöschko
Jan Pöschko

Reputation: 5580

You probably want

import operator 
from functools import reduce    # Python 3
search_params = reduce(operator.or_, search_params, Q())

This will place a bit-wise or (|) between all the items in search_params, starting with an empty condition Q().

Upvotes: 19

Related Questions