You are on page 1of 22

ROGERS- AUTOMATION

PLATFORMS
(PROJECT ID#30763)

Knowledge Transfer Session#1


April 1, 2020

© 2019 Juniper Networks


Juniper Business
Juniper Use Only
Internal
AGENDA
Day 1 – Session 1

Topics

• Introduction to Python
• Presenter : Yajna and Abhishek

• Introduction to DevOps
• Presenter : Abhishek

• LUNCH BREAK

• Introduction to Jenkins
• Presenter : Abhishek

• Introduction to Gitlab
• Presenter : Yajna

© 2019 Juniper Networks 2


Juniper Business
Juniper Use Only
Internal
AGENDA
• Why learn Python

• History

• Characteristics of Python

• Python Syntax

• Python Data Types

• Functions

• Main function

• Modules Identifiers Conventions

• Reserved Words Quotations And Error Handling

• Regular Expressions

• OOPs
© 2019 Juniper Networks 3
Juniper Business
Juniper Use Only
Internal
WHY LEARN A PROGRAMMING LANGUAGE

FOR NETWORK ENGINEER

• Large number of python libraries exist that help


with network automation transforming python into
powerful automation tool

• Ability to extend and customize tools like Ansible


and Salt with custom python modules

• Understand and debug existing tools by reading


source code

© 2019 Juniper Networks 4


Juniper Business
Juniper Use Only
Internal
HISTORY

ABOUT PYTHON

• Python is an object-oriented programming language created


by Dutch programmer named Guido Rossum in 1989
• He named it after the television show Monty Python's Flying
Circus
• Many large companies use the Python programming
language include NASA, Google, YouTube, BitTorrent, etc.
• Python programming is widely used in Artificial Intelligence,
Natural Language Generation, Neural Networks and other
advanced fields of Computer Science
• Python source code is also available under the GNU General
Public License (GPL)

© 2019 Juniper Networks 5


Juniper Business
Juniper Use Only
Internal
ABOUT PYTHON

SALIENT FEATURES SALIENT FEATURES SALIENT FEATURES

• Python is a high-level, interpreted, • Python is Interpreted − Python is • Python is Object-Oriented − Python


interactive and object-oriented processed at runtime by the supports Object-Oriented style or
scripting language. interpreter. You do not need to technique of programming that
compile your program before encapsulates code within objects.
executing it. This is similar to PERL and
• Python is designed to be highly PHP.
readable. It uses English keywords • Python is a Beginner's Language −
frequently where as other languages Python is a great language for the
use punctuation, and it has fewer • Python is Interactive − You can beginner-level programmers and
syntactical constructions than other actually sit at a Python prompt and supports the development of a wide
languages. interact with the interpreter directly to range of applications from simple text
write your programs. processing to browsers to games.

© 2019 Juniper Networks 6


Juniper Business
Juniper Use Only
Internal
CHARACTERISTICS

CHARACTERISTICS

• It supports functional and structured programming methods as well as OOP

• It can be used as a scripting language or can be compiled to byte-code for building large applications.

• It provides very high-level dynamic data types and supports dynamic type checking.

• It supports automatic garbage collection.

• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

Program execution

Source Code is run Python code is Source Code is run


START through python translated to byte through python STOP
interpreter (.py) code (.pyc) interpreter (.py)

© 2019 Juniper Networks 7


Juniper Business
Juniper Use Only
Internal
APPLICATIONS

APPLICATIONS OF PYTHON

• Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax
• Easy-to-read − Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain − Python's source code is fairly easy-to-maintain.
• A broad standard library − Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and
Macintosh.
• Interactive Mode − Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
• Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms.
• Extendable − You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize
their tools to be more efficient.
• Databases − Python provides interfaces to all major commercial databases.
• GUI Programming − Python supports GUI applications that can be created and ported to many system calls, libraries and windows
systems, such as Windows MFC, Macintosh, and the X Window system of Unix.
• Scalable − Python provides a better structure and support for large programs than shell scripting.

© 2019 Juniper Networks 8


Juniper Business
Juniper Use Only
Internal
PYTHON SYNTAX

SYNTAX

© 2019 Juniper Networks 9


Juniper Business
Juniper Use Only
Internal
PYTHON DATA TYPES

IMMUTABLE DATATYPES MUTABLE DATATYPES

• Number: This datatype used to store


numerical values. (int, float, complex) • Lists: A list is a data structure
in Python that is a mutable, or
• Strings: Strings datatype in Python are changeable, ordered sequence of
used to store textual information. elements
• Python dictionary is an unordered
collection of items. Dictionary has a
• A tuple is a collection which is ordered key: value pair.
and unchangeable.
• A set is an unordered collection of
items with no duplicate elements.
FEW EXAMPLES

• x = "Hello World” x = ["apple", "banana", "cherry"]


• x = 20 x = {"name" : "John", "age" : 36}
• x = ("apple", "banana", "cherry”, ”apple”) x= {"apple", "banana", "cherry"}

© 2019 Juniper Networks 10


Juniper Business
Juniper Use Only
Internal
FUNCTION IN PYTHON

BUILT-IN FUNCTIONS USER-DEFINED FUNCTIONS ANONYMOUS FUNCTIONS

The Python interpreter has a number of User-defined functions are written by Anonymous functions are also called
functions and types built into it that are the developers to meet certain lambda functions in Python because
always available requirements. instead of declaring them with the
• sum() Function definition standard def keyword,
the lambda keyword is used.
• min() Function call
• max() Function arguments
Example: double = lambda x: x * 2
• set() Global vs Local Variables
print(double(5))
• sorted()

PYTHON RECURSION

A recursive function is a function defined in terms of itself via


self-referential expressions.
Example: Factorial , Fibonacci series solution

© 2019 Juniper Networks 11


Juniper Business
Juniper Use Only
Internal
PYTHON MAIN FUNCTION
MAIN FUNCTION

• PYTHON MAIN FUNCTION is a starting point of any


program.
• When the program is run, the python interpreter runs the
code sequentially.
• Main function is executed only when it is run as a Python
program.
• It will not run the main function if it imported as a
module.

EXAMPLE

• It is because we did not declare the call function "if


__name__== "__main__"

© 2019 Juniper Networks 12


Juniper Business
Juniper Use Only
Internal
PYTHON MAIN FUNCTION

PYTHON MAIN AND NAME VARIABLE TWO TYPES OF USAGE FOR MAIN

• When Python interpreter reads a source file, it will execute


all the code found in it.

• When Python runs the "source file" as the main program, it


sets the special variable (__name__) to have a value
("__main__").

• In Python "if __name__== "__main__" allows you to run


the Python files either as reusable modules or
standalone programs.

© 2019 Juniper Networks 13


Juniper Business
Juniper Use Only
Internal
MODULES

MODULE MODULE IMPORT TYPES - IMPORT THE FROM...IMPORT <NAME> STATEMENT

• A module allows you to logically • You can use any Python source file as a • Python's from statement lets you
organize your Python code. module by executing an import statement import specific attributes from a
in some other Python source file. module into the current namespace.
The import has the following syntax The from...import has the following
• Grouping related code into a module syntax −
makes the code easier to understand
and use. – import module1[, module2[,... module]
– from modname import name1[,
• When the interpreter encounters an name2[, ... nameN]]
• A module is a Python object with
import statement, it imports the module
arbitrarily named attributes that you
if the module is present in the search path • This statement does not import the
can bind and reference.
entire module fib into the current
• A module is loaded only once, regardless namespace; it just introduces the item
• Simply, a module is a file consisting of fibonacci from the module fib into the
of the number of times it is imported. This
Python code. A module can define global symbol table of the importing
prevents the module execution from
functions, classes and variables. A module.
happening over and over again if multiple
module can also include runnable
imports occur.
code.

© 2019 Juniper Networks 14


Juniper Business
Juniper Use Only
Internal
MODULES

THE FROM...IMPORT * STATEMENT LOCATING MODULES SYS MODULE

• It is also possible to import all names • When you import a module, the • The module search path is stored in
from a module into the current Python interpreter searches for the the system module sys as
namespace by using the following module in the following sequences the sys.path variable. The sys.path
import statement − variable contains the current directory,
PYTHONPATH, and the installation-
– The current directory.
dependent default.
– from modname import *
– If the module isn't found, Python
• This provides an easy way to import then searches each directory in the • The PYTHONPATH is an environment
all the items from a module into the shell variable PYTHONPATH. variable, consisting of a list of
current namespace; however, this directories. The syntax of
statement should be used sparingly. PYTHONPATH is the same as that of
– If all else fails, Python checks the
the shell variable PATH.
default path. On UNIX, this default
path is normally
/usr/local/lib/python/.

© 2019 Juniper Networks 15


Juniper Business
Juniper Use Only
Internal
IDENTIFIERS AND CONVENTIONS

PYTHON IDENTIFIERS CONVENTIONS

• A Python identifier is a name used to identify a • Class names start with an uppercase letter. All
variable, function, class, module or other object. An other identifiers start with a lowercase letter.
identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters,
underscores and digits (0 to 9) • Starting an identifier with a single leading
underscore indicates that the identifier is private.

• Python does not allow punctuation characters such


as @, $, and % within identifiers. Python is a case • Starting an identifier with two leading underscores
sensitive programming language. indicates a strongly private identifier.

• If the identifier also ends with two trailing


underscores, the identifier is a language-defined
special name.

© 2019 Juniper Networks 16


Juniper Business
Juniper Use Only
Internal
RESERVED WORDS AND QUOTATIONS

RESERVED WORDS MULTILINE STATEMENTS AND QUOTATIONS

• Statements in Python typically end with a new line. Python


does, however, allow the use of the line continuation
character (\) to denote that the line should continue.
– total = item_one + \
item_two + \
item_three

• Statements contained within the [], {}, or () brackets do not


need to use the line continuation character.
days = ['Monday', 'Tuesday', 'Wednesday’,
'Thursday', 'Friday’]

• Python accepts single ('), double (") and triple (''' or """)
quotes to denote string literals, as long as the same type of
quote starts and ends the string.

• The triple quotes are used to span the string across multiple
lines

© 2019 Juniper Networks 17


Juniper Business
Juniper Use Only
Internal
REGULAR EXPRESSIONS

RE MODULE

• A regular expression is a special sequence of characters that helps you match or find other strings or sets of
strings, using a specialized syntax held in a pattern

• Python re module

– re.match(pattern, string, flags=0)


– re.search(pattern, string, flags=0)

• match checks for a match only at the beginning of the string, while search checks for a match anywhere in
the string
• The re.match and re.search function return a match object on success, None on failure. We
usegroup(num) or groups() function of match object to get matched expression.

#!/usr/bin/python
import re
line = "Cats are smarter than dogs"
matchObj = re.match( r'(.*) are (.*?) .*', line, re.I)

© 2019 Juniper Networks 18


Juniper Business
Juniper Use Only
Internal
ERROR HANDLING

TYPES OF ERRORS ASSERTION EXCEPTION HANDLING

• Python provides two very important • assert Expression[, Arguments] • If you have some suspicious code that
features to handle any unexpected may raise an exception, you can
error in your Python programs and to defend your program by placing the
add debugging capabilities in them − suspicious code in a
When it encounters an assert statement,
Python evaluates the accompanying – try: block
– Assertions
expression, which is hopefully true. If the
– Exception handling – except: statement,
expression is false, Python raises
an AssertionError exception.
followed by a block of code which
handles the problem as elegantly as
possible.
• An exception is an event, which occurs
during the execution of a program that
disrupts the normal flow of the
program's instructions..

© 2019 Juniper Networks 19


Juniper Business
Juniper Use Only
Internal
OOPS

OOPS TYPES ANALOGY

• procedure-oriented : blocks of • Classes and objects are the two main • variables that store integers are
statements which manipulate data aspects of object oriented variables which are instances (objects)
programming. of the int class
• Another way of organizing your
program which is to combine data and • A class creates a • Variables that belong to an object or
functionality and wrap it inside new type where objects are instances class are referred to as fields. Objects
something called an object. of the class. can also have functionality by using
functions that belong to a class. Such
functions are called methods of the
• This is called the object class.
oriented programming paradigm.

• the fields and methods can be


referred to as the attributes of that
class.

© 2019 Juniper Networks 20


Juniper Business
Juniper Use Only
Internal
OOPS

SELF SAMPLE CLASS __INIT__ AND INHERITANCE

• Class methods have only one specific class Person: • The __init__ method is run as soon as
difference from ordinary functions an object of a class is instantiated
def __init__(self, name):
self.name = name • The method is useful to do
– they must have an extra first name any initialization (
that has to be added to the def say_hi(self):
print('Hello, my name is', self.name) • Notice the double underscores both
beginning of the parameter list, at the beginning and at the end of the
– but you do not give a value for this p = Person(‘Rogers’) name.
parameter when you call the p.say_hi()
• One of the major benefits of object
method, # The previous 2 lines can also be written as #
oriented programming is reuse of
Person('Swaroop').say_hi()
– Python will provide it. code and one of the ways this is
$ python oop_init.py
achieved is through
Hello, my name is Rogers
the inheritance mechanism.
Inheritance can be best imagined as
implementing a type and
subtype relationship between classes.

© 2019 Juniper Networks 21


Juniper Business
Juniper Use Only
Internal
Q&A

Thank You

© 2019 Juniper Networks


Juniper Business
Juniper Use Only
Internal

You might also like