Best Python code snippet using nose
intro.py
Source:intro.py
...98Creating a Decorator in python99By using the concepts that we have learned about functions, let us create a simple decorator.100Example of creating a decorator101'''102def my_decor(func):103 def my_wrap():104 print("Decorator Function")105 return func()106 return my_wrap107'''108In the above code example, we have created a decorator called my_decor. 109Letâs have a look at how to use the above-created decorator.110Using a Decorator in Python111To use a decorator, we need to have a main function which we want to decorate and we need to assign the 112decorator function to a variable. Usually, the name of the variable must be the same as the function 113which we want to decorate.114Example of using a decorator115'''116def my_function():117 print("Main Function")118my_function = my_decor(my_function)119my_function()120'''121Output122Decorator Function123Main Function124Decorators with Arguments in Python:125=================================126We can use a decorator even when the function has some arguments. To do so we need to define our decorator a bit differently than before.127Example of decorator in Python128'''129def my_decor(func):130 def my_wrap(str1, str2):131 print("Decorator Function")132 return func(str1, str2)133 return my_wrap134def my_function(str1, str2):135 print("Main Function")136 print(str1 + " are " + str2)137my_function = my_decor(my_function)138my_function("Mangoes", "Sweet")139'''140Output141Decorator Function142Main Function143Mangoes are Sweet144In the above code example, we used our decorator on a function with two arguments. 145But there is still a problem with the decorator.146The decorator only works for functions with two arguments. 147What if we wish to decorate a function with more than two arguments? Or what if we wish to decorate a function that doesnât accept any arguments?148We canât define different decorators for different numbers of arguments. So to resolve this, we need to use arbitrary arguments called args and kwargs.149Any number of non-keyword arguments can be passed to args, and any number of keyword arguments can be passed to kwargs. By including both of these in the decorator, we can use the decorator with any function regardless of the number of arguments.150Example of decorators in Python151'''152def my_decor(func):153 def my_wrap(*args, **kwargs):154 print("Decorator Function")155 return func(*args, **kwargs)156 return my_wrap157def my_function(str1, str2):158 print("Main Function")159 print(str1 + " are " + str2)160my_function = my_decor(my_function)161my_function("Mangoes", "Delicious")162'''163Output164Decorator Function165Main Function166Mangoes are Delicious167Using the Decorator Syntax in Python168=====================================169Python makes using decorators a lot easier. Instead of assigning the decorator to a variable, we can simply use the @ sign. This is the most common method of implementing decorators.170Example of decorator in Python171'''172def my_decor(func):173 def my_wrap(*args, **kwargs):174 print("Decorator Function")175 return func(*args, **kwargs)176 return my_wrap177@my_decor178def my_function(str1, str2):179 print("Main Function")180 print(str1 + " are " + str2)181my_function("Mangoes", "Delicious")182'''183Output184Decorator Function185Main Function186Mangoes are Delicious187Multiple Decorators in Python188=============================189In Python, we can apply several decorators to a single function. The decorators, on the other hand, will be used in the sequence that weâve designated. This is called chaining decorators.190Example of multiple decorators in Python191'''192def my_decor(func):193 def my_wrap(*args, **kwargs):194 print("Decorator Function 1")195 return func(*args, **kwargs)196 return my_wrap197def my_another_decor(func):198 def my_wrap(*args, **kwargs):199 print("Decorator Function 2")200 return func(*args, **kwargs)201 return my_wrap202@my_decor203@my_another_decor204def my_function(str1, str2):205 print("Main Function")206 print(str1 + " are " + str2)...
20Decor.py
Source:20Decor.py
...14# return "ÐÑивеÑ, Ñ ÑÑнкÑÐ¸Ñ 'ÐÑивеÑ'"15# test = hello # пÑиваиваем пеÑеменной ÑÑнкÑиÑ, без Ñкобок (обÑекÑ)16# print(test()) # ÐÑивеÑ, Ñ ÑÑнкÑÐ¸Ñ 'ÐÑивеÑ'17# '''ÐекоÑиÑование, ÑÑнкÑÐ¸Ñ Ð²ÑÑавлÑем в ÑÑнкÑÐ¸Ñ Ð¾Ð±ÐµÑÑкии Ñ Ð¸ÑполÑзованием декоÑаÑоÑа @ '''18# def my_decor(fucn):19# def func_wrapper():20# print("Ðод до")21# fucn() # ÐÑзÑваем Ñо ÑÑо пÑилеÑÐ¸Ñ Ð² ÑÑнкÑÐ¸Ñ my_decor22# print("Ðод поÑле")23# return func_wrapper # Ðез ÑкобоÑ, без вÑполениÑ, нам нÑжно веÑнÑÑÑ ÑезÑлÑÑÐ°Ñ ÑÑого декоÑаÑоÑа24#25# @my_decor # Ð¸Ð¼Ñ Ñакоеже как Ñ Ð¾Ð±ÐµÑÑки, еÑли закоменÑиÑоваÑÑ Ñо полÑÑим ÑолÑко ÑÑÑ ÑÑнкÑиÑ26# def func_go(): # ÐºÐ°ÐºÐ°Ñ Ñо вÑоÑÐ°Ñ ÑенкÑиÑ27# print("ÐÑивеÑ, Ñ ÑÑнкÑÐ¸Ñ 'Go'")28#29# # ''' ÑÑо б избавиÑÑÑ Ð¾Ñ ÑÑого ÑÑо ниже, можно иÑполÑзÑоваÑÑ Ð´ÐµÐºÐ¾ÑаÑÐ¾Ñ '''30# # go = my_decor(func_go) # ÐекоÑиÑование, ÑÑнкÑиÑ(без Ñкобок) вÑÑавлÑем в ÑÑнкÑÐ¸Ñ Ð¾Ð±ÐµÑÑки и пÑиÑваиваем в пеÑеменнÑÑ31# # go() # запÑÑкаем, Ð´Ð»Ñ Ð·Ð°Ð¿ÑÑка ÑолÑко ÑÑÑ ÑÑваим Ñкобки32#33# func_go() # еÑли еÑÑÑ Ð´ÐµÐºÐ¾ÑаÑоÑ, Ñо запÑÑкаме ÑÑнкÑÐ¸Ñ Ñого ÑÑо Ñ
оÑем обеÑнÑÑÑ34''' ÐÑивем иÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ Ð´ÐµÐºÐ¾ÑаÑоÑа,35даннÑй декÑоÑÐ¾Ñ Ð¿Ð¾Ð»ÑÑÐ°ÐµÑ ÑекÑÑ,36пÑиводим 1 бÑÐºÐ²Ñ Ðº веÑÑ
Ð½ÐµÐ¼Ñ ÑегиÑÑÑÑ37и ÑдалÑÐµÑ Ð·Ð°Ð¿ÑÑÑе '''38''' ÑÑнкÑÐ¸Ñ Ð´ÐµÐºÑоÑоÑа, коÑоÑÐ°Ñ ÑÑо Ñо пÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ , обÑабаÑÑÐ²Ð°ÐµÑ Ð¸ возваÑаеÑ, без вÑполнениÑ'''39def make_title(fn):40 def wrapper():41 title = fn() # в ÑÑнкÑÐ¸Ñ ÑÑо Ñо пÑилеÑÐ°ÐµÑ Ð¸ ÑÑо Ñо ÑÑо Ñо заÑоваваем в пеÑеменнÑÑ42 title = title.capitalize() # беÑем пеÑеменнÑÑ Ð¸ пÑиводим 1 бÑÐºÐ²Ñ Ðº веÑÑ
Ð½ÐµÐ¼Ñ ÑегиÑÑÑÑ Ð¸ запиÑ
иваем в пеÑемен.43 title = title.replace(",","") # беÑем пеÑеменнÑÑ Ð¸ Ñдаем вÑе запÑÑÑе44 return title # возвÑаÑаем пеÑеменнÑÑ , ÑезÑлÑÑаÑ...
day.11.py
Source:day.11.py
...26"""декоÑаÑÑ"""27# def sayhallo():28# print("Hello everybody!")29# print("how are you?")30# def my_decor(my_func):31# def wrap():32# print("begin of decor")33# my_func()34# print("End of decor")35# wrap()36# my_decor(sayhallo)37# def my_decor(my_func):38# def wrap():39# print("begin of decor")40# my_func()41# print("End of decor")42# wrap()43# @my_decor44# def sayhallo():45# print("Hello everybody!")46# print("how are you?")47# # from my_function import my_lambda48# # print(my_lambda)49# @my_decor50# def removesinhle():51# a = [5, 6, 8, 7, 5, 6]...
decorators_demo.py
Source:decorators_demo.py
...18added_func = my_decorator(added)19another()20added_func()21# syntathic sugar22def my_decor(my_func):23 def wrapper():24 print('First print')25 my_func()26 print('Printed after "my_func" call')27 return wrapper28# instead of asigninig the returned function, we can use @parent funct29@my_decor # add this befroe defining the function30def func_one():31 print('func_one is printed')32@my_decor33def func_two():34 print('func_two is printed')35func_one()36func_two()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!