SlideShare a Scribd company logo
1 of 24
Download to read offline
Topics in Advanced Python 
No agenda – jumping in... 
© 2014 Zaar Hai tech.zarmory.com
Decorators 
Decorator is a function that accepts a function and returns 
another function 
from functools import wraps 
def deco(f): 
@wraps(f) 
def wrapper(*args, **kwargs): 
print "started at %s" % time.time() 
rv = f(*args, **kwargs) 
print "finished at %s" % time.time() 
return wrapper 
@deco 
def sleeper(): 
"Sleeper function" 
time.sleep(2) 
>>> sleeper() 
started at 1384245972.01 
finished at 1384245974.01 
>>> print sleeper.__name__ 
sleeper 
wraps takes care about wrapped aux data. Without it: 
>>> print sleeper.__name__ 
wrapper 
Important when reading online help of someone else's code 
2 © 2014 Zaar Hai tech.zarmory.com
Decorator usage examples 
To return cached values 
To implement retries 
To guard methods/functions with a lock 
To validate input/output of the function 
To limit function execution by a certain timeout 
Decorators greatly help with code reuse and 
Make code explicitly readable 
3 © 2014 Zaar Hai tech.zarmory.com
Decorator is just a function 
You can call decorators “inline” in your code 
@deco 
def sleeper(): pass 
sleeper() 
deco(sleeper)() 
@deco1 
@deco2 
def sleeper(): pass 
sleeper() 
deco1(deco2((sleeper))() 
Similarly, decorators themselves can accept parameters 
@deco(5) 
def sleeper(): pass 
sleeper() 
deco(5)(sleeper)() 
Note: The invocation order is a bit different 
Note:@deco(5) is executed when the code is imported 
4 © 2014 Zaar Hai tech.zarmory.com
Decorator with parameter - example 
$ cat mydeco.py 
def deco(p): 
print "got %s" % p 
def real_deco(f): 
@wraps(f) 
def wrapper(*args, **kwargs): 
print "decorating" 
return f(*args, **kwargs) 
return wrapper 
return real_deco 
@deco(5) 
def hello(): pass 
>>> import mydeco # outer function runs during import 
got 5 
>>> mydeco.hello() # decorator runs when function executed 
decorating 
>>> 
5 © 2014 Zaar Hai tech.zarmory.com
Decorator can be a class 
Some times its useful to implement decorators as a class 
class deco(object): 
def __init__(self, p): 
self.p = p 
print "got %s" % p 
def __call__(self, f): 
@wraps(f) 
def wrapper(*args, **kwargs): 
print "decorating" 
return f(*args, **kwargs) 
return wrapper 
__call__ is a special method that is invoked when you try 
calling an object as if it was a function 
6 © 2014 Zaar Hai tech.zarmory.com
How does @property decorator work? 
@property – one of the most common decorators in Python 
class A(object): 
@property 
def a(self): 
return "a" 
>>> A().a 
'a' 
But how does it work? 
7 © 2014 Zaar Hai tech.zarmory.com
Meet descriptors 
Descriptor is a protocol for accessing object attributes 
class A(object): 
def __init__(self): 
self.a = 1 
>>> f = A(); f.a 
When you access a, what actually happens is 
v = self.__dict__["a"] 
if hasattr(v, '__get__'): 
return v.__get__(self) 
else: 
return v 
I.e. when attribute defines __get__ (or __set__) methods, 
they are called to produce (or set) actual attribute value 
8 © 2014 Zaar Hai tech.zarmory.com
Back to the @property 
@property implements both decorator and descriptor 
semantics like this: 
class Property(object): 
def __init__(self, getter): 
self.getter = getter 
def __get__(self, obj, csl): 
return self.getter(obj) 
class A(object): 
@Property 
def a(self): 
return "a" 
>>> o = A(); o.a 
'a' 
>>> type(o.a) 
<type 'str'> 
9 © 2014 Zaar Hai tech.zarmory.com
Descriptors in action 
One-time caching of method's return values 
class OneTime(object): 
def __init__(self, func): 
self.func = func 
def __get__(self, obj, cls): 
to_augment = obj or cls 
rv = self.func(to_augment) 
pname = self.func.__name__ 
setattr(to_augment, pname, rv) 
return rv 
class BigMath(object): 
@OneTime 
def big_data(self): 
print "working hard...." 
return 42 
Reusable – follows DRY principle 
More on this here and here 
class BigMath(object): 
def get_big_data(self): 
if hasattr(self, "_rv"): 
return self._rv 
self._rv = self._get_data() 
return self._rv 
def _get_data(self): 
print "working hard...." 
return 42 
Nice, but leads to a lot 
of copy/paste code 
10 © 2014 Zaar Hai tech.zarmory.com
© 2014 Zaar Hai tech.zarmory.com 
Multiple inheritance 
“LEGO” goes on
M-I in action - MixIns 
“MixIn” is a programming concept about creating aux class 
that enriches functionality of the main class 
class SteeringWheel(object): ... 
class HornMixIn(object): 
"""Mix-in class to enable horn functionality""" 
def horn(self, *args, **kwargs): 
print "move over!" 
class CruiseControlMixIn(object): 
def cc_set(self): … 
def cc_cancel(self): ... 
def cc_restore(self):... 
class Mazda2SteeringWheel(CruiseControlMixIn, HornMixIn, SteeringWheel): 
"""Sometimes there is nothing to configure here at all""" 
12 © 2014 Zaar Hai tech.zarmory.com
M-I in action – MixIns (continued) 
Here is another example 
class Synchronized(object): 
def __init__(self, *args, **kwargs): 
self._sync_lock = threading.Lock() 
super(Synchronized, self).__init__(*args, **kwargs) 
@staticmethod 
def synchronized(func): 
@wraps(func) 
def wrapper(self, *args, **kwargs): 
with self._sync_lock: 
return func(self, *args, **kwargs) 
return wrapper 
class Writer(Syncrhonized, Db): 
@Synchronized.synchronized 
def write(self, o): 
self.db.write(o) 
As we see, for a lot of real-live examples M-I can be used 
straight-forward and its behavior in Python “makes sense” 
13 © 2014 Zaar Hai tech.zarmory.com
“New style” classes 
Not exactly “new” - since Python 2.2 
Always inherit from object 
Really, Always inherit from object 
Use super to call ancestor methods 
class Foo(object): 
def bar(self): pass 
class Bar(Foo): 
def bar(self): 
return super(Bar, self).bar() 
class Foo: 
def bar(self): pass 
class Bar(Foo): 
def bar(self): 
return Foo.bar(self) 
Following the above technique will make your and others' life 
much easier 
14 © 2014 Zaar Hai tech.zarmory.com
Multiple inheritance and MRO 
class A(object): 
def __init__(self): 
super(A, self).__init__() 
print "A" 
class B(object): 
def __init__(self): 
super(B, self).__init__() 
print "B" 
class C(A,B): 
def __init__(self): 
super(C, self).__init__() 
print "C" 
What will be output of running C()? 
“A B C”? 
“B A C”? 
How do we arrive from super(A..) to the method of B? 
15 © 2014 Zaar Hai tech.zarmory.com
Multiple inheritance and MRO 
MRO – method resolution order 
Python utilizes C3 algorithm 
>>> C.__mro__ 
(<class 'my2.C'>, <class 'my2.A'>, <class 'my2.B'>, <type 'object'>) 
And the answer to the previous question is “B A C” 
For most real-life example I've seen, C3 logic “makes sense” 
Without inheriting from object and using super, one had to 
do MRO by himself in the code. 
Bottom line – super() does not return ancestor, but the next 
member of MRO chain 
16 © 2014 Zaar Hai tech.zarmory.com
MRO pitfalls 
class A(object): pass 
class B(object): pass 
class C(A,B): pass 
class D(B,A): pass 
class E(C,D): pass 
Where super(E, self).__init__() will end up? 
17 © 2014 Zaar Hai tech.zarmory.com
MRO pitfalls 
class A(object): pass 
class B(object): pass 
class C(A,B): pass 
class D(B,A): pass 
class E(C,D): pass 
Where super(E, self).__init__() will end up? 
>>> class E(C,D): pass 
... 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
TypeError: Error when calling the metaclass bases 
Cannot create a consistent method resolution 
order (MRO) for bases B, A 
There are cases when C3 would (and should) fail to prevent a 
naive programmer from creating a mess 
18 © 2014 Zaar Hai tech.zarmory.com
type function revised 
All of you are familiar with type: 
>>> class A(object): pass 
>>> a = A() 
>>> type(a) 
<class '__main__.A'> 
BTW, don't use type(a) == A, but isinstance(a, A) 
What type(A) will print? 
19 © 2014 Zaar Hai tech.zarmory.com
type function revised 
>>> type(A) 
<type 'type'> 
In Python, object model hierarchy has two levels: 
Objects are instances of their Class 
Classes are instances of their Type – AKA metaclass 
All classes by default are instances of type metaclass, but we 
can define our own metaclasses of course: 
class MetaA(type): pass 
class A(object): 
__metaclass__ = MetaA 
>>> type(A) 
<class '__main__.MetaA'> 
Metaclass can augment class creation. More on this later 
20 © 2014 Zaar Hai tech.zarmory.com
Yet another face of type function 
type can be used to create new classes on the fly 
The syntax: type(name, bases, dict) 
class Actor(object): 
def get(self, req): 
return self.val 
for c in [Volumes, Hosts]: 
class Conf(object): pass 
class Volumes(Conf): 
url = '/volumes' 
val = 5 
class Hosts(Conf): 
url = '/hosts' 
val = 4 
HandlerClass = type('Handler', (Actor, c), {}) 
HttpServer.addUrl(c.url, HandlerClass) 
HttpServer.start() 
Allows really decoupled design. Yet flexible and efficient. 
21 © 2014 Zaar Hai tech.zarmory.com
Metaclass example – proper Enum 
class UserRoles(Enum): 
root = 10 
user = 20 
user__desc = "Regular users" 
Nobody = 30 
>>> UserRoles.root 
10 
>>> UserRoles.root.name 
'root' # Wow, I have a name! 
>>> UserRoles.user.desc 
'Regular users' # And even description 
>>> UserRoles.root == 10 
True # I look and quack just like the native type I was assigned to 
>>> role = UserRoles(10) # Cast me! (with the DB value for example) 
>>> role; role.name; role == 10 
10 
'root' 
True 
>>> role == UserRoles.root 
True 
Magic? May be; but a simple one 
22 © 2014 Zaar Hai tech.zarmory.com
Magic revealed 
class EnumMeta(type): 
def __new__(mcs, name, base, d): 
cls = super(EnumMeta, mcs).__new__(mcs, name, base, d) 
for k, v in cls.__dict__.items(): 
pname = getattr(cls, k+"__name", k) 
pdesc = getattr(cls, k+"__desc", "") 
n = type(v).__name__ 
prop = type(n, (type(v),), {"name" : pname, "desc" : pdesc}) 
p = prop(v) 
setattr(cls, k, p) 
return cls 
class Enum(object): 
__metaclass__ = EnumMeta 
Full story here 
23 © 2014 Zaar Hai tech.zarmory.com
© 2014 Zaar Hai tech.zarmory.com 
Thank you …

More Related Content

What's hot

Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonSiddhi
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)David de Boer
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
Opaque Pointers Are Coming
Opaque Pointers Are ComingOpaque Pointers Are Coming
Opaque Pointers Are ComingNikita Popov
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerMario Fusco
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskellnebuta
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 

What's hot (20)

Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Trafaret: monads and python
Trafaret: monads and pythonTrafaret: monads and python
Trafaret: monads and python
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
Opaque Pointers Are Coming
Opaque Pointers Are ComingOpaque Pointers Are Coming
Opaque Pointers Are Coming
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 

Similar to Advanced Python, Part 1

Python decorators
Python decoratorsPython decorators
Python decoratorsAlex Su
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developersgeorgebrock
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2Graham Dumpleton
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved againrik0
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the BasicsMichael Koby
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java DevelopersYakov Fain
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Workhorse Computing
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicGraham Dumpleton
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicNew Relic
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
 
Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonPython Ireland
 

Similar to Advanced Python, Part 1 (20)

Python decorators
Python decoratorsPython decorators
Python decorators
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
Django Good Practices
Django Good PracticesDjango Good Practices
Django Good Practices
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2
 
Django Heresies
Django HeresiesDjango Heresies
Django Heresies
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved again
 
обзор Python
обзор Pythonобзор Python
обзор Python
 
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the Basics
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New Relic
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in Python
 

More from Zaar Hai

When Less is More - Save Brain Cycles with GKE Autopilot and Cloud Run
When Less is More - Save Brain Cycles with GKE Autopilot and Cloud RunWhen Less is More - Save Brain Cycles with GKE Autopilot and Cloud Run
When Less is More - Save Brain Cycles with GKE Autopilot and Cloud RunZaar Hai
 
Google auth dispelling the magic
Google auth   dispelling the magicGoogle auth   dispelling the magic
Google auth dispelling the magicZaar Hai
 
Google auth - dispelling the magic
Google auth - dispelling the magicGoogle auth - dispelling the magic
Google auth - dispelling the magicZaar Hai
 
Deep into Prometheus
Deep into PrometheusDeep into Prometheus
Deep into PrometheusZaar Hai
 
Dip into prometheus
Dip into prometheusDip into prometheus
Dip into prometheusZaar Hai
 
Apache ignite - a do-it-all key-value db?
Apache ignite - a do-it-all key-value db?Apache ignite - a do-it-all key-value db?
Apache ignite - a do-it-all key-value db?Zaar Hai
 

More from Zaar Hai (6)

When Less is More - Save Brain Cycles with GKE Autopilot and Cloud Run
When Less is More - Save Brain Cycles with GKE Autopilot and Cloud RunWhen Less is More - Save Brain Cycles with GKE Autopilot and Cloud Run
When Less is More - Save Brain Cycles with GKE Autopilot and Cloud Run
 
Google auth dispelling the magic
Google auth   dispelling the magicGoogle auth   dispelling the magic
Google auth dispelling the magic
 
Google auth - dispelling the magic
Google auth - dispelling the magicGoogle auth - dispelling the magic
Google auth - dispelling the magic
 
Deep into Prometheus
Deep into PrometheusDeep into Prometheus
Deep into Prometheus
 
Dip into prometheus
Dip into prometheusDip into prometheus
Dip into prometheus
 
Apache ignite - a do-it-all key-value db?
Apache ignite - a do-it-all key-value db?Apache ignite - a do-it-all key-value db?
Apache ignite - a do-it-all key-value db?
 

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Advanced Python, Part 1

  • 1. Topics in Advanced Python No agenda – jumping in... © 2014 Zaar Hai tech.zarmory.com
  • 2. Decorators Decorator is a function that accepts a function and returns another function from functools import wraps def deco(f): @wraps(f) def wrapper(*args, **kwargs): print "started at %s" % time.time() rv = f(*args, **kwargs) print "finished at %s" % time.time() return wrapper @deco def sleeper(): "Sleeper function" time.sleep(2) >>> sleeper() started at 1384245972.01 finished at 1384245974.01 >>> print sleeper.__name__ sleeper wraps takes care about wrapped aux data. Without it: >>> print sleeper.__name__ wrapper Important when reading online help of someone else's code 2 © 2014 Zaar Hai tech.zarmory.com
  • 3. Decorator usage examples To return cached values To implement retries To guard methods/functions with a lock To validate input/output of the function To limit function execution by a certain timeout Decorators greatly help with code reuse and Make code explicitly readable 3 © 2014 Zaar Hai tech.zarmory.com
  • 4. Decorator is just a function You can call decorators “inline” in your code @deco def sleeper(): pass sleeper() deco(sleeper)() @deco1 @deco2 def sleeper(): pass sleeper() deco1(deco2((sleeper))() Similarly, decorators themselves can accept parameters @deco(5) def sleeper(): pass sleeper() deco(5)(sleeper)() Note: The invocation order is a bit different Note:@deco(5) is executed when the code is imported 4 © 2014 Zaar Hai tech.zarmory.com
  • 5. Decorator with parameter - example $ cat mydeco.py def deco(p): print "got %s" % p def real_deco(f): @wraps(f) def wrapper(*args, **kwargs): print "decorating" return f(*args, **kwargs) return wrapper return real_deco @deco(5) def hello(): pass >>> import mydeco # outer function runs during import got 5 >>> mydeco.hello() # decorator runs when function executed decorating >>> 5 © 2014 Zaar Hai tech.zarmory.com
  • 6. Decorator can be a class Some times its useful to implement decorators as a class class deco(object): def __init__(self, p): self.p = p print "got %s" % p def __call__(self, f): @wraps(f) def wrapper(*args, **kwargs): print "decorating" return f(*args, **kwargs) return wrapper __call__ is a special method that is invoked when you try calling an object as if it was a function 6 © 2014 Zaar Hai tech.zarmory.com
  • 7. How does @property decorator work? @property – one of the most common decorators in Python class A(object): @property def a(self): return "a" >>> A().a 'a' But how does it work? 7 © 2014 Zaar Hai tech.zarmory.com
  • 8. Meet descriptors Descriptor is a protocol for accessing object attributes class A(object): def __init__(self): self.a = 1 >>> f = A(); f.a When you access a, what actually happens is v = self.__dict__["a"] if hasattr(v, '__get__'): return v.__get__(self) else: return v I.e. when attribute defines __get__ (or __set__) methods, they are called to produce (or set) actual attribute value 8 © 2014 Zaar Hai tech.zarmory.com
  • 9. Back to the @property @property implements both decorator and descriptor semantics like this: class Property(object): def __init__(self, getter): self.getter = getter def __get__(self, obj, csl): return self.getter(obj) class A(object): @Property def a(self): return "a" >>> o = A(); o.a 'a' >>> type(o.a) <type 'str'> 9 © 2014 Zaar Hai tech.zarmory.com
  • 10. Descriptors in action One-time caching of method's return values class OneTime(object): def __init__(self, func): self.func = func def __get__(self, obj, cls): to_augment = obj or cls rv = self.func(to_augment) pname = self.func.__name__ setattr(to_augment, pname, rv) return rv class BigMath(object): @OneTime def big_data(self): print "working hard...." return 42 Reusable – follows DRY principle More on this here and here class BigMath(object): def get_big_data(self): if hasattr(self, "_rv"): return self._rv self._rv = self._get_data() return self._rv def _get_data(self): print "working hard...." return 42 Nice, but leads to a lot of copy/paste code 10 © 2014 Zaar Hai tech.zarmory.com
  • 11. © 2014 Zaar Hai tech.zarmory.com Multiple inheritance “LEGO” goes on
  • 12. M-I in action - MixIns “MixIn” is a programming concept about creating aux class that enriches functionality of the main class class SteeringWheel(object): ... class HornMixIn(object): """Mix-in class to enable horn functionality""" def horn(self, *args, **kwargs): print "move over!" class CruiseControlMixIn(object): def cc_set(self): … def cc_cancel(self): ... def cc_restore(self):... class Mazda2SteeringWheel(CruiseControlMixIn, HornMixIn, SteeringWheel): """Sometimes there is nothing to configure here at all""" 12 © 2014 Zaar Hai tech.zarmory.com
  • 13. M-I in action – MixIns (continued) Here is another example class Synchronized(object): def __init__(self, *args, **kwargs): self._sync_lock = threading.Lock() super(Synchronized, self).__init__(*args, **kwargs) @staticmethod def synchronized(func): @wraps(func) def wrapper(self, *args, **kwargs): with self._sync_lock: return func(self, *args, **kwargs) return wrapper class Writer(Syncrhonized, Db): @Synchronized.synchronized def write(self, o): self.db.write(o) As we see, for a lot of real-live examples M-I can be used straight-forward and its behavior in Python “makes sense” 13 © 2014 Zaar Hai tech.zarmory.com
  • 14. “New style” classes Not exactly “new” - since Python 2.2 Always inherit from object Really, Always inherit from object Use super to call ancestor methods class Foo(object): def bar(self): pass class Bar(Foo): def bar(self): return super(Bar, self).bar() class Foo: def bar(self): pass class Bar(Foo): def bar(self): return Foo.bar(self) Following the above technique will make your and others' life much easier 14 © 2014 Zaar Hai tech.zarmory.com
  • 15. Multiple inheritance and MRO class A(object): def __init__(self): super(A, self).__init__() print "A" class B(object): def __init__(self): super(B, self).__init__() print "B" class C(A,B): def __init__(self): super(C, self).__init__() print "C" What will be output of running C()? “A B C”? “B A C”? How do we arrive from super(A..) to the method of B? 15 © 2014 Zaar Hai tech.zarmory.com
  • 16. Multiple inheritance and MRO MRO – method resolution order Python utilizes C3 algorithm >>> C.__mro__ (<class 'my2.C'>, <class 'my2.A'>, <class 'my2.B'>, <type 'object'>) And the answer to the previous question is “B A C” For most real-life example I've seen, C3 logic “makes sense” Without inheriting from object and using super, one had to do MRO by himself in the code. Bottom line – super() does not return ancestor, but the next member of MRO chain 16 © 2014 Zaar Hai tech.zarmory.com
  • 17. MRO pitfalls class A(object): pass class B(object): pass class C(A,B): pass class D(B,A): pass class E(C,D): pass Where super(E, self).__init__() will end up? 17 © 2014 Zaar Hai tech.zarmory.com
  • 18. MRO pitfalls class A(object): pass class B(object): pass class C(A,B): pass class D(B,A): pass class E(C,D): pass Where super(E, self).__init__() will end up? >>> class E(C,D): pass ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases B, A There are cases when C3 would (and should) fail to prevent a naive programmer from creating a mess 18 © 2014 Zaar Hai tech.zarmory.com
  • 19. type function revised All of you are familiar with type: >>> class A(object): pass >>> a = A() >>> type(a) <class '__main__.A'> BTW, don't use type(a) == A, but isinstance(a, A) What type(A) will print? 19 © 2014 Zaar Hai tech.zarmory.com
  • 20. type function revised >>> type(A) <type 'type'> In Python, object model hierarchy has two levels: Objects are instances of their Class Classes are instances of their Type – AKA metaclass All classes by default are instances of type metaclass, but we can define our own metaclasses of course: class MetaA(type): pass class A(object): __metaclass__ = MetaA >>> type(A) <class '__main__.MetaA'> Metaclass can augment class creation. More on this later 20 © 2014 Zaar Hai tech.zarmory.com
  • 21. Yet another face of type function type can be used to create new classes on the fly The syntax: type(name, bases, dict) class Actor(object): def get(self, req): return self.val for c in [Volumes, Hosts]: class Conf(object): pass class Volumes(Conf): url = '/volumes' val = 5 class Hosts(Conf): url = '/hosts' val = 4 HandlerClass = type('Handler', (Actor, c), {}) HttpServer.addUrl(c.url, HandlerClass) HttpServer.start() Allows really decoupled design. Yet flexible and efficient. 21 © 2014 Zaar Hai tech.zarmory.com
  • 22. Metaclass example – proper Enum class UserRoles(Enum): root = 10 user = 20 user__desc = "Regular users" Nobody = 30 >>> UserRoles.root 10 >>> UserRoles.root.name 'root' # Wow, I have a name! >>> UserRoles.user.desc 'Regular users' # And even description >>> UserRoles.root == 10 True # I look and quack just like the native type I was assigned to >>> role = UserRoles(10) # Cast me! (with the DB value for example) >>> role; role.name; role == 10 10 'root' True >>> role == UserRoles.root True Magic? May be; but a simple one 22 © 2014 Zaar Hai tech.zarmory.com
  • 23. Magic revealed class EnumMeta(type): def __new__(mcs, name, base, d): cls = super(EnumMeta, mcs).__new__(mcs, name, base, d) for k, v in cls.__dict__.items(): pname = getattr(cls, k+"__name", k) pdesc = getattr(cls, k+"__desc", "") n = type(v).__name__ prop = type(n, (type(v),), {"name" : pname, "desc" : pdesc}) p = prop(v) setattr(cls, k, p) return cls class Enum(object): __metaclass__ = EnumMeta Full story here 23 © 2014 Zaar Hai tech.zarmory.com
  • 24. © 2014 Zaar Hai tech.zarmory.com Thank you …