I want to share a recent addition to my network automation toolbox - Dq (Dictionary query).

Dq is a Cisco Genie library that provides various helper functions to ease the parsing of nested Python dictionaries.

For example, let's say we have the following Python dictionary.

>>> print(ospf_neighbors)
'ipv4': {
    'instance': {
        '1': {
            'areas': {
                '': {
                    'interfaces': {
                        'Ethernet1/1': {
                            'neighbors': {
                                '': {
                                    'router_id': '',
                                    'address': '',
                                    'state': 'full',
                                    'last_state_change': '1d22h',

Normally if we wanted to pull out the state for the neighbour we would need to perform a query much like the below (painful right?).

>>> ospf_neighbors['vrf']['default']['address_family']['ipv4']['instance']['1']['areas']['']['interfaces']['Ethernet1/1']['neighbors']['']['state']

With Dq, we can simply perform the following! Niiiice!

$ pip3 install genie.libs.parser==21.2
>>> from genie.utils import Dq
>>> Dq(ospf_neighbors).contains('').get_values('state', 0)
Become a Network Automation Jedi
Join our membership and enjoy:
  • Deep Dive Library
  • Community Support
  • Hands-on Examples
  • Multivendor Labs (beta)

100% Satisfaction Guaranteed!
Cancel your membership at any time.

Join our Community