Shrayas (@shrayasr)
def say_something(thought):
def _thought_makes_sense(thought):
return True
if _thought_makes_sense(thought):
return thought
print say_something("That person stinks!")
That person stinks!
def say_something(thought):
def _thought_makes_sense():
print "HMMM \""+thought+"\"... I Wonder..."
return True
if _thought_makes_sense():
return thought
print say_something("That person stinks!")
HMMM "That person stinks!"... I Wonder... That person stinks!
def say_something(thought, tone=None):
return tone(thought)
def yell(thought):
return thought.upper()
def whisper(thought):
return "Shhh... " + thought.lower()
print say_something("where are you babu?", tone=yell)
print say_something("Lets go get an ice cream!", tone=whisper)
WHERE ARE YOU BABU? Shhh... lets go get an ice cream!
def mood_maker(mood):
def happy(thought):
return "^_^ " + thought + " ^_^"
def sad(thought):
return thought + " ... :'( sniff"
moods = {
"HAPPY": happy,
"SAD": sad
}
return moods[mood.upper()]
say_happily = mood_maker("happy")
print say_happily("Hello!")
say_sadly = mood_maker("sad")
print say_sadly("I lost my favourite pen!")
^_^ Hello! ^_^ I lost my favourite pen! ... :'( sniff
def masala_adder(func):
def masalad_function():
return "^#!^^@) [" + func() + "] !@&$**^&$"
return masalad_function
def say_hi():
return "Hii"
print say_hi()
masala_say_hi = masala_adder(say_hi)
print masala_say_hi()
Hii ^#!^^@) [Hii] !@&$**^&$
@masala_adder
def say_hello():
return "Hello"
say_hello()
'^#!^^@) [Hello] !@&$**^&$'
say_hello = masala_adder(say_hello)
Decorators are just syntactic sugar on a function that takes a function and returns a replacement function
A.K.A
Wrappers
# Define decorator here
def decorator(func):
def new_func():
print "before"
func()
print "after"
return new_func
@decorator
def say_hello():
print "Hello!"
say_hello()
before Hello! after
def ears(func):
def ears_wrapper():
owl_string = " /\\_/\\\n"
owl_string += func()
return owl_string
return ears_wrapper
def eyes(func):
def eyes_wrapper():
owl_string = " (O.O) \n"
owl_string += func()
return owl_string
return eyes_wrapper
def body(func):
def body_wrapper():
owl_string = " (= =)\n"
owl_string += func()
return owl_string
return body_wrapper
def legs(func):
def legs_wrapper():
owl_string = " ^^^\n"
owl_string += func()
return owl_string
return legs_wrapper
@ears
@eyes
@body
@legs
def owl():
return "hoot hoot"
print owl()
/\_/\ (O.O) (= =) ^^^ hoot hoot
owl = ears(eyes(body(legs(owl))))
def decorator(func):
def new_func(arg1, arg2):
print "I R HAZ ARGZ! [%s, %s]" % (arg1, arg2)
return func(arg1, arg2)
return new_func
@decorator
def sum_of_squares(num1, num2):
print num1*num1 + num2*num2
sum_of_squares(1,2)
I R HAZ ARGZ! [1, 2] 5
def logger(func):
def decorated_function(*args, **kwargs):
print "[INFO][Arguments]", args, kwargs
return func(*args, **kwargs)
return decorated_function
@logger
def sum_of_squares(num1, num2):
print num1*num1 + num2*num2
@logger
def csv_reader(file_path, record_delimiter=",", line_delimiter="\n"):
print "reading csv"
sum_of_squares(1,2)
csv_reader("/etc/init.d", record_delimiter=";")
[INFO][Arguments] (1, 2) {} 5 [INFO][Arguments] ('/etc/init.d',) {'record_delimiter': ';'} reading csv
Let us write a small cache!
_cache = {}
"""
1_2 => 3
"""
def cache(func):
def new_func(*args, **kwargs):
key_str = ""
for arg in args:
key_str += str(arg) + "_"
key_str = key_str[:-1]
val = _cache.get(key_str)
if not val:
val = func(*args)
_cache[key_str] = val
return val
return new_func
import time
@cache
def add_2_nos(a,b):
time.sleep(2)
return a+b
add_2_nos(1,2)
3
add_2_nos(1,2)
3
add_2_nos(10,20)
30
add_2_nos(10,20)
30
_cache
{'10_20': 30, '1_2': 3}