Skip to main content

Python Simple Data Query

Project description

dq.py - Python DataQuery
=========================
Provides the ability to query a data source made up of any combination
of lists and dicts.

To use from within Python:
```python
>>> from dq import query
>>> dq.query('[0].name', [{'name': 'michael'}, {'name': 'jane'}])
'michael'

>>> dq.query('..name', [{'name': 'michael'}, {'name': 'jane'}])
['michael', 'jane']
```
Installation
------------
To install dq:
```
pip install dq
```
dq does not depend on any non-standard libraries

Query Language
--------------
The query language is a much simplified version of [json path][1]. The subset
is selected to allow for simple structural querying of data - no filters or
expressions. This keeps the parser nice and simple, and since its a Python
library you have the full power of Python to perform complex filters.

The query language supports the following path syntax:
```
.key - Returns the given key from the dict
..key - Returns the given key from any descendant dictionary
[index] - Returns an item from a list at the specified index
[*] - Returns all items in a list
[start:stop:step] - Performs a python slice operation on a list
```

Query Examples
--------------
These syntax items can be combined to build complex queries.
```python
>>> from dq import query
>>> d = {}
>>> d['one'] = 1
>>> d['two'] = [1,2,3,4,5,6,7,8,9,10]
>>> d['three'] = [{'name': 'john'}, {'name': 'mary'}, {'one': 'guy'}]

>>> query('.one', d)
1

>>> query('.two', d)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> query('.two[1]', d)
2

>>> query('.two[*]', d)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> query('.two[2:-2]', d)
[3, 4, 5, 6, 7, 8]

>>> query('.two[1:6:2]', d)
[2, 4, 6]

>>> query('.three[1].name', d)
'mary'

>>> query('.three[*].name', d)
['john', 'mary']

>>> query('..name', d)
['john', 'mary']

>>> query('..one', d)
[1, 'guy']
```

Compiled Queries
----------------
When using the same query string repeatedly a CompiledQuery can be constructed
to prevent parsing the query string multiple times:
```python
>>> from dq import compiled
>>> query = compiled('[1]')
>>> query([1,2,3,4])
2
>>> query([5,6,7,8])
6
```

Thanks
-------
This library is based on the ideas and code from [python-jsonpath-rw][2].

Copyright
---------
Copyright (c) 2013 Michael Lamb. See LICENSE.txt for details.

[1]: http://goessner.net/articles/JsonPath/ "Json Path"
[2]: https://github.com/kennknowles/python-jsonpath-rw "python-jsonpath-rw"

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

dq-0.1.2.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file dq-0.1.2.tar.gz.

File metadata

  • Download URL: dq-0.1.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dq-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0a375f1385af15257660ef23641e9de706dfd227d5b713af1497cb2fe0c64888
MD5 d075238cb5613aa2cf22f479ae972018
BLAKE2b-256 c3b8caec444f580ef5d0eaf4b78fcbb73d69b33e145ca91dc1eb3520fead9953

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page