58
Python Master Class Part 2 By Chathuranga Bandara www.chathuranga.com

Python master class 2

Embed Size (px)

Citation preview

Page 1: Python master class 2

Python Master ClassPart 2

By Chathuranga Bandarawww.chathuranga.com

Page 2: Python master class 2

AgendaSome advanced concepts in Python

Exceptions Handling in Python

ORM - SQLAlchemy

Unit testing in Python

Page 3: Python master class 2

GeneratorsGenerators are a simple and powerful tool for creating iterators.

Page 4: Python master class 2

ie:

Page 5: Python master class 2

What is wrong there?The code is quite simple and straightforward, but its builds the full list in memory. This is clearly not acceptable in our case,

because we cannot afford to keep all n "10 megabyte" integers in memory.

Page 6: Python master class 2

Generator pattern may be?

Page 7: Python master class 2

Still Have issues.● Lots of code / boilerplate

● Hard to read

Page 8: Python master class 2

Generators to the Rescue!

Page 9: Python master class 2

Some Dark Magic there! WTF is yield?

Page 10: Python master class 2

Yield is a keyword that is used like return, except the function will return a generator.

Page 11: Python master class 2

When a yield statement is executed, the state of the generator is frozen and the value of expression_list is returned to next()‘s caller. By “frozen” we mean that all

local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time next() is invoked, the function can proceed exactly as if the

yield statement were just another external call.

Page 12: Python master class 2

Iterators It's a stateful helper object that will produce the next value when you call next() on

it. Any object that has a __next__() method is therefore an iterator. How it produces a value is irrelevant.

Page 13: Python master class 2

ie: inbuilt iterators

Page 14: Python master class 2
Page 15: Python master class 2

Custom Iterators

Page 16: Python master class 2

Decorators Some sh*t which is very useful

Page 17: Python master class 2

What is a decorator?decorators dynamically alter the functionality of a function, method or class without

having to directly use subclasses.

Page 18: Python master class 2

Some ContextWe can assign names to functions right?

Page 19: Python master class 2

Also Closures. (yes. JS stole it from here)

Page 20: Python master class 2

Also Higher Order Functions

Page 21: Python master class 2

Return a function

Page 22: Python master class 2
Page 23: Python master class 2

Finally Decorators! (stitch all the sh*t together)

Page 24: Python master class 2

Python Decorator Syntax

Page 25: Python master class 2
Page 26: Python master class 2
Page 27: Python master class 2

Better way?

Page 28: Python master class 2

Passing Arguments to decorators

Page 29: Python master class 2

More Pythonic

Page 30: Python master class 2

Exceptions Handling in PythonTry and Catch that nigga!

Page 31: Python master class 2

Syntax

Page 32: Python master class 2
Page 33: Python master class 2

How to raise an Exception

Page 34: Python master class 2

Custom Exceptions

Page 35: Python master class 2

ORM for Flask

Page 36: Python master class 2

pip install Flask-SQLAlchemy

Page 37: Python master class 2
Page 38: Python master class 2
Page 39: Python master class 2
Page 40: Python master class 2
Page 41: Python master class 2

Relationships?

Page 42: Python master class 2
Page 43: Python master class 2
Page 44: Python master class 2

Do Something!

Page 45: Python master class 2

Small Applications/yourapplication /yourapplication.py /static /style.css /templates layout.html index.html login.html ...

Page 46: Python master class 2

Packages/yourapplication /runserver.py /yourapplication /__init__.py /views.py /static /style.css /templates layout.html index.html login.html ...

Page 47: Python master class 2

With Blueprintsyourapp/

__init__.py

static/

templates/

home/

control_panel/

admin/

views/

__init__.py

home.py

control_panel.py

admin.py

models.py

Page 48: Python master class 2
Page 49: Python master class 2
Page 50: Python master class 2

Unit Testing

Page 51: Python master class 2

Inbuilt?

Page 52: Python master class 2
Page 53: Python master class 2

Nose helps you manage unit tests

Page 54: Python master class 2
Page 55: Python master class 2

pip install unittest2

Page 56: Python master class 2
Page 57: Python master class 2

Questions?

Page 58: Python master class 2

That’s all for today Folks!