Skip to main content

Python module to add support for ORM-style filtering to any list of items

Project description

Python module to add support for ORM-style filtering to any list of items

Use through one of the list-type extending classes:

QueryableListObjs - This assumes each item is an object [or implements __getattribute__].

QueryableListDicts - This assumes that each item is a dict [or implements __getitem__].

You can filter these objects by using the method “filterAnd” (or its alias, “filter”), or “filterOr”.

filterAnd returns a QueryableList where each item matches ALL of the provided criteria. filterOr returns a QueryableList where each item matches ANY of the provided criteria.

You specify the filter operations by passing arguments of $fieldName__$operation (e.x. results = objs.filter(name__ne=’Tim’) ), where “$fieldName” matches the name of an attribute/key and “$operation” is one of the following:

Operations

  • eq - Test equality ( = operator )

  • ieq - Test equality, ignoring case (must be strings, or at least implement the .lower() method)

  • ne - Test inequality ( != operator )

  • ine - Test inequality, ignoring case (must be strings, or at least implement the .lower() method)

  • lt - The item’s field value must be less than the provided value

  • lte - The item’s field value must be less than or equal to the provided value

  • gt - The item’s field value must be greater than the provided value

  • gte - The item’s field value must be greater than or equal to the provided value

  • isnull - Provided value must be True/False. If True, the item’s field value must be None, otherwise it must not be None.

  • is - Test identity equality ( is operator )

  • isnot - Test identity inequality ( is not operator )

  • in - Test that the item’s field value is contained in the provided list of items

  • notin - Test that the item’s field value is not contained in the provided list of items

  • contains - Test that the item’s field value contains the provided value ( using “in” )

  • notcontains - Test that the item’s field value does not contain the provided value ( using “not in” )

  • containsAny - Test that the item’s field value contains any of the items in the provided list ( using “in” )

  • notcontainsAny - Test that the item’s field value does not contain any of the items in the provided list ( using “not in” )

Full Documentation

Pydoc documentation can be found at: http://htmlpreview.github.io/?https://github.com/kata198/QueryableList/blob/master/doc/QueryableList.html?vers=1

Example

Here is an example with some simple, silly data, doing some filters, followed by the results.

from QueryableList import QueryableListDicts, QueryableListObjs

import sys

class DataObj(object):

pass

class SampleDataObj(object):

def __init__(self, colour, age, name, likes):

self.colour = colour

self.age = age

self.name = name

self.likes = likes

def __str__(self):

return str(self.__dict__)

__repr__ = __str__

if __name__ == ‘__main__’:

#data = [{‘colour’: ‘purple’, ‘age’: 31, ‘name’: ‘Tim’, ‘likes’ : [‘puppies’, ‘rainbows’]}, {‘colour’: None, ‘age’: 19, ‘name’: ‘Joe’, ‘likes’ : [‘puppies’, ‘cars’]}, {‘colour’: ‘PURPLE’, ‘age’: 23, ‘name’: ‘Joe’, ‘likes’ : [‘cheese’, ‘books’]}]

data = [

SampleDataObj(colour=’purple’, age=31, name=’Tim’, likes=[‘puppies’, ‘rainbows’]),

SampleDataObj(colour=None, age=19, name=’Joe’, likes=[‘puppies’, ‘cars’]),

SampleDataObj(colour=’PURPLE’, age=23, name=’Joe’, likes=[‘cheese’, ‘books’]),

]

#data = QueryableListDicts(data)

data = QueryableListObjs(data)

sys.stdout.write(“Data: %snn” %(data,))

sys.stdout.write(‘People who are over 22 years old:n%snn’ %(data.filter(age__gt=22),))

#sys.stdout.write(‘People who like puppies or bricks, and their favourite colour is purple:nn’ %(data.filter(likes__containsAny=(‘puppies’, ‘bricks’)).filter(colour__ieq=’purple’),)) sys.stdout.write(‘People who like puppies or bricks, and their favourite colour is purple:n%snn’ %(data.filter(likes__containsAny=(‘puppies’, ‘bricks’), colour__ieq=’purple’),))

sys.stdout.write(‘People who are at least 30 years old or like cheese:n%snn’ %(data.filterOr(likes__contains=’cheese’, age__gte=30),))

#import pdb; pdb.set_trace()

Results:

Data: [{‘colour’: ‘purple’, ‘likes’: [‘puppies’, ‘rainbows’], ‘age’: 31, ‘name’: ‘Tim’}, {‘colour’: None, ‘likes’: [‘puppies’, ‘cars’], ‘age’: 19, ‘name’: ‘Joe’}, {‘colour’: ‘PURPLE’, ‘likes’: [‘cheese’, ‘books’], ‘age’: 23, ‘name’: ‘Joe’}]

People who are over 22 years old:

[{‘colour’: ‘purple’, ‘likes’: [‘puppies’, ‘rainbows’], ‘age’: 31, ‘name’: ‘Tim’}, {‘colour’: ‘PURPLE’, ‘likes’: [‘cheese’, ‘books’], ‘age’: 23, ‘name’: ‘Joe’}]

People who like puppies or bricks, and their favourite colour is purple:

[{‘colour’: ‘purple’, ‘likes’: [‘puppies’, ‘rainbows’], ‘age’: 31, ‘name’: ‘Tim’}]

People who are at least 30 years old or like cheese:

[{‘colour’: ‘purple’, ‘likes’: [‘puppies’, ‘rainbows’], ‘age’: 31, ‘name’: ‘Tim’}, {‘colour’: ‘PURPLE’, ‘likes’: [‘cheese’, ‘books’], ‘age’: 23, ‘name’: ‘Joe’}]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

QueryableList-1.0.0.tar.gz (19.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page