Giter Club home page Giter Club logo

collection-filter's Introduction

Python collection filter

https://github.com/kudo/collection-filter

Build Status Unit Test Coverage

This library provides a DSL filter for list or dict data. It is original designed for RESTful API partial response. Tested with Python 2.6, 2.7.

Quick Start

To install, use pip:

$ pip install collection-filter

Then:

$ python

>>> from collection_filter import collection_filter

# Query a dict
>>> collection_filter({'foo': 1, 'bar': 2}, 'foo')
{'foo': 1}

# Query a dict with multiple elements (seperated by comma)
>>> collection_filter({'foo': 1, 'bar': 2}, 'foo,bar')
{'foo': 1, 'bar': 2}

# Query a dict deeply
>>> collection_filter({'foo': {'bar': 2, 'orange': 'sweet'}}, 'foo.bar')
{'foo': {'bar': 2}}

# Query a list
>>> collection_filter([{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4}], '[].foo')
[{'foo': 1}, {'foo': 3}]

# list/dict combination example
>>> collection_filter({'foo': 'bar', 'aListOfDict': [{'foo': 1, 'bar': 2}]}, 'aListOfDict[].foo')
{'aListOfDict': [{'foo': 1}]}

Syntax

Currently support operators

Operator Description
, Multiple field query sperated by comma
[] To indicate the data should be handled as list
. Dot notation should follow with a symbol name which to specify a field in current dict data

Copyright and License

Copyright 2015, Kudo Chien

Licensed under a MIT license.

collection-filter's People

Contributors

kudo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

collection-filter's Issues

Exception for deep list filter

Given

collection_filter({'aList': [{'anotherList': [{'foo': 1, 'bar': 2}]}]}, 'aList[].anotherList[].foo')

Will throw exception:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-11-0e52659d65b7> in <module>()
----> 1 collection_filter({'aList': [{'anotherList': [{'foo': 1, 'bar': 2}]}]}, 'aList[].anotherList[].foo')

/Users/kudo/.virtualenvs/biienv/lib/python2.7/site-packages/collection_filter-0.5-py2.7.egg/collection_filter/collection_filter.pyc in collection_filter(data, fields)
    111         # [2] For each dot notated sub field, do further query recursively
    112         next_field, remain_query = _get_next_field(field)
--> 113         subset = _inner_filter(copy.deepcopy(data), next_field, remain_query)
    114         if data_as_list:
    115             # [3-1] For list, set each element as merged dictionary

/Users/kudo/.virtualenvs/biienv/lib/python2.7/site-packages/collection_filter-0.5-py2.7.egg/collection_filter/collection_filter.pyc in _inner_filter(data, field, remain_query, expect_list)
     80                                    next_field,
     81                                    remain_query,
---> 82                                    expect_list=expect_list)
     83
     84         # Return {field: result} if there are existed next query

/Users/kudo/.virtualenvs/biienv/lib/python2.7/site-packages/collection_filter-0.5-py2.7.egg/collection_filter/collection_filter.pyc in _inner_filter(data, field, remain_query, expect_list)
     51                                       field,
     52                                       remain_query,
---> 53                                       expect_list=False)
     54         return data
     55     else:

/Users/kudo/.virtualenvs/biienv/lib/python2.7/site-packages/collection_filter-0.5-py2.7.egg/collection_filter/collection_filter.pyc in _inner_filter(data, field, remain_query, expect_list)
     80                                    next_field,
     81                                    remain_query,
---> 82                                    expect_list=expect_list)
     83
     84         # Return {field: result} if there are existed next query

/Users/kudo/.virtualenvs/biienv/lib/python2.7/site-packages/collection_filter-0.5-py2.7.egg/collection_filter/collection_filter.pyc in _inner_filter(data, field, remain_query, expect_list)
     55     else:
     56         # `data` is dict
---> 57         assert type(data) == dict
     58         if not field:
     59             # If current field query is empty, try to fetch next query

AssertionError:

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.