MySDQ is a simple and easy dictionary querer with an api close to the one of Django QuerySet
It is meant to quickly play around with some JSON/Dict data.
It supports all operator from the operator module (Yes even the ones that won’t work).
Think Django QuerySet when using it.
Installation
pip install mysdq
Quickstart
Data used in here can be found in here
In [1]: import json
In [2]: data = json.load(open('tests/users.json'))
In [3]: from mysdq import DictQuerer
In [4]: qs = DictQuerer(data)
In [5]: qs.count() == 7
Out[5]: True
In [8]: qs.get(nickname='yloking')
Out[8]:
{'address': {'city': 'Paris',
'name': 'rue du chatea',
'num': 169,
'zipcode': '75014'},
'age': 25,
'firstname': 'yosuke',
'lastname': 'loking',
'nickname': 'yloking',
'profiles': [{'name': 'twitter',
'url': 'https://twitter.com/yloking/',
'username': 'yloking'},
{'name': 'github',
'url': 'https://github.com/yloking/',
'username': 'yloking'},
{'name': 'reddit',
'url': 'https://reddit.com/yloking/',
'username': 'yloking'}]}
# Querying non matching entry returns None
In [9]: qs.get(lastname='young', age__le=20)
# Querying an entry and requesting only 2 attributes
In [11]: qs.filter(lastname='young', age__gt=20).values('nickname', 'age')
Out[11]: [{'age': 35, 'nickname': 'kyoung'}]
# Querying a sub key
In [12]: qs.filter(address__zipcode='44000').values('nickname', 'age', 'address')
Out[12]:
[{'address': {'city': 'Nantes',
'name': 'cheval blanc',
'num': 12,
'zipcode': '44000'},
'age': 35,
'nickname': 'kyoung'}]
# Querying a item in a list
In [13]: qs.filter(profiles__0__url__contains='kwame')
Out[13]:
[{'age': 24,
'nickname': 'kkwame',
'profiles': [{'name': 'twitter',
'url': 'https://twitter.com/kkwame/',
'username': 'kkwame'},
{'name': 'github',
'url': 'https://github.com/kkwame/',
'username': 'kkwame'},
{'name': 'reddit',
'url': 'https://reddit.com/kkwame/',
'username': 'kkwame'}]}]
# Ordering by attribute
In [14]: qs.order_by('age').values('nickname', 'age')
Out[14]:
[{'age': 15, 'nickname': 'tblack'},
{'age': 24, 'nickname': 'kkwame'},
{'age': 25, 'nickname': 'yloking'},
{'age': 25, 'nickname': 'jrodriguez'},
{'age': 28, 'nickname': 'jkouka'},
{'age': 32, 'nickname': 'dmccrey'},
{'age': 35, 'nickname': 'kyoung'}]
# Grouping by attribute
In [16]: res = qs.group_by('age')
In [17]: assert len(res[25]) == 2
In [18]: len(res[25])
Out[18]: 2
# Apply a function to an attribute
In [19]: qs.apply(lambda x: x*2, 'age').values('nickname', 'age')
Out[19]:
[{'age': 30, 'nickname': 'tblack'},
{'age': 48, 'nickname': 'kkwame'},
{'age': 50, 'nickname': 'yloking'},
{'age': 50, 'nickname': 'jrodriguez'},
{'age': 56, 'nickname': 'jkouka'},
{'age': 64, 'nickname': 'dmccrey'},
{'age': 70, 'nickname': 'kyoung'}]
That’s pretty much it. For more filter attribute, just check the code :wink:.
Metadata
Release files for mysdq 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mysdq-0.4.tar.gz | 4.6 kB | Details |
Release files / mysdq-0.4.tar.gz
| Download URL | mysdq-0.4.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7877ae9ad500c2ee11142cb38ec427bb119221c3c2f68aefe930117193bf867b
|
|
BLAKE2b-256 checksum How to use checksums |
a00240b9f53b7193d6a83e0dae69ff0478b0ffd1f55f399fe602e69d810079f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.8
|