This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # If a function can return a function, then a function can also take a function | |
| # as an argument | |
| # Defining a function which we will pass as an argument to another function | |
| def scream(word = 'yes'): | |
| return '%s!' %(word.upper()) | |
| # Function taking a function as argument | |
| def do_something_before(func): | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Shows how to manually define and use a decorator | |
| # A decorator is a function which expects another function as a parameter | |
| def my_shiny_new_decorator(a_function_to_decorate): | |
| # Inside, the decorator defines a function on the fly: "the wrapper". | |
| # This function is going to be wrapped around the original function so it can execute | |
| # code before and after the original function | |
| def the_wrapper_around_the_original_function(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # More than one decorator on a function | |
| # A decorator to make text bold | |
| def make_bold(func): | |
| def wrapper_1(): | |
| return '<b>%s</b>' %(func()) | |
| return wrapper_1 | |
| # A decorator to make text italic | |
| def make_italic(func): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Passing arguments to the decorated function | |
| # It's not magic, you just have to let the "wrapper" pass the argument | |
| def a_decorator_passing_arguments(function_to_decorate): | |
| def a_wrapper_accepting_arguments(arg1, arg2): | |
| print 'I got args! Look: %s, %s' %(arg1, arg2) | |
| function_to_decorate(arg1, arg2) | |
| return a_wrapper_accepting_arguments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Decorated methods is same as decorating functions. Just take 'self' into consideration | |
| def method_friendly_decorator(method_to_decorate): | |
| def wrapper(self, lie): | |
| lie = lie + 3 | |
| return method_to_decorate(self, lie) | |
| return wrapper | |
| class Hermione(object): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # To make a generic decorator use *args and **kwargs | |
| def a_decorator_passing_arbitrary_arguments(function_to_decorate): | |
| # The wrapper accepts any arguments | |
| def a_wrapper_accepting_arbitrary_arguments(*args, **kwargs): | |
| print 'Do I have args?' | |
| print args | |
| print kwargs | |
| function_to_decorate(*args, **kwargs) | |
| return a_wrapper_accepting_arbitrary_arguments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script will show how a decorator maker works. This is required for understanding how | |
| # we can pass arguments to the decorators | |
| def decorator_maker(): | |
| print 'I make decorators. I get executed only once when you make or create a decorator' | |
| def my_decorator(func): | |
| print 'I am a decorator. I get executed only one when you decorate a function' | |
| def wrapper(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # To show how we can pass arguments to decorators. We will use the decorator maker pattern | |
| # that we saw in the previous example | |
| def decorator_maker_with_args(decorator_arg1, decorator_arg2): | |
| print 'I make decorators. And I accept arguments: %s, %s' %(decorator_arg1, | |
| decorator_arg2) | |
| def my_decorator(func): | |
| # This is the decorator. The ability to pass arguments here is because of closure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from decorator import decorator | |
| def my_decorator_wrapper(func, first_name, last_name): | |
| print 'You passed me the args: %s, %s' %(first_name, last_name) | |
| return func(first_name, last_name) | |
| my_decorator_wrapper = decorator(my_decorator_wrapper) | |
| @my_decorator_wrapper | |
| def decorated_function(first_name, last_name): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script tests the VFS | |
| from splinter import Browser | |
| def main(browser): | |
| browser.visit(url = 'https://deals.shopsocially.com/flashsales/nikitaisawesome?cmp_id=516cfc502bba0163b6000002&offer_ref=nikitaisawesome_email') | |
| # Press the unlock link | |
| try: | |
| try: |