Simple Django queryset like dict querer
Project description
MySDQ is a simple and easy dictionary querer with an api close to the one of Django QuerySet
It is meant to be used just to quickly play around with some JONS/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
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:.
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
mysdq-0.3.tar.gz
(4.6 kB
view details)
File details
Details for the file mysdq-0.3.tar.gz
.
File metadata
- Download URL: mysdq-0.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79d1d8f12d8817883726d15f3f08a2f2d936d93ced0bcfb7642540fe04008b89 |
|
MD5 | fc5d7c1c2caed9ca4a1b5827436cfaac |
|
BLAKE2b-256 | a7016a0929db42dd51ad546c6bea0b89cef7e5f4bc804fec8561bb336fd861ea |