Like many coders, engineers, and people who ‘code tinker’ in the small hours of the morning, I am always looking at new ways to improve my code. This is for many reasons. For example, Comprehensions in Python can provide us with a short and sweet way to construct new sequences. They are a concise and speedy way to create new lists.
Python built-in functions can save you time
Python has many great built-in functions. Often, I have spent ages trying to write the best code I can only to discover one of the many underlying libraries would have saved me hours (if not days!). Take a look at these Python library functions to check if you are replicating any of these built-in functions within your code. These are tips and tricks you may acquire overtime or by having someone review your code and send you a pull request.
Recently, I was asked to present a workshop on network automation. I had presented on this subject two years ago, but two years can be a lifetime in the code world. I needed to fix this up, as there have been some changes to the API. I took the opportunity to update the code end-to-end. Now you may think I live dangerously (as I do all my demo’s live!). And I know that performance matters, as does output. So I used two Python libraries to give me the performance and output quality I wanted – Scrapli and Rich.
Scrapli is fast… really fast
Last year, one of our DevNet community members tweeted about Scrapli, the brain child of super genius Carl Montanari. The name scrapli — is just “scrape cli” (as in screen scrape) squished together! scrapli’s goal is to be as fast and flexible as possible while providing a thoroughly tested, well-typed, well-documented, simple API that supports both synchronous and asynchronous usage. I gave it a try and it was super cool and fast too.
But there is more, by default Scrapli uses ssh, but now you can use netconf over ssh!
“scrapli_netconf is a netconf driver built on top of scrapli. The purpose of scrapli_netconf is to provide a fast,
flexible, thoroughly tested, well typed, well documented, simple API that supports both synchronous and
asynchronous usage. Working together scrapli and scrapli_netconf aim to provide a consistent (as is practical)
look and feel when automating devices over telnet, SSH, or netconf (over SSH)”
Rich API makes your code shine
So our code is now faster thanks to Scrapli. Can we make it look more pretty? The answer to that is yes, with Rich. Rich (which is written by full-stack software developer, and Python expert Will McGugan) is a Python library for rich text and beautiful formatting in the terminal. The Rich API makes it easy to add color and style to terminal output. Rich can also render pretty tables, progress bars, markdown, syntax highlighted source code, tracebacks, and more — out of the box.
I tried this with pprint module and json. When getting a lot of json output back from the always-on sd-wan sandbox, using the sd-wan sdk, adding Rich really helps, as code can really pop, during demos, this is great as details show out really clearly.
I do a lot of work/output with tables and Rich can render flexible tables with unicode box characters, plus there is a large variety of formatting options for borders, styles, cell alignment, etc.
So now you know of these two really cool Python libraries, let’s see what you can build with them. When you are done building your code why not share it on DevNet Code Exchange. If you are looking for some more inspiration check out this code scrapli_netconf_BGP.
If you have any feedback let me know in the comments section below — leave me a message, ask me a question, whatever you want.
- Visit the DevNet Networking Dev Center to find and learn about your network automation use case
- Learn about Cisco DevNet professional certifications