97
DEVOPS Introduction to Python

DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

DEVOPS

Introduction to Python

Page 2: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• Introduction

• History

• Basic Syntax

• Types

• Operators

• Control Flow

• Functions

• Object Oriented Programming

• Iterators

• Files I/O

• Directories and Files

• Sockets

• Python Flask

• Try – Except / Error handling

Page 3: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Introduction

Page 4: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991.

An interpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly braces or keywords), and a syntax which allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.

The language provides constructs intended to enable writing clear programs on both a small and large scale.Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, functional programming, and procedural styles. It has a large and comprehensive standard library.

Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems.

Page 5: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

History

Page 6: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Python was conceived in the late 1980s,and its implementation began in December 1989 by Guido van Rossum at Centrum

Wiskunde & Informatica (CWI) in the Netherlands as a successor to the ABC language (itself inspired by SETL)capable of exception handling and interfacing with the operating system Amoeba.

Python 2.0 was released on 16 October 2000 and had many major new features, including a cycle-detecting garbage collector and support for Unicode. With this release the development process was changed and became more transparent and community-backed.

Python 3.0 (which early in its development was commonly referred to as Python 3000 or py3k), a major, backwards-incompatible release, was released on 3 December 2008 after a long period of testing. Many of its major features have been backported to the backwards-compatible Python 2.6.x and 2.7.x version series.

Page 7: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Basic Syntax

Page 8: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Python uses whitespace indentation to delimit blocks – rather than curly braces or keywords.

An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.

Page 9: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Commenting in Python is also quite different than other languages, but it is pretty easy to get used to. In Python there are basically two ways to comment: • Single line commenting is good for a short, quick comment (or for debugging)• block comment is often used to describe something much more in detail or to

block out an entire chunk of code.

Page 10: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Types

Page 11: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM
Page 12: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

a immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.

Page 13: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Strings:

Page 14: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Numbers:

Page 15: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

List:

Page 16: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Dictionary/ Associative array :

Page 17: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Dictionary methods:

Page 18: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

A tuple consists of a number of values separated by commas

Tuple:

Page 19: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Boolean:

Page 20: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Operators

Page 21: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Arithmetic:

Page 22: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

String Manipulation:

Page 23: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Logical Comparison:

Page 24: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Identity Comparison:

Page 25: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Arithmetic Comparison:

Page 26: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Control Flow

Page 27: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Conditionals :

Page 28: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

For Loop :

Page 29: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Difference between xrange and range:

• range(): range(1, 10) returns a list from 1 to 10 numbers & hold whole list in memory.

• xrange(): Like range(), but instead of returning a list, returns an object that generates the numbers in the range on demand. For looping, this is lightly faster than range() and more memory efficient. xrange() object like an iterator and generates the numbers on demand.(Lazy Evaluation)

Page 30: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

For Loop over dictionary :

Page 31: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

While Loop:

Page 32: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

break, continue and pass:

• The break statement in Python terminates the current loop and resumes execution at the next statement.

• The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

• The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes.

• The else statement associated with a loop statements.

• If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.

• If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

Page 33: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

break:

Page 34: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

continue:

Page 35: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

pass:

Page 36: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

else (in loop):

Page 37: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

List Comprehensions:

Page 38: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Functions

Page 39: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Basic Function:

Page 40: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Function Arguments (positional and keyword arguments):

Page 41: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Global vs. Local variables:

• Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

• This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions.

• When you call a function, the variables declared inside it are brought into scope.

Page 42: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Global vs. Local variables:

Page 43: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Pass by reference vs value:

• All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function

Page 44: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Arbitrary Arguments (*args and **kwargs):

• Specify that a function can be called with an arbitrary number of arguments.

• These arguments will be wrapped up in a tuple (see Tuples and Sequences).

• Before the variable number of arguments, zero or more normal arguments may occur.

• Normally, these variadic arguments will be last in the list of formal parameters,

• because they scoop up all remaining input arguments that are passed to the function.

• Any formal parameters which occur after the *args parameter are ‘keyword-only’ arguments,

• meaning that they can only be used as keywords rather than positional arguments.

• In the same fashion, dictionaries can deliver keyword arguments with the **-operator.

Page 45: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Arbitrary Arguments (*args and **kwargs):

• You would use *args when you're not sure how many arguments might be passed to your function. Similarly, **kwargs allows you to handle named arguments that you have not defined in advance

Page 46: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

The Anonymous Functions:

• These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.

• Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.

• An anonymous function cannot be a direct call to print because lambda requires an expression

• Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.

Page 47: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Functional programming:

• In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

• A number of concepts and paradigms are specific to functional programming :

• First-class and higher-order functions: functions that can either take other functions as arguments or return them as results.

• Pure functions: Pure functions (or expressions) have no side effects (memory or I/O)

• Recursion: Recursive functions invoke themselves, allowing an operation to be performed over and over until the base case is reached.

Page 48: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Map, Filter and Reduce:

• Map applies a function to all the items in an input list.filter creates a list of elements for which a function returns true.

• Reduce is a useful function for performing some computation on a list and returning the result.

Page 49: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Object Oriented Programming

Page 50: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.

• Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.

• Class variable: A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.

• Data member: A class variable or instance variable that holds data associated with a class and its objects.

• Function overloading: The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.

• Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class.

• Inheritance: The transfer of the characteristics of a class to other classes that are derived from it.

Overview of OOP Terminology:

Page 51: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Overview of OOP Terminology:

• Instance: An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.

• Instantiation: The creation of an instance of a class.

• Method : A special kind of function that is defined in a class definition.

• Object: A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.

• Operator overloading: The assignment of more than one function to a particular operator.

Page 52: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Creating Classes:

Page 53: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Creating Instance Objects:

Page 54: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Setters and Getters:

Page 55: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Built-In Class Attributes:

• Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute

• __dict__: Dictionary containing the class's namespace.

• __doc__: Class documentation string or none, if undefined.

• __name__: Class name.

• __module__: Module name in which the class is defined.

• This attribute is "__main__" in interactive mode.

• __bases__: A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

Page 56: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Built-In Class Attributes:

Page 57: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Class Inheritance:

• Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name.

• The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class.

• A child class can also override data members and methods from the parent.

Page 58: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Class Inheritance:

Page 59: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Overriding Methods:

• You can always override your parent class methods.

• One reason for overriding parent's methods is because you may want special or different functionality in your subclass.

Page 60: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Data Hiding (Encapsulation):

Page 61: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Iterators

Page 62: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• If you have written some code in Python, you have probably used iterable objects.

• Iterable objects are objects that conforms to the "Iteration Protocol" and can hence be used in a loop.

The range(50) is an iterable object that provide, at each iteration, a different value that is assigned to the "i" variable.

Page 63: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

The iteration protocol:

Page 64: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Files I/O

Page 65: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Printing to the Screen:

• The simplest way to produce output is using the print statement where you can pass zero or more expressions separated by commas.

Page 66: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard : raw_input , input.

Page 67: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

File open :

• Open a file for reading and writing.

Page 68: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

File methods :

Page 69: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Directories and Files

Page 70: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

OS module :

• This module provides a portable way of using operating system dependent functionality.

• To use this module you need to import it first and then you can call any related functions.

Page 71: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Sockets

Page 72: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• Python networking :

Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.

Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

Page 73: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Python sockets :

• Sockets are the endpoints of a bidirectional communications channel.

• Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.

• Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on.

• The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest.

Page 74: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• Method

• Description

• s.bind()

• This method binds address (hostname, port number pair) to socket.

• s.listen()

• This method sets up and start TCP listener.

• s.accept()

• This passively accept TCP client connection, waiting until connection arrives (blocking).

• s.connect()

• This method actively initiates TCP server connection.

• s.recv()

• This method receives TCP message

• s.send()

• This method transmits TCP message

• s.recvfrom()

• This method receives UDP message

• s.sendto()

• This method transmits UDP message

• s.close()

• This method closes socket

• socket.gethostname()

• Returns the hostname.

Socket Module:To create a socket, you must use the socket.socket() function available in socket module.

Page 75: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

A Simple Server:

Page 76: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

A Simple Client:

Page 77: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Python Flask

Page 78: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Introduction to Python Flask:

• Flask is a web framework.

• This means flask provides you with tools, libraries and technologies that allow you to build a web application.

• This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

• Flask is part of the categories of the micro-framework.

• Micro-framework are normally framework with little to no dependencies to external libraries.

• This has pros and cons.

• Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins.

Page 79: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Setting Up Flask:

• Setting up Flask is pretty simple and quick.

• With pip package manager, all we need to do is: “pip install flask”

• Once you're done with installing Flask, Import the flask module and create an app using Flask.

app.run(host (Defaults to 127.0.0.1), port(Defaults to 5000), debug, options)

Page 80: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

URL params:

Page 81: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Template render:

Page 82: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

POST/GET and Session Handle:

Page 83: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Redirect Handle:

Page 84: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• Python Regular Expressions

Page 85: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

What is Regular Expressions (Regex):• 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.

• The module re provides full support for Perl-like regular expressions in Python.

Method Description

^ Matches beginning of line.

$ Matches end of line.

. Matches any single character except newline. Using m option allows it to match newline as well.

[...] Matches any single character in brackets.

[^...] Matches any single character not in brackets

* Matches 0 or more occurrences of preceding expression.

+ Matches 1 or more occurrence of preceding expression.

? Matches 0 or 1 occurrence of preceding expression.

{ n} Matches exactly n number of occurrences of preceding expression.

Page 86: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• Method

• Description

• { n,}

• Matches n or more occurrences of preceding expression.

• { n, m}

• Matches at least n and at most m occurrences of preceding expression.

• a| b

• Matches either a or b.

• (…)

• Groups regular expressions and remembers matched text.

• \w

• Matches word characters.

• \W

• Matches nonword characters.

• \s

• Matches whitespace. Equivalent to [\t\n\r\f].

• \S

• Matches nonwhitespace.

• \d

• Matches digits. Equivalent to [0-9].

• \D

• Matches nondigits.

• \A

• Matches beginning of string.

• \Z

• Matches end of string. If a newline exists, it matches just before newline.

What is Regular Expressions (Regex):

Page 87: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• Method

• Description

• \z

• Matches end of string.

• \G

• Matches point where last match finished.

• \b

• Matches word boundaries when outside brackets. Matches backspace when inside brackets.

• \B

• Matches nonword boundaries.

• \n, \t, etc.

• Matches newlines, carriage returns, tabs, etc.

• \1...\9

• Matches nth grouped subexpression.

• \10

• Matches nth grouped subexpression if it matched already.

What is Regular Expressions (Regex):

Page 88: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

The match Function:

• This function attempts to match RE pattern to string with optional flags.

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

Page 89: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

The search Function:

• This function searches for first occurrence of RE pattern within string with optional flags.

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

Page 90: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Matching Versus Searching:

• Python offers two different primitive operations based on regular expressions:

• match checks for a match only at the beginning of the string,

• while search checks for a match anywhere in the string.

Page 91: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Search and Replace:

• This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.

Page 92: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Try – Except Error handling

Page 93: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Handling Exceptions:

• The simplest way to handle exceptions is with a "try-except" block:

If you wanted to examine the exception from code, you could have:

Page 94: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

General Error Catching:

• In case you want to catch all errors that could possibly be generated.

Page 95: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

Questions and

Answers

Page 96: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

• www.tutorialspoint.com/python

• www.slideshare.net/nowells/introduction-to-python-5182313

• wiki.python.org

• en.wikipedia.org/wiki/Python_(programming_language)

• flask.pocoo.org

• pymbook.readthedocs.io/en/latest/flask.html

Page 97: DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay Created Date: 5/25/2018 12:35:18 PM

•Thanks for listening