Giter VIP home page Giter VIP logo

iterable_orm's Introduction

Iterable_orm

Latest PyPI version

Latest Travis CI build status

Iterable_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 API

filter - Returns a new list of objects or dictionaries that match the given lookup parameters

exclude - Returns a new list of objects or dictionaries that do not match the given lookup parameters

get - Returns a single object or dictionary if there's two matches returns an exception

order_by - Returns a list ordered objects or dictionaries

first - Returns the first object or dictionary of filtered or exlcude data

last - Returns the first object or dictionary of filtered or exlcude data

count - Returns a lenth of filtered or exlcude or dictionaries

Please 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 Usage

Pass a list of objects or dictionary to Queryset

from iterable_orm import QuerySet
Accounts = [ A list of account objects that have attribute such as name, email, age, gender ect ]
manager = Queryset(Accounts)

Filtering and Excluding

You 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,

from iterable_orm import QuerySet
Accounts = [A list of account objects that have attribute such as name, email, age, gender ect ]
manager = Queryset(Accounts)

# Filter accounts with age greater than 25 and exclude if gender is male
data = manager.filter(age__gt=20).exclude(gender='male')

# Filter using lamda  
data = manager.filter(age=lambda x: x >= 20 and x <= 30).exclude(gender='male')

# Filter accounts with the name starting with letter 's' and gender is female
data = manager.filter(name__istartswith='s').exclude(gender='female')
# Filter accounts who have registred from 2014 till 2016 of current date and who are a female

data = 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 object

data = manager.filter(registered__date_range=('01-01-2015', '01-01-2016')).exclude(gender='female')

Filtering

You can filter data by value or lookups, such as gt, gte ect.

Below are code examples of filtering,

from iterable_orm import QuerySet
Accounts = [A list of account objects that have 
such as name, email, age, gender ect ]
manager = Queryset(Accounts)

# Filter accounts with age greater that 25 
data = manager.filter(age__gt=20)

# Filter accounts with age less that 25 and who are a male
data = manager.filter(age__lt=20, gender='male')

# Get number of accounts with age 20 and who are a female
data = 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 2016

data = manager.filter(registered__date_range=('01-01-2015', '01-01-2016'))

# Filter accounts who have friends who are a male

data = manager.filter(friends__gender='male')

# Filter accounts with date range

data = manager.filter(registered__value_range=('2015-11-15', '2015-11-16')

# chain filter e.g

data = manager.filter(name__istartswith='s').filter(gender='male')

Excluding

You can Exclude data by value or lookups such as gt, gte ect. Below are code examples of exlcude function:

from iterable_orm import QuerySet
Accounts = [A list of account objects that have attribute such as name, email, age, gender ect ]
manager = Queryset(Accounts)

# Exclude accounts with age greater that 25
data = manager.exclude(age__gt=20)

# Exclude accounts with age less then 25 and who are a male
data = 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 2016

data = manager.exclude(registered__date_range=('01-01-2015', '01-01-2016'))

# Exclude accounts who have friends who are a male

data = manager.filter(friends__gender='male')

# Chain exclude e.g.

data = manager.exclude(name__istartswith='s').exclude(gender='male')

Ordering

You can order data by any value of object or dictionary :

from iterable_orm import QuerySet
Accounts = [A list of account objects that have attribute such as name, email, age, gender ect ]
manager = Queryset(Accounts)

# Order by name 
data = manager.order_by('name)

# Order name by descending
data = manager.order_by('-name)

# Ordering by related lookup of friends name
data = manager.order_by('friends__name')

# Ordering by related lookup of friends name descending
data = manager.order_by('-friends__name')

Unit Test

Unit test inlcudes full example usage of the API

To tun unit test run:

python test.py

Installation

Install the latest release with:

pip install iterable_orm

Compatibility

Python 2.7, 3.0 to 3.5

iterable_orm's People

Contributors

7i11 avatar benkonrath avatar said-ali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

iterable_orm's Issues

consider making the filter api consistent with the django queryset api

This is nice library. Thanks for creating it and open-sourcing it on github.

I noticed that your filter api isn't always compatible with the django queryset filter api which is problematic for my use case. For instance, you use value_in where as the django queryset api uses just in.

interable_orm:
https://github.com/Said007/iterable_orm/blob/master/iterable_orm/query.py#L31

django:
https://docs.djangoproject.com/en/1.10/ref/models/querysets/#in

Is there a reason for this? Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.