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)
(snip)
'ipv4': {
    'instance': {
        '1': {
            'areas': {
                '0.0.0.0': {
                    'interfaces': {
                        'Ethernet1/1': {
                            'neighbors': {
                                '1.1.1.1': {
                                    'router_id': '1.1.1.1',
                                    'address': '10.1.1.1',
                                    'state': 'full',
                                    'last_state_change': '1d22h',
(snip)

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

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

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

$ pip3 install pyats[library]
>>> from genie.utils import Dq
>>> Dq(ospf_neighbors).contains('1.1.1.1').get_values('state', 0)
'full'
Master Network Automation
Join today and get access to:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 Multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Tech demos and sessions

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


Learn More