This package provides a QueryList class with django ORM-esque filtering, excluding, and getting for lists. It also provides BetterDict, a dot lookup/assignment capable wrapper for dicts that is 100% backwards compatible.
Project description
# querylist
[![Build Status](https://img.shields.io/travis/thomasw/querylist.svg)](https://travis-ci.org/thomasw/querylist)
[![Coverage Status](https://img.shields.io/coveralls/thomasw/querylist.svg)](https://coveralls.io/r/thomasw/querylist)
[![Latest Version](https://img.shields.io/pypi/v/querylist.svg)](https://pypi.python.org/pypi/querylist/)
[![Downloads](https://img.shields.io/pypi/dm/querylist.svg)](https://pypi.python.org/pypi/querylist/)
Sick of for loop + conditional soup when dealing with complicated lists?
Querylist is here to help.
This package provides a data structure called a QueryList, an extension of
Python's built in list data type that adds django ORM-eseque filtering,
exclusion, and get methods. QueryLists allow developers to easily query and
retrieve data from complex lists without the need for unnecessarily verbose
iteration and selection cruft.
The package also provides BetterDict, a backwards-compatible wrapper for
dictionaries that enables dot lookups and assignment for key values.
Take a look at the [complete
documentation](https://querylist.readthedocs.org/) for more information.
## Installation
Querylist can be installed like any other python package:
> pip install querylist
Querylist is tested against Python 2.6, 2.7, 3.3, 3.4, and pypy.
## Usage
### BetterDicts
BetterDicts wrap normal dicts. They have all of the same functionality one
would expect from a normal dict:
>>> from querylist import BetterDict
>>> src = {'foo': 'bar', 'items': True}
>>> bd = BetterDict(src)
>>> bd == src
True
>>> bd['foo']
'bar'
>>> bd.items()
[('items', True), ('foo', 'bar')]
However, BetterDicts can also preform dot lookups and assignment of key
values!
>>> bd.bar_time = True
>>> bd.foo = 'meh'
>>> bd.foo
'meh'
>>> bd.bar_time
True
>>> bd['bar_time']
True
Key values that conflict with normal dict attributes are accessible via a
`_bd_` attribute.
>>> bd.items
<built-in method items of BetterDict object at 0x10d3a7fb0>
>>> bd._bd_.items
True
[More about BetterDicts >>](https://querylist.readthedocs.org/en/latest/betterdict.html)
### QueryLists
QueryLists work just like lists:
>>> from querylist import QueryList
>>> site_list = [
{
'url': 'http://site1.tld/',
'meta': {
'keywords': ['Mustard', 'kittens'],
'description': 'My cool site'
},
'published': True,
'id': 1,
'name': 'Site 1'
}, {
'url': 'http://site2.tld/',
'meta': {
'keywords': ['Catsup', 'dogs'],
'description': 'My cool site'
},
'published': True,
'id': 2,
'name': 'SitE 2'
}, {
'url': 'http://site3.tld/',
'meta': {
'keywords': ['Mustard', 'kittens'],
'description': 'My cool site'
},
'published': False,
'id': 3,
'name': 'Site 3'
}
]
>>> ql = QueryList(site_list)
>>> ql == site_list
True
They also let developers, exclude objects that don't match criteria via field
lookups or filter the QueryList to only the objects that do match a provided
criteria:
>>> ql.exclude(published=True)
[{'url': 'http://site3.tld/', 'meta': {'keywords': ['Mustard', 'kittens'], 'description': 'My cool site'}, 'id': 3, 'name': 'Site 3', 'published': False}]
>>> ql.filter(published=True).exclude(meta__keywords__contains='Catsup')
[{'url': 'http://site1.tld/', 'meta': {'keywords': ['Mustard', 'kittens'], 'description': 'My cool site'}, 'id': 1, 'name': 'Site 1', 'published': True}]
And finally, they let developers retrieve specific objects with the get
method:
>>> ql.get(id=2)
{'url': 'http://site1.tld/', 'meta': {'keywords': ['Mustard', 'kittens'], 'description': 'My cool site'}, 'id': 2, 'name': 'Site 1', 'published': True}
By default, QueryLists work exclusively with lists of dictionaries. This is
achieved partly by converting the member dicts to BetterDicts on
instantiation. QueryLists also supports lists of any objects that support dot
lookups. `QueryList.__init__()` has parameters that let users easily convert
lists of dictionaries to custom objects. Consider the `site_list` example
above: instead of just letting the QueryList default to a BetterDict, we could
instantiate it with a custom Site class that provides methods for publishing,
unpublishing, and deleting sites. That would then allow us to write code like
the following, which publishes all unpublished sites:
>>> from site_api import Site
>>> ql = QueryList(site_list, wrap=Site)
>>> [x.publish() for x in ql.exclude(published=True)]
[More about QueryLists >>](https://querylist.readthedocs.org/en/latest/querylist.html)
## Contributing
1. Fork the repo and then clone it locally.
2. Install the development requirements: `pip install -r requirements.txt` (
use `requirements26.txt` for python 2.6)
3. Use [testtube](https://github.com/thomasw/testtube/)'s `stir` command
(installed via #2) to monitor the project directory for changes and
automatically run the test suite.
4. Make changes and submit a pull request.
At the moment, Querylist has great test coverage. Please do your part to help
keep it that way by writing tests whenever you add or change code.
## Everything else
Copyright (c) [Thomas Welfley](http://welfley.me). See
[LICENSE](https://github.com/thomasw/querylist/blob/master/LICENSE) for
details.
[![Build Status](https://img.shields.io/travis/thomasw/querylist.svg)](https://travis-ci.org/thomasw/querylist)
[![Coverage Status](https://img.shields.io/coveralls/thomasw/querylist.svg)](https://coveralls.io/r/thomasw/querylist)
[![Latest Version](https://img.shields.io/pypi/v/querylist.svg)](https://pypi.python.org/pypi/querylist/)
[![Downloads](https://img.shields.io/pypi/dm/querylist.svg)](https://pypi.python.org/pypi/querylist/)
Sick of for loop + conditional soup when dealing with complicated lists?
Querylist is here to help.
This package provides a data structure called a QueryList, an extension of
Python's built in list data type that adds django ORM-eseque filtering,
exclusion, and get methods. QueryLists allow developers to easily query and
retrieve data from complex lists without the need for unnecessarily verbose
iteration and selection cruft.
The package also provides BetterDict, a backwards-compatible wrapper for
dictionaries that enables dot lookups and assignment for key values.
Take a look at the [complete
documentation](https://querylist.readthedocs.org/) for more information.
## Installation
Querylist can be installed like any other python package:
> pip install querylist
Querylist is tested against Python 2.6, 2.7, 3.3, 3.4, and pypy.
## Usage
### BetterDicts
BetterDicts wrap normal dicts. They have all of the same functionality one
would expect from a normal dict:
>>> from querylist import BetterDict
>>> src = {'foo': 'bar', 'items': True}
>>> bd = BetterDict(src)
>>> bd == src
True
>>> bd['foo']
'bar'
>>> bd.items()
[('items', True), ('foo', 'bar')]
However, BetterDicts can also preform dot lookups and assignment of key
values!
>>> bd.bar_time = True
>>> bd.foo = 'meh'
>>> bd.foo
'meh'
>>> bd.bar_time
True
>>> bd['bar_time']
True
Key values that conflict with normal dict attributes are accessible via a
`_bd_` attribute.
>>> bd.items
<built-in method items of BetterDict object at 0x10d3a7fb0>
>>> bd._bd_.items
True
[More about BetterDicts >>](https://querylist.readthedocs.org/en/latest/betterdict.html)
### QueryLists
QueryLists work just like lists:
>>> from querylist import QueryList
>>> site_list = [
{
'url': 'http://site1.tld/',
'meta': {
'keywords': ['Mustard', 'kittens'],
'description': 'My cool site'
},
'published': True,
'id': 1,
'name': 'Site 1'
}, {
'url': 'http://site2.tld/',
'meta': {
'keywords': ['Catsup', 'dogs'],
'description': 'My cool site'
},
'published': True,
'id': 2,
'name': 'SitE 2'
}, {
'url': 'http://site3.tld/',
'meta': {
'keywords': ['Mustard', 'kittens'],
'description': 'My cool site'
},
'published': False,
'id': 3,
'name': 'Site 3'
}
]
>>> ql = QueryList(site_list)
>>> ql == site_list
True
They also let developers, exclude objects that don't match criteria via field
lookups or filter the QueryList to only the objects that do match a provided
criteria:
>>> ql.exclude(published=True)
[{'url': 'http://site3.tld/', 'meta': {'keywords': ['Mustard', 'kittens'], 'description': 'My cool site'}, 'id': 3, 'name': 'Site 3', 'published': False}]
>>> ql.filter(published=True).exclude(meta__keywords__contains='Catsup')
[{'url': 'http://site1.tld/', 'meta': {'keywords': ['Mustard', 'kittens'], 'description': 'My cool site'}, 'id': 1, 'name': 'Site 1', 'published': True}]
And finally, they let developers retrieve specific objects with the get
method:
>>> ql.get(id=2)
{'url': 'http://site1.tld/', 'meta': {'keywords': ['Mustard', 'kittens'], 'description': 'My cool site'}, 'id': 2, 'name': 'Site 1', 'published': True}
By default, QueryLists work exclusively with lists of dictionaries. This is
achieved partly by converting the member dicts to BetterDicts on
instantiation. QueryLists also supports lists of any objects that support dot
lookups. `QueryList.__init__()` has parameters that let users easily convert
lists of dictionaries to custom objects. Consider the `site_list` example
above: instead of just letting the QueryList default to a BetterDict, we could
instantiate it with a custom Site class that provides methods for publishing,
unpublishing, and deleting sites. That would then allow us to write code like
the following, which publishes all unpublished sites:
>>> from site_api import Site
>>> ql = QueryList(site_list, wrap=Site)
>>> [x.publish() for x in ql.exclude(published=True)]
[More about QueryLists >>](https://querylist.readthedocs.org/en/latest/querylist.html)
## Contributing
1. Fork the repo and then clone it locally.
2. Install the development requirements: `pip install -r requirements.txt` (
use `requirements26.txt` for python 2.6)
3. Use [testtube](https://github.com/thomasw/testtube/)'s `stir` command
(installed via #2) to monitor the project directory for changes and
automatically run the test suite.
4. Make changes and submit a pull request.
At the moment, Querylist has great test coverage. Please do your part to help
keep it that way by writing tests whenever you add or change code.
## Everything else
Copyright (c) [Thomas Welfley](http://welfley.me). See
[LICENSE](https://github.com/thomasw/querylist/blob/master/LICENSE) for
details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
querylist-0.4.0.tar.gz
(128.8 kB
view details)
Built Distributions
querylist-0.4.0-py2-none-any.whl
(13.5 kB
view details)
File details
Details for the file querylist-0.4.0.tar.gz
.
File metadata
- Download URL: querylist-0.4.0.tar.gz
- Upload date:
- Size: 128.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd6167e10e93ddb901960b6dac9cf5936d87ab384feea278b07070c287511dd5 |
|
MD5 | 1775ae66e5fd9f6c33a0c2a1282c2625 |
|
BLAKE2b-256 | 96253b073a6a5ea1c4d3cfbe9e5f4059ee1beed65c83282361d796a1dd6f4ac4 |
File details
Details for the file querylist-0.4.0-py2.py3-none-any.whl
.
File metadata
- Download URL: querylist-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a3ec57cda338527586efb1a5f2abfb6895ffe145c381b2f2c0454fd3fbe08e3 |
|
MD5 | d731ace6df30a4a4f8e9c818a26d00fb |
|
BLAKE2b-256 | 7a9a961112ce4a83199d3be48cff7aa8697418f3be3beed2efa763616a2e7c66 |
File details
Details for the file querylist-0.4.0-py2-none-any.whl
.
File metadata
- Download URL: querylist-0.4.0-py2-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d83e4d8fc367ee056c652f5ea82b4e35a629f6bdff8c2245f223f3a9a7837d12 |
|
MD5 | 8001d8e61b6b54f266d0c793e724e091 |
|
BLAKE2b-256 | 3b80948c031840efa76f06e5e4f416f6e893ac176ec8032fa9c4107fa32cfc3b |