DEVOPS Introduction to Python · Title: DEVOPS Introduction to Python Author: Ayelet Shabtay...

Preview:

Citation preview

DEVOPS

Introduction to Python

• 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

Introduction

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.

History

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.

Basic Syntax

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.

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.

•Types

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.

Strings:

Numbers:

List:

Dictionary/ Associative array :

Dictionary methods:

A tuple consists of a number of values separated by commas

Tuple:

Boolean:

•Operators

Arithmetic:

String Manipulation:

Logical Comparison:

Identity Comparison:

Arithmetic Comparison:

•Control Flow

Conditionals :

For Loop :

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)

For Loop over dictionary :

While Loop:

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.

break:

continue:

pass:

else (in loop):

List Comprehensions:

•Functions

Basic Function:

Function Arguments (positional and keyword arguments):

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.

Global vs. Local variables:

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

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.

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

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.

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.

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.

•Object Oriented Programming

• 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:

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.

Creating Classes:

Creating Instance Objects:

Setters and Getters:

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.

Built-In Class Attributes:

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.

Class Inheritance:

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.

Data Hiding (Encapsulation):

•Iterators

• 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.

The iteration protocol:

•Files I/O

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.

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.

File open :

• Open a file for reading and writing.

File methods :

•Directories and Files

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.

•Sockets

• 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.

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.

• 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.

A Simple Server:

A Simple Client:

•Python Flask

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.

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)

URL params:

Template render:

POST/GET and Session Handle:

Redirect Handle:

• Python Regular Expressions

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.

• 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):

• 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):

The match Function:

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

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

The search Function:

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

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

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.

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.

•Try – Except Error handling

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:

General Error Catching:

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

Questions and

Answers

• 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

•Thanks for listening

Recommended