Simplify Parsing with Cisco's Genie Dq

Simplify Parsing with Cisco's Genie Dq

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'

Subscribe to our newsletter to keep updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!