cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1264
Views
0
Helpful
0
Comments
Shaun Roberts
Cisco Employee
Cisco Employee

Howdy out there in Automation Land and Happy New Year to all! I have been going over many training topics to put out and this one came up because there are so many folks who have learned and use Python in Cisco that I thought it would be a good time to take something people know well and translate it into maybe something they do not know as well. Hence translating from Python to CPO! We are going to write a coupe of quick examples in python and then show that same setup/code in CPO. We will be looking at a simple loop with conditionals and then making an API call and processing the data it returns. Since we are talking translation I thought this movie poster works.....

 

pondering_automation_LIT.JPG

 

Let's look at the examples we will use....

 

Example 1 - Let's take a for loop and go from 0 to 10. Along the way, let the user know if we have 10 or if the number we found is greater than 5 or if it is less than or equal to 5.

 

In python, pretty straight forward right?

print("****EXAMPLE 2****")
for x in range(11):
    if x == 10:
        print("X is 10!")
    elif x>5:
        print("X is bigger than 5!")
    else:
        print("X is 5 or less!")




 

While we will do the construction in the video below, you must understand some basic concepts here for CPO.

 

  1. For loops are For-Each loops in CPO and more-so made to go through tables... however we can do the above just fine
  2. Conditionals are pretty much the same
  3. Print statements do not really exists since each activity prints out what it does, however we will do a pseudo-print statement

 

Here is a preview about how that looks in CPO(to see more details you must watch the video!)

pondering_automation_python_example1.JPG

 

Pretty neat to compare side by side, right?

 

Onto Example 2... in example 2 we will make an API call to get the exchange rate of a currency and then convert one currency to the other!

The API is at Foreign exchange rates and currency conversion API

 

In python it looks like this...

 

print("Example 2")

#Running an API call against an API and then working with the data

import requests

from_currency = 'USD'

from_amount = 10.00

to_currency = 'GBP'

r = requests.get('https://api.fixer.io/latest', params={'base':from_currency})

print(r.status_code)

print(r.text)

print(r.json())

r_json = r.json()

converted_amount = r_json['rates'][to_currency] * from_amount

print("On {} converting ${:4.2f} {} to {} is ${:4.2f}.".format(r_json['date'], from_amount, from_currency, to_currency, converted_amount))

Now, I did change the call above from the video to exactly match CPO's setup... just FYI.

 

So in the above what things do we need to learn in the translation to CPO?

 

  1. What a target is and how the above code has a target, but you do not really realize it
  2. How to handle input and output variables
  3. How to handle the API calls and using CPO's built-in web API library (similar in nature to request python library)

 

Here is your preview for CPO:

 

pondering_automation_python_example2.JPG

See? Not too bad either! But I would wager you want to see the great insides and all the gritty details right? Well then time to watch the video!

 

Onto the VIDEO!

 

 

 

 

 

 

Play recording (45 min 7 sec)

Recording password: PqP4sMBu

 

 

 

 

 

 

 

 

 

 

 

 

 

Standard End-O-Blog Disclaimer:

 

Thanks as always to all my wonderful readers and those who continue to stick with and use CPO. Big things are on the horizon and I hope that you will continue to use CPO and find great uses for it! If you have a really exciting automation story, please email me it! (see below) I would love to compile some stories and feature customers or individual stories in an upcoming blog!!!

 

AUTOMATION BLOG DISCLAIMER: As always, this is a blog and my (Shaun Roberts) thoughts on CPO and automation, my thoughts on best practices, and my experiences with the product and customers. The above views are in no way representative of Cisco or any of it's partners, etc. None of these views, etc are supported and this is not a place to find standard product support. If you need standard product support please do so via the current call in numbers on Cisco.com or email tac@cisco.com

 

Thanks and Happy Automating!!!

 

--Shaun Roberts

shaurobe@cisco.com

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: