项目作者: msempere

项目描述 :
Data structures for Redis
高级语言: Python
项目地址: git://github.com/msempere/redis_structures.git
创建时间: 2014-12-30T14:00:54Z
项目社区:https://github.com/msempere/redis_structures

开源协议:MIT License

下载


Logo

Data structures for Redis

Table of Contents

Versions:

  • v0.1 Build Status Requirements Status
  • master Build Status Requirements Status

Structures:

  • Queue
  • Priority queue
  • Stack
  • Circular buffer
  • Deque

Setup:

  1. pip install -r requirements.txt
  1. python setup.py install

Usage:

  • STACK: LIFO data structure

    1. >>> from redis_structures import Stack
    2. >>> stack = Stack(name='dummy_stack', host='127.0.0.1', port=6379)
    3. >>> len(stack)
    4. 0
    5. >>> stack.push('a_element')
    6. >>> stack.push('another_element')
    7. >>> stack.push('one_more_element')
    8. # same as doing stack.addAll(['a_element', 'another_element', 'one_more_element'])
    9. >>> len(stack)
    10. 3
    11. >>> stack.content()
    12. ['one_more_element', 'another_element', 'a_element']
    13. >>> stack.pop()
    14. 'one_more_element'
    15. >>> stack.content()
    16. ['another_element', 'a_element']
    17. >>> stack.clear()
    18. True
    19. >>> stack.content()
    20. []
  • QUEUE: FIFO data structure

    1. >>> from redis_structures import Queue
    2. >>> queue = Queue(name='dummy_queue', host='127.0.0.1', port=6379)
    3. >>> len(queue)
    4. 0
    5. >>> queue.push('a_element')
    6. >>> queue.push('another_element')
    7. >>> queue.push('one_more_element')
    8. # same as doing queue.addAll(['a_element', 'another_element', 'one_more_element'])
    9. >>> len(queue)
    10. 3
    11. >>> queue.content()
    12. ['one_more_element', 'another_element', 'a_element']
    13. >>> queue.pop()
    14. 'a_element'
    15. >>> queue.content()
    16. ['one_more_element', 'another_element']
    17. >>> queue.clear()
    18. True
    19. >>> queue.content()
    20. []
    21. ``
  • PRIORITY QUEUE: Abstract data type where each element has a priority associated

    1. >>> from redis_structures import PriorityQueue
    2. >>> priority_queue = PriorityQueue(name='dummy_queue', host='127.0.0.1', port=6379)
    3. >>> len(priority_queue)
    4. 0
    5. >>> priority_queue.push('a_element', 20.0)
    6. >>> priority_queue.push('another_element', 40.0)
    7. >>> priority_queue.push('one_more_element', 10.0)
    8. # same as doing priority_queue.addAll([('a_element', 20.0), ('another_element', 40.0), ('one_more_element', 10.0)])
    9. >>> len(priority_queue)
    10. 3
    11. >>> priority_queue.content()
    12. [('one_more_element', 10.0), ('a_element', 20.0), ('another_element', 40.0)]
    13. >>> priority_queue.pop()
    14. ('another_element', 40.0)
    15. >>> priority_queue.content()
    16. [('one_more_element', 10.0), ('a_element', 20.0)]
    17. >>> priority_queue.clear()
    18. True
    19. >>> priority_queue.content()
    20. []
  • CIRCULAR BUFFER: Fixed-size buffer with connected end-to-end

    1. >>> from redis_structures import CircularBuffer
    2. >>> circular_buffer = CircularBuffer(name='dummy_cbuffer', size=3, host='127.0.0.1', port=6379)
    3. >>> len(circular_buffer)
    4. 0
    5. >>> circular_buffer.push('a_element')
    6. >>> circular_buffer.push('another_element')
    7. >>> circular_buffer.push('one_more_element')
    8. # same as doing circular_buffer.addAll(['a_element', 'another_element', 'one_more_element'])
    9. >>> len(circular_buffer)
    10. 3
    11. >>> circular_buffer.content()
    12. ['one_more_element', 'another_element', 'a_element']
    13. >>> circular_buffer.push('other_element!!') # overwriting old data because size=3
    14. >>> len(circular_buffer)
    15. 3
    16. >>> circular_buffer.content()
    17. ['other_element!!', 'one_more_element', 'another_element']
    18. >>> circular_buffer.pop()
    19. 'another_element'
    20. >>> circular_buffer.content()
    21. ['other_element!!', 'one_more_element',]
    22. >>> circular_buffer.clear()
    23. True
    24. >>> circular_buffer.content()
    25. []
  • DEQUE: Double-ended queue also called head-tail linked list

    1. >>> from redis_structures import Deque
    2. >>> deque = Deque(name='dummy_cbuffer', host='127.0.0.1', port=6379)
    3. >>> len(deque)
    4. 0
    5. >>> deque.back_push('a_element')
    6. >>> deque.back_push('another_element')
    7. >>> deque.back_push('one_more_element')
    8. # same as doing deque.addAll(['one_more_element', 'another_element', 'a_element'], back=True)
    9. >>> len(deque)
    10. 3
    11. >>> deque.content()
    12. ['one_more_element', 'another_element', 'a_element']
    13. >>> deque.front_push('other_element!!')
    14. >>> len(deque)
    15. 3
    16. >>> deque.content()
    17. ['one_more_element', 'another_element', 'a_element', 'other_element!!']
    18. >>> deque.back_pop()
    19. 'one_more_element'
    20. >>> deque.content()
    21. ['another_element', 'a_element', 'other_element!!']
    22. >>> deque.clear()
    23. True
    24. >>> deque.content()
    25. []