Guest User

Untitled

a guest
Jan 7th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. # A decorator is a function that takes a function as an argument and adds a bow tie on top of it.
  2. def decorator(func):
  3. return func
  4. #This "decorator" doesn't really decorate the input function. It just returns the input function.
  5.  
  6. @decorator
  7. def aFunc():
  8. print('hello\n')
  9. aFunc()
  10.  
  11.  
  12.  
  13. #The decorator above doesn't have a bow tie. Let's create a decorator that can decorate an input function with a bowtie.
  14. def decorate_with_bowtie(func):
  15. print('>-<')
  16. #This bow tie could be whatever logic you want it to be. The key is that the bow tie code is executed "before" (on top of) the input-function.
  17. return func
  18.  
  19. @decorate_with_bowtie
  20. def aFunc2():
  21. print('I am decorated with a bow tie')
  22. aFunc2()
  23.  
  24. #I can decorate multiple functions with a bow tie now
  25. @decorate_with_bowtie
  26. def aFunc3():
  27. print('I am decorated with a bow tie also')
  28. aFunc3()
  29.  
  30.  
  31.  
  32. # Now we are going to wrap the input function and add a bow tie to it
  33. def wrap_and_tie(func):
  34. def wrapped():
  35. #Bow tie
  36. print('>-<')
  37. #Call the function
  38. func()
  39. print('..Your function has been wrapped and bow-tied')
  40. #Return the wrapped function
  41. return wrapped
  42.  
  43. @wrap_and_tie
  44. def aFunc4():
  45. print('gift')
  46. aFunc4()
  47.  
  48.  
  49.  
  50. #Decorate a divide function with a check for a valid denominator value.
  51. def prevent_divide_by_zero(func):
  52. #To pass the arguments of the function to the wrapper:
  53. def wrapped(*args,**kwargs):
  54. #If the 2nd argument is not 0:
  55. if args[1]!=0:
  56. print("The quotient is:")
  57. #Call the input-function, passing in the keyword arguments
  58. func(*args,**kwargs)
  59. #Otherwise print that they cannot divide by zero.
  60. else:
  61. print("Can't divide by 0")
  62. #Return the wrapped function
  63. return wrapped
  64.  
  65. @prevent_divide_by_zero
  66. def divide(a,b):
  67. print(a/b)
  68.  
  69. divide(20,0)
  70. divide(20,6.0)
  71.  
  72.  
  73.  
  74. #Now for something more useful. Decorate an attempt to create a user with a user validation
  75. def validate_user(func):
  76. def wrapped(*args,**kwargs):
  77. #If the username argument is 'admin' and the password argument is 'password':
  78. if kwargs['username']=='admin' and kwargs['password']=='password':
  79. print('ACCESS GRANTED: \n{}'.format(kwargs))
  80. #execute the input-function
  81. func()
  82. #No arguments needed because the arguments have default values.
  83. #Otherwise:
  84. else:
  85. #Not needed, but first, print the keyword arguments entered
  86. print(kwargs)
  87. print('This user does not have administrative privilages.')
  88. print('ACCESS DENIED')
  89. #and return the nicely wrapped function.
  90. return wrapped
  91.  
  92. @validate_user
  93. def create_user(username="admin",password="password"):
  94. response = {'msg':"User created successfully","User":{"username":username,"password":password}}
  95. print(response)
  96.  
  97. print('\n')
  98. create_user(username='joe',password='smith')
  99. print('\n')
  100. create_user(username='admin',password='password')
  101.  
  102. #-Tutorial Doctor
Add Comment
Please, Sign In to add comment