Package: iterable_orm
Iterable_ormIterable_orm allows you to filter, exclude and sort data using similer API provided by Django ORM. The data needs to be a list of objects or dictionary. Python 2 & 3 is supported.Iterable_orm gives you the following APIfilter - Returns a new list of objects or dictionaries that match the given lookup parametersexclude - Returns a new list of objects or dictionaries that do not match the given lookup parametersget - Returns a single object or dictionary if there’s two matches returns an exceptionorder_by - Returns a list ordered objects or dictionariesfirst - Returns the first object or dictionary of filtered or exlcude datalast - Returns the first object or dictionary of filtered or exlcude datacount - Returns a lenth of filtered or exlcude or dictionariesPlease note that Iterable_orm does not support Q like objects offered by Django ORM, but offers a way around by passing anonymous function to filter or exclude function e.g manager.filter(age=lambda x: x >= 20 and x <= 30)Basic UsagePass a list of objects or dictionary to Querysetfromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)Filtering and ExcludingYou can filter and exclude data by value or lookups, such as gt, gte, lt, lte, startswith, istartswith, endswith, contains, icontains, value_in, value_not_in, value_range, date_range(expects a datetime object) or anonymous function.iterable_orm also allows you to filter related objects using the standard double-underscore notation to separate related fields, e.g manager.filter(parent__name=’John’), this filters by parent.child == ‘John’.All filtering and exlcuding are lazy so you can construct as many filtering as you like and its only evaluated on iterating, calling count, first, last and order_by.Below are code examples of filtering and excluding,fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Filter accounts with age greater than 25 and exclude if gender is maledata=manager.filter(age__gt=20).exclude(gender='male')# Filter using lamdadata=manager.filter(age=lambdax:x>=20andx<=30).exclude(gender='male')# Filter accounts with the name starting with letter 's' and gender is femaledata=manager.filter(name__istartswith='s').exclude(gender='female')# Filter accounts who have registred from 2014 till 2016 of current date and who are a femaledata=manager.filter(registered__date_range=(datetime.today().replace(year=2014),datetime.today().replace(year=2016))).exclude(gender='female')# Filter accounts who have registred from 01-01-2015 till 2016 and who are a female if date is string objectdata=manager.filter(registered__date_range=('01-01-2015','01-01-2016')).exclude(gender='female')FilteringYou can filter data by value or lookups, such as gt, gte ect.Below are code examples of filtering,fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Filter accounts with age greater that 25data=manager.filter(age__gt=20)# Filter accounts with age less that 25 and who are a maledata=manager.filter(age__lt=20,gender='male')# Get number of accounts with age 20 and who are a femaledata=manager.filter(age__gt=20,gender='female').count()# Filter accounts with name starting with letter 's'data=manager.filter(name__istartswith='s')# Filter accounts who have registred from 01-01-2015 till 2016data=manager.filter(registered__date_range=('01-01-2015','01-01-2016'))# Filter accounts who have friends who are a maledata=manager.filter(friends__gender='male')# Filter accounts with date rangedata=manager.filter(registered__value_range=('2015-11-15','2015-11-16')# chain filter e.gdata=manager.filter(name__istartswith='s').filter(gender='male')ExcludingYou can Exclude data by value or lookups such as gt, gte ect.
Below are code examples of exlcude function:fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Exclude accounts with age greater that 25data=manager.exclude(age__gt=20)# Exclude accounts with age less then 25 and who are a maledata=manager.exclude(age__lt=20,gender='male')# Exclude accounts with name starting with letter 's'data=manager.filter(name__istartswith='s')# Exclude accounts who have registred from 01-01-2015 till 2016data=manager.exclude(registered__date_range=('01-01-2015','01-01-2016'))# Exclude accounts who have friends who are a maledata=manager.filter(friends__gender='male')# Chain exclude e.g.data=manager.exclude(name__istartswith='s').exclude(gender='male')OrderingYou can order data by any value of object or dictionary :fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Order by namedata=manager.order_by('name)# Order name by descendingdata=manager.order_by('-name)# Ordering by related lookup of friends namedata=manager.order_by('friends__name')# Ordering by related lookup of friends name descendingdata=manager.order_by('-friends__name')Unit TestUnit test inlcudes full example usage of the APITo tun unit test run:pythontest.pyInstallationInstall the latest release with:pip install iterable_ormCompatibilityPython 2.7, 3.0 to 3.5