Top Banner
Python Master Class Part 1 By Chathuranga Bandara www.chathuranga.com
71

Python master class part 1

Feb 09, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Python master class part 1

Python Master ClassPart 1

By Chathuranga Bandarawww.chathuranga.com

Page 2: Python master class part 1

Today’s Agenda

Introduction to Python

Introduction to Flask

Page 3: Python master class part 1

Python

Page 4: Python master class part 1

What is Python… really..

Multi Purpose

Object Oriented

Interpreted?

Dynamically Typed and Strong as well

Readability and We are Adults!

Page 5: Python master class part 1

Features

Cross Platform

Functional Support as well

Everything is an Object

Many Implementations

Batteries Included

Page 6: Python master class part 1

Who Uses Python?

Page 7: Python master class part 1

Why Python?

Page 8: Python master class part 1
Page 9: Python master class part 1
Page 10: Python master class part 1

Versions

v2.7.X

v3.X

Page 11: Python master class part 1

Some Tax…. i mean Syntax..

Page 12: Python master class part 1

Indentation is the Key!

Page 13: Python master class part 1
Page 14: Python master class part 1
Page 15: Python master class part 1

Types?

Page 16: Python master class part 1

Not Strongly TypedWhat the f*** that means?

Page 17: Python master class part 1

Let’s Discuss Python Memory Management.

Page 18: Python master class part 1

What is a C type Variable?

Page 19: Python master class part 1

Variable location value

a 0x3FS 101

b 0x3F9 101

unique location These values live in a fixed size bracket hence can only hold same sized data or an overflow happens

Page 20: Python master class part 1

When we change one value

Variable location value

a 0x3FS 110

b 0x3F9 101

Data in the memory location get overwritten

Page 21: Python master class part 1

Python has Names not Variables

Page 22: Python master class part 1

names

references

objects

Page 23: Python master class part 1

Name is just a label for an objectIn Python you can have multiple names for a single object

Page 24: Python master class part 1

What is a reference?Name or a container pointing to an object

Page 25: Python master class part 1

ie:

110

x = 110

x

+1 reference count

Page 26: Python master class part 1

110

x = 110y = 110

x

+2 reference count

y

Page 27: Python master class part 1

Not sure?

Page 28: Python master class part 1

also,

z = [110, 110]

110

x yReference count: 4

Page 29: Python master class part 1

Decrease Ref count?

110

x

reference count: 1

y

del will not delete the object but rather delete the reference to the object.

Page 30: Python master class part 1

What happens when the ref count is 0?Then it will delete the object. (what the garbage collector does in python)

Page 31: Python master class part 1

Different types of objects then?● Numbers● Strings

● dict● list● classes

Page 32: Python master class part 1

Because of names

Page 33: Python master class part 1
Page 34: Python master class part 1

Functions also can have names

Page 35: Python master class part 1

Strings

Page 36: Python master class part 1

Numbers

Page 37: Python master class part 1

None is the new null

Page 38: Python master class part 1

Lists

Page 39: Python master class part 1

List Comprehension

Page 40: Python master class part 1

Dictionaries

Page 41: Python master class part 1

Mutable vs Immutable Mutable : can alter

● list, dict, byte array, set

Immutable: otherwise

● Numbers, string, tuples, frozen sets

Page 42: Python master class part 1

ie:

Is extremely inefficient

Page 43: Python master class part 1

Much efficient

Page 44: Python master class part 1

Control Flows - Conditionals

Page 45: Python master class part 1

Loops

Page 46: Python master class part 1

Functions

Page 47: Python master class part 1
Page 48: Python master class part 1

CLASSES

Page 49: Python master class part 1
Page 50: Python master class part 1

What is PIP?pip is the Python package index

Page 51: Python master class part 1

$ sudo apt-get install python-pip

Page 52: Python master class part 1

Virtual Environment?

Page 53: Python master class part 1

$ pip install virtualenv$ cd my_project_folder$ virtualenv name_of_the_virt$ source name_of_the_virt/bin/activate

$ do pip sh*t

$ deactivate

Page 54: Python master class part 1

Good practice!Have a folder in root called requirements and inside that have following:

● dev.txt● Prod.txt

Page 55: Python master class part 1

Ie: dev.txt

Page 56: Python master class part 1

$ pip install -r requirements/dev.txt

Page 57: Python master class part 1

pip install Flask

# Import Flask libraryfrom flask import Flask # Initialize the app from Flaskapp = Flask(__name__) # Define a route to hello_world [email protected]('/hello')def hello_world(): return 'Hello World Again!' # Run the app on http://localhost:8085app.run(debug=True,port=8085)

python hello.py

Page 58: Python master class part 1
Page 59: Python master class part 1
Page 60: Python master class part 1

Routing

@app.route('/')

def index():

return 'Index

Page'

@app.route('/hello')

def hello():

return 'Hello, World'

Page 61: Python master class part 1

Variables?

@app.route('/user/<username>')

def show_user_profile(username):

# show the user profile for that user

return 'User %s' % username

@app.route('/post/<int:post_id>')

def show_post(post_id):

# show the post with the given id, the id is an integer

return 'Post %d' % post_id

Page 62: Python master class part 1

That is sweet, what about the REST?

Page 63: Python master class part 1

from flask import request

@app.route('/login', methods=['GET', 'POST'])

def login():

if request.method == 'POST':

do_the_login()

else:

show_the_login_form()

Page 64: Python master class part 1

Rendering Templates

from flask import render_template

@app.route('/hello/')

@app.route('/hello/<name>')

def hello(name=None):

return render_template('hello.html', name=name)

Page 65: Python master class part 1

Where the f**k is the template then?

Page 66: Python master class part 1

<!doctype html>

<title>Hello from Flask</title>

{% if name %}

<h1>Hello {{ name }}!</h1>

{% else %}

<h1>Hello, World!</h1>

{% endif %}

Page 67: Python master class part 1

Request Object

from flask import request

@app.route('/login', methods=['POST', 'GET'])

def login():

error = None

if request.method == 'POST':

if valid_login(request.form['username'],

request.form['password']):

return log_the_user_in(request.form['username'])

else:

error = 'Invalid username/password'

# the code below is executed if the request method

# was GET or the credentials were invalid

return render_template('login.html', error=error)

Page 68: Python master class part 1

sessions

Page 69: Python master class part 1

from flask import Flask, session, redirect, url_for, escape, request

app = Flask(__name__)

@app.route('/')

def index():

if 'username' in session:

return 'Logged in as %s' % escape(session['username'])

return 'You are not logged in'

@app.route('/login', methods=['GET', 'POST'])

def login():

if request.method == 'POST':

session['username'] = request.form['username']

return redirect(url_for('index'))

return ''' <form action="" method="post">

<p><input type=text name=username>

<p><input type=submit value=Login>

</form>

'''

Page 70: Python master class part 1

@app.route('/logout')

def logout():

# remove the username from the session if it's there

session.pop('username', None)

return redirect(url_for('index'))

# set the secret key. keep this really secret:

app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

Page 71: Python master class part 1

That’s all for today Folks!