Git Product home page Git Product logo

lifter's Introduction

What is lifter?

https://readthedocs.org/projects/lifter/badge/?version=latest https://travis-ci.org/EliotBerriot/lifter.svg?branch=master https://travis-ci.org/EliotBerriot/lifter.svg?branch=develop

Lifter is a lightweight query engine for Python iterables, inspired by Django ORM.

If you find if painful to gather data from collections of Python objects or dictionaries, such as API results, lifter is for you!

Warning: This package is still in alpha state and a lot of work is still needed to make queries faster and efficient. Contributions are welcome :)

Useful links:

Features

  • Operates on plain objects or mapping (such as dictionaries)
  • Queryset API similar to Django and SQLAlchemy
  • Lazy querysets
  • Composable queries
  • Lightweight: absolutely no dependencies
  • Tested and working on Python 2.7 to Python 3.5

Example usage

Consider the following list of users, returned from a REST API endpoint:

users = [
    {
        "is_active": True,
        "age": 35,
        "eye_color": "brown",
        "name": "Bernard",
        "gender": "male",
        "email": "[email protected]",
    },
    {
        "is_active": True,
        "age": 34,
        "eye_color": "brown",
        "name": "Manny",
        "gender": "male",
        "email": "[email protected]",
    },
    {
        "is_active": True,
        "age": 35,
        "eye_color": "brown",
        "name": "Fran",
        "gender": "female",
        "email": "[email protected]",
    },
    # And so on ...
]

Now, imagine you have to extract data from this list. Let's compare how you can do this using regular Python and lifter.

To use lifter in your project, you'll only need the following instructions:

import lifter

User = lifter.models.Model('User')
manager = User.load(users)

Getting getting all active 26 years old users:

# vanilla Python
results = [
    user for user in users
    if user['age'] == 26 and user['is_active']
]

# lifter
results = manager.filter(User.age == 26, User.is_active == True)

Getting names and emails of inactive users under 56:

# vanilla Python
results = [
    (user['name'], user['email']) for user in users
    if not user['is_active'] and user['age'] < 56
]

# lifter
results = manager.filter(User.is_active == False, User.age < 56)\
                 .values_list('name', 'email')

Getting all active users except the one with brown eyes and sort them by age:

# vanilla Python
raw_results = [
    user for user in users
    if user['is_active'] and not user['eye_color'] == 'brown'
]
results = sorted(raw_results, key=lambda v: v['age'])

# lifter
results = manager.filter(User.is_active == True)\
                 .exclude(User.eye_color == 'brown')\
                 .order_by('age')

Getting minimum and average women age:

# vanilla Python
from statistics import mean # Only in Python >=3.4
women_ages = [
    user['age'] for user in users
    if user['gender'] == 'female'
]
women_average_age = mean(women_ages)
minimum_woman_age = min(women_ages)

# lifter
results = manager.filter(User.gender='female')\
                 .aggregate((User.age, mean), (User.age, min))

As you can see, lifter's version is shorter and more readable than vanilla Python. It's also less error prone, especially if you're writing really complex queries, and quite familiar if you've already used an ORM.

Wanna know more? Have a look at the documentation!

lifter's People

Contributors

eliotberriot avatar youtux avatar pentusha avatar radarhere avatar johnfraney avatar mec-is avatar

Watchers

James Cloos avatar  avatar

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.