Coder Social home page Coder Social logo

lbzhao28 / zato-redis-paginator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from weizhong1988/zato-redis-paginator

0.0 2.0 0.0 106 KB

Django-like Redis pagination for Python

License: BSD 3-Clause "New" or "Revised" License

Shell 0.56% Python 99.44%

zato-redis-paginator's Introduction

Django-like Redis pagination for Python

Except for the _init_ method, this is 100% API compatible with Django's own pagination but works with Redis instead of SQL databases.

Supported data types:

  • lists
  • sorted sets
  • ranges of sorted sets between min/max scores

The first two don't do it but the last one needs to fetch an entire result between min and max score to calculate the pages. This is because ZRANGEBYSCORE doesn't have any means to obtain the number of results that would've been returned without actually returning them.

Working with lists

Establishes a connection, creates a list and splits it into pages.

    from uuid import uuid4
    from redis import StrictRedis
    from zato.redis_paginator import ListPaginator
    
    conn = StrictRedis()
    key = 'paginator:{}'.format(uuid4().hex)
    
    for x in range(1, 18):
        conn.rpush(key, x)
        
    p = ListPaginator(conn, key, 6)
    
    print(p.count)      # 17
    print(p.num_pages)  # 3
    print(p.page_range) # [1, 2, 3]
    
    page = p.page(3)
    print(page)             # <Page 3 of 3>
    print(page.object_list) # ['13', '14', '15', '16', '17']
        
    conn.delete(key)

Working with sorted sets

Establishes a connection, creates a sorted set and splits it into pages.

    from uuid import uuid4
    from redis import StrictRedis
    from zato.redis_paginator import ZSetPaginator
    
    conn = StrictRedis()
    key = 'paginator:{}'.format(uuid4().hex)
    
    # 97-114 is 'a' to 'r' in ASCII
    for x in range(1, 18):
        conn.zadd(key, x, chr(96 + x))
        
    p = ZSetPaginator(conn, key, 6)
    
    print(p.count)      # 17
    print(p.num_pages)  # 3
    print(p.page_range) # [1, 2, 3]
    
    page = p.page(3)
    print(page)             # <Page 3 of 3>
    print(page.object_list) # ['m', 'n', 'o', 'p', 'q']
        
    conn.delete(key)

Working with sorted sets and min/max scores

Establishes a connection, creates a sorted set and splits it into pages while including only these members that are between min and max scores.

    from uuid import uuid4
    from redis import StrictRedis
    from zato.redis_paginator import ZSetPaginator
    
    conn = StrictRedis()
    key = 'paginator:{}'.format(uuid4().hex)
    
    # 97-114 is 'a' to 'r' in ASCII
    for x in range(1, 18):
        conn.zadd(key, x, chr(96 + x))
        
    p = ZSetPaginator(conn, key, 2, score_min=5, score_max=13)
    
    print(p.count)      # 9
    print(p.num_pages)  # 5
    print(p.page_range) # [1, 2, 3, 4, 5]
    
    page = p.page(3)
    print(page)             # <Page 3 of 5>
    print(page.object_list) # ['i', 'j']
        
    conn.delete(key)

Changelog

  • Aug 11, 2013 - Initial 1.0 release

License

BSD 3-clause license, see LICENSE.txt for terms and conditions.

More information

Originally part of Zato - ESB, SOA and cloud integrations in Python - https://zato.io/docs/

zato-redis-paginator's People

Watchers

 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.