7.3. KeySearch (key)

class easydata.queries.key.KeySearch(query: str, **kwargs)[source]

Bases: easydata.queries.base.QuerySearch

KeyQuery (shortcut: key) allows you to select a value from a dictionary by key.

7.3.1. Examples

>>> test_dict = {"title": "EasyBook pro 15", "brand": {"name": "EasyData"}}
>>> ed.key('title').get(test_dict)
'EasyBook pro 15'
>>> ed.key('brand').get(test_dict)
{'name': 'EasyData'}

7.3.2. Pseudo keys

::values

::values pseudo key will ensure that only values are selected from a dictionary and returned as a list.

Example without *::values* pseudo key

>>> {'brand': {'name': 'EasyData', 'origin': 'Slovenia'}}
>>> ed.key('brand').get(test_dict)
{'name': 'EasyData', 'origin': 'Slovenia'}

Example with *::values* pseudo key

>>> {'brand': {'name': 'EasyData', 'origin': 'Slovenia'}}
>>> ed.key('brand::values').get(test_dict)
['EasyData', 'Slovenia']
::keys

::keys pseudo key will ensure that only keys are selected from a dictionary and returned as a list.

>>> ed.key('brand::keys').get(test_dict)
['name', 'origin']
::dict(<key>:<value>)

With ::dict we can convert any list of dictionary into a dictionary. ::dict must receive two parameters (key and value), which are value keys from from a list of dictionaries.

We will use following option dictionary in examples bellow:

>>> odict = {'options': [{'name': 'Monitor', 'stock': 'y'}, {'name': 'Mouse', 'stock': 'n'}]}

Example without *::dict* pseudo key

>>> ed.key('options').get(odict)
[{'name': 'Monitor', 'stock': 'y'}, {'name': 'Mouse', 'stock': 'n'}]

Example with *::dict* pseudo key

>>> ed.key('options::dict(name:stock)').get(odict)
{'Monitor': 'y', 'Mouse': 'n'}