Allows filtering iterable of dictionary or objects by another dictionary
Project description
QyPy
Query list of dictionaries or objects as if you are filtering in DBMS. You can get dicts/objects that are matched by OR, AND or NOT or all of them.
Installation
pip install QyPy
Usage
from query import Q
l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}]
filtered= Q(l,{'name__contains':"k", "age__lt":20})
print(list(filtered))
output
[{'name': 'Mike', 'age': '19'}]
The above filtration can be written as
from query import Q
l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}]
filtered= Q(l,name__contains="k", age__lt = 20)
Notes:
Q
returns an iterator which can be converted to a list by callinglist
.- Even though, age was
str
in the dict, as the value of in the query dict wasint
, QyPy converted the value in dict automatically to match the query data type. This behaviour can be stopped by passingFalse
toconvert_types
parameter.
Supported filters
eq
: equals and this default filtergt
: greater than.gte
: greater than or equal.lt
: less thanlte
: less than or equalin
: the value in a list of a tuple.- e.g. age__in=[10,20,30]
contains
: contains a substring as in the example.icontains
: case-insensitivecontains
.startswith
: checks if a value starts with a query strings.istartswith
: case-insensitivestartswith
.endswith
: checks if a value ends with a query strings.iendswith
: case-insensitiveendswith
.isnull
: checks if the value matches any of NULL_VALUES which are('', '.', None, "None", "null", "NULL")
- e.g.
filter__isnull=True
orfilter__isnull=False
- e.g.
For eq
,gt
,gte
,lt
,lte
, in
, contains
, icontains
, startswith
,istartswith
, endswith
and iendswith
, you can add a n
to negate the results. e.g nin
which is equivalent to not in
Advanced examples
This section will cover the use of OR
, AND
and NOT
Usage of OR
OR
or __or__
takes a list of dictionaries to evaluate and returns with the first True
.
from query import Q
l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}]
filtered= Q(l,{"OR":[{"name__contains":"k"}, {"age__gte":21}]})
print(list(filtered))
output
[{'name': 'Mike', 'age': '19'}, {'name': 'Sarah', 'age': '21'}]
Usage of NOT
NOT
or __not__
takes a dict for query run.
from query import Q
l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}]
filtered= Q(l,{"age__gt":15, "NOT":{"age__eq":19}})
print(list(filtered))
output
[{'name': 'John', 'age': '16'}, {'name': 'Sarah', 'age': '21'}]
Usage of AND
AND
or __and__
takes a list of dict for query run, returns with the first False
.
from query import Q
l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}]
filtered= Q(l,{"__and__":[{"age__gte":15},{"age__lt":21}]})
print(list(filtered))
output
[{'name': 'John', 'age': '16'}, {'name': 'Mike', 'age': '19'}]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file QyPy-0.9.2.tar.gz
.
File metadata
- Download URL: QyPy-0.9.2.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f15b975eaa9291f71ee9a6ed8f8e59e04e97b309069b6483d5ff67a3b9dc773 |
|
MD5 | cd2a873a9c699571a45f04b87d1b7368 |
|
BLAKE2b-256 | 1c87df0be49ec54c8f5c124abd2063cac824768915d116c3faef6d316227a9bb |