SlideShare a Scribd company logo
HANDLE ERROR,
GENERATOR AND
DECORATOR
John
Saturday, December 21, 2013
HANDLE ANY
UNEXPECTED
ERROR
Brief introduction
• Python provide 2 ways to handle unexpected
error: exception and assert.
• Exception handling: is an event, which occurs
during the execution of a program, that disrupts
the normal flow of the program's instructions.
• The exceptions are defined in the built-in class
exceptions
• For example: If divided by 0, we want to raise an
exception
Built-in exceptions
Warnings
• It is defined on the warnings module
Raising Exceptions
• The raise statement allows the programmer
to force a specified exception to occur
raise NameError('HiThere')
• Raise statement is to raise an exceptions, tryexception-finally clause is to catch an
exceptions and decide how to do.
Try …except…finally structure

•
•
•
•

First, the try clause(print 100/0) is executed
If no exception occurs, the except clause is skipped
Otherwise, the rest of the try clause is skipped. Go to the line its type
matches the exception name(ZeroDivisionError).
Clean-up info in the finally sentence. It executed under all conditions
Write User-defined Exceptions
>>> class MyError(Exception):
... def __init__(self, value):
...
self.value = value
... def __str__(self):
...
return repr(self.value)

• Define user-defined exception MyError.
• Raise an exception when x == 0. Also write the try-except-finally
clause
• When call f(0,100), the exception is raised and catched.
Brief introduction of assert
• The assert clause is used on situation or
condition that should never happen. For
example: assert 1>0
• “assert” statement is removed when the
compilation is optimized (-O and -OO option,
it is because __debug__ change to False
when -O or -OO option are added).
• So It is a convenient way to insert debugging
assertion into a program
Quick example

• We can see assert is ignored when add -O
option
GENERATOR
Brief introduction
• Generator s are a simple and powerful tool for
create iterators.
• Use yield statement instead of return to return
data
• the __iter__() and next() methods are created
automatically. The local and execution state are
saved automatically.
• When generator terminate, it raise StopIteration
Quick example
• When you call the generator function, the co
de does not run. It just return the generator
object.
The difference between generator
and sequence type
>>> mylist = [x*x for x in range(3)]
>>> mygenerator = (x*x for x in range(3))
•Both mylist and mygenerator are iterable
•But you can only read
generator once.
•Generator do not store all
the values in memory, they
generate the values on the
fly.
DECORATOR
Brief introduction
• Functions are objects in python.
• We can define other function inside function
definition.
• We can pass a function as argument of other
function.
Quick example
•

benchmark function accept
func as input argument.
• We can see @benchmark
equal to
apply benchmark function
on f
f = benchmark(f)
•

This is the typical usage of
decorator: Use func as input
argument. define wrapper
function inside function
definition
Official document
• PEP - 318 Decorators for Functions and
Methods
DESCRIPTOR
Brief introduction
• A descriptor is an object attribute with
“binding behavior”.
• If any of __get__(), __set__() and
__delete__() are defined for an object, it is
said to be a descriptor.
Descriptor protocol
• If an object defines both __get__() and __set__(), it is considered
a data descriptor.
• Descriptors that only define __get__() are called non-data
descriptors

• descriptors are invoked by the __getattribute__() method
• overriding __getattribute__() prevents automatic descriptor
calls
Descriptor example
• Define __set__ and __get__ method.
Implement the property() method
• Calling property() is a succinct way of building
a data descriptor
Let us write the similar property()
descriptor
Function are non-data descriptor
• All functions include __get__() method for
binding methods.
Non-data descriptor staticmethod
• The pure python verson of static method
should be like:

Use static method
Non-data descriptor classmethod
• Pure python version of classmethod looks
like:

More Related Content

What's hot

Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smile
Martin Melin
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
Python libraries
Python librariesPython libraries
Python libraries
Prof. Dr. K. Adisesha
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
 
PYTHON PROGRAMMING
PYTHON PROGRAMMINGPYTHON PROGRAMMING
PYTHON PROGRAMMING
indupps
 
Function overloading
Function overloadingFunction overloading
Function overloading
Sudeshna Biswas
 
Review functions
Review functionsReview functions
Review functions
Kgr Sushmitha
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
C++ tokens and expressions
C++ tokens and expressionsC++ tokens and expressions
C++ tokens and expressions
NabeelaNousheen
 
Header files in c
Header files in cHeader files in c
Header files in c
HoneyChintal
 
Pointers
PointersPointers
Pointers
Joy Forerver
 
Modern C++
Modern C++Modern C++
Modern C++
Richard Thomson
 
Python advance
Python advancePython advance
Python advance
Deepak Chandella
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
Aditya Shankar
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Function
FunctionFunction
Function
yash patel
 

What's hot (20)

Python recursion
Python recursionPython recursion
Python recursion
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smile
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Python libraries
Python librariesPython libraries
Python libraries
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
PYTHON PROGRAMMING
PYTHON PROGRAMMINGPYTHON PROGRAMMING
PYTHON PROGRAMMING
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Review functions
Review functionsReview functions
Review functions
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Python functions
Python functionsPython functions
Python functions
 
C++ tokens and expressions
C++ tokens and expressionsC++ tokens and expressions
C++ tokens and expressions
 
Header files in c
Header files in cHeader files in c
Header files in c
 
Pointers
PointersPointers
Pointers
 
Modern C++
Modern C++Modern C++
Modern C++
 
Python advance
Python advancePython advance
Python advance
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Function
FunctionFunction
Function
 

Viewers also liked

Python session 12
Python session 12Python session 12
Python session 12
Navaneethan Naveen
 
Advanced Python : Decorators
Advanced Python : DecoratorsAdvanced Python : Decorators
Advanced Python : Decorators
Bhanwar Singh Meena
 
Python decorators
Python decoratorsPython decorators
Python decorators
Alex Su
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Decorators in Python
Decorators in PythonDecorators in Python
Decorators in Python
Ben James
 

Viewers also liked (6)

Python session 12
Python session 12Python session 12
Python session 12
 
Advanced Python : Decorators
Advanced Python : DecoratorsAdvanced Python : Decorators
Advanced Python : Decorators
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Decorators in Python
Decorators in PythonDecorators in Python
Decorators in Python
 

Similar to Python advanced 1.handle error, generator, decorator and decriptor

Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
Srinivas Narasegouda
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
Ajit Mali
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
VGaneshKarthikeyan
 
Data Structures and Algorithms in Python
Data Structures and Algorithms in PythonData Structures and Algorithms in Python
Data Structures and Algorithms in Python
JakeLWright
 
Unit iii
Unit iiiUnit iii
Unit iii
snehaarao19
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
sarthakgithub
 
Unit iii pds
Unit iii pdsUnit iii pds
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
Mukund Trivedi
 
Lecture 09 Exception Handling(1 ) in c++.pptx
Lecture 09 Exception Handling(1 ) in c++.pptxLecture 09 Exception Handling(1 ) in c++.pptx
Lecture 09 Exception Handling(1 ) in c++.pptx
ZenLooper
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
python ppt.pptx
python ppt.pptxpython ppt.pptx
python ppt.pptx
MONAR11
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
mustafatahertotanawa1
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
SURBHI SAROHA
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
Yagna15
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
VishuSaini22
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
Mukund Trivedi
 
Handling
HandlingHandling
Handling
Amit Vats
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
skilljiolms
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Exceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptxExceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
 

Similar to Python advanced 1.handle error, generator, decorator and decriptor (20)

Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
Data Structures and Algorithms in Python
Data Structures and Algorithms in PythonData Structures and Algorithms in Python
Data Structures and Algorithms in Python
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
 
Unit iii pds
Unit iii pdsUnit iii pds
Unit iii pds
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Lecture 09 Exception Handling(1 ) in c++.pptx
Lecture 09 Exception Handling(1 ) in c++.pptxLecture 09 Exception Handling(1 ) in c++.pptx
Lecture 09 Exception Handling(1 ) in c++.pptx
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
python ppt.pptx
python ppt.pptxpython ppt.pptx
python ppt.pptx
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Handling
HandlingHandling
Handling
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
Exceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptxExceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptx
 

More from John(Qiang) Zhang

Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
John(Qiang) Zhang
 
Python testing
Python  testingPython  testing
Python testing
John(Qiang) Zhang
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
John(Qiang) Zhang
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
John(Qiang) Zhang
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
John(Qiang) Zhang
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modulesPython advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
John(Qiang) Zhang
 

More from John(Qiang) Zhang (6)

Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
 
Python testing
Python  testingPython  testing
Python testing
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)A useful tools in windows py2exe(optional)
A useful tools in windows py2exe(optional)
 
Python advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modulesPython advanced 3.the python std lib by example – system related modules
Python advanced 3.the python std lib by example – system related modules
 

Recently uploaded

Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 

Recently uploaded (20)

Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 

Python advanced 1.handle error, generator, decorator and decriptor

  • 3. Brief introduction • Python provide 2 ways to handle unexpected error: exception and assert. • Exception handling: is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. • The exceptions are defined in the built-in class exceptions • For example: If divided by 0, we want to raise an exception
  • 5. Warnings • It is defined on the warnings module
  • 6. Raising Exceptions • The raise statement allows the programmer to force a specified exception to occur raise NameError('HiThere') • Raise statement is to raise an exceptions, tryexception-finally clause is to catch an exceptions and decide how to do.
  • 7. Try …except…finally structure • • • • First, the try clause(print 100/0) is executed If no exception occurs, the except clause is skipped Otherwise, the rest of the try clause is skipped. Go to the line its type matches the exception name(ZeroDivisionError). Clean-up info in the finally sentence. It executed under all conditions
  • 8. Write User-defined Exceptions >>> class MyError(Exception): ... def __init__(self, value): ... self.value = value ... def __str__(self): ... return repr(self.value) • Define user-defined exception MyError. • Raise an exception when x == 0. Also write the try-except-finally clause • When call f(0,100), the exception is raised and catched.
  • 9. Brief introduction of assert • The assert clause is used on situation or condition that should never happen. For example: assert 1>0 • “assert” statement is removed when the compilation is optimized (-O and -OO option, it is because __debug__ change to False when -O or -OO option are added). • So It is a convenient way to insert debugging assertion into a program
  • 10. Quick example • We can see assert is ignored when add -O option
  • 12. Brief introduction • Generator s are a simple and powerful tool for create iterators. • Use yield statement instead of return to return data • the __iter__() and next() methods are created automatically. The local and execution state are saved automatically. • When generator terminate, it raise StopIteration
  • 13. Quick example • When you call the generator function, the co de does not run. It just return the generator object.
  • 14. The difference between generator and sequence type >>> mylist = [x*x for x in range(3)] >>> mygenerator = (x*x for x in range(3)) •Both mylist and mygenerator are iterable •But you can only read generator once. •Generator do not store all the values in memory, they generate the values on the fly.
  • 16. Brief introduction • Functions are objects in python. • We can define other function inside function definition. • We can pass a function as argument of other function.
  • 17. Quick example • benchmark function accept func as input argument. • We can see @benchmark equal to apply benchmark function on f f = benchmark(f) • This is the typical usage of decorator: Use func as input argument. define wrapper function inside function definition
  • 18. Official document • PEP - 318 Decorators for Functions and Methods
  • 20. Brief introduction • A descriptor is an object attribute with “binding behavior”. • If any of __get__(), __set__() and __delete__() are defined for an object, it is said to be a descriptor.
  • 21. Descriptor protocol • If an object defines both __get__() and __set__(), it is considered a data descriptor. • Descriptors that only define __get__() are called non-data descriptors • descriptors are invoked by the __getattribute__() method • overriding __getattribute__() prevents automatic descriptor calls
  • 22. Descriptor example • Define __set__ and __get__ method.
  • 23. Implement the property() method • Calling property() is a succinct way of building a data descriptor
  • 24. Let us write the similar property() descriptor
  • 25. Function are non-data descriptor • All functions include __get__() method for binding methods.
  • 26. Non-data descriptor staticmethod • The pure python verson of static method should be like: Use static method
  • 27. Non-data descriptor classmethod • Pure python version of classmethod looks like: