Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. >>> sum = lambda x, y : x + y
  2. >>> sum(1, 2)
  3. 3
  4. >>> incr = lambda y : sum(1, y)
  5. >>> incr(2)
  6. 3
  7. >>> def sum2(x, y):
  8. return x + y
  9.  
  10. >>> incr2 = functools.partial(sum2, 1)
  11. >>> incr2(4)
  12. 5
  13.  
  14. incr = lambda y : sum(1, y)
  15.  
  16. def partial(func, *part_args):
  17. def wrapper(*extra_args):
  18. args = list(part_args)
  19. args.extend(extra_args)
  20. return func(*args)
  21.  
  22. return wrapper
  23.  
  24. class EventNotifier(object):
  25. def __init__(self):
  26. self._listeners = []
  27.  
  28. def add_listener(self, callback):
  29. ''' callback should accept two positional arguments, event and params '''
  30. self._listeners.append(callback)
  31. # ...
  32.  
  33. def notify(self, event, *params):
  34. for f in self._listeners:
  35. f(event, params)
  36.  
  37. def log_event(context, event, params):
  38. context.log_event("Something happened %s, %s", event, params)
  39.  
  40. class Listener(object):
  41. def __init__(self, context):
  42. self._context = context
  43.  
  44. def __call__(self, event, params):
  45. self._context.log_event("Something happened %s, %s", event, params)
  46.  
  47.  
  48. notifier.add_listener(Listener(context))
  49.  
  50. log_listener = lambda event, params: log_event(context, event, params)
  51. notifier.add_listener(log_listener)
  52.  
  53. context = get_context() # whatever
  54. notifier.add_listener(partial(log_event, context))
  55.  
  56. # create some data
  57. import random as RND
  58. fnx = lambda: RND.randint(0, 10)
  59. data = [ (fnx(), fnx()) for c in range(10) ]
  60. target = (2, 4)
  61.  
  62. import math
  63. def euclid_dist(v1, v2):
  64. x1, y1 = v1
  65. x2, y2 = v2
  66. return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
  67.  
  68. data.sort(key=euclid_dist)
  69.  
  70. from functools import partial
  71.  
  72. p_euclid_dist = partial(euclid_dist, target)
  73.  
  74. >>> p_euclid_dist((3, 3))
  75. 1.4142135623730951
  76.  
  77. data.sort(key=p_euclid_dist)
  78.  
  79. # verify that it works:
  80. for p in data:
  81. print(round(p_euclid_dist(p), 3))
  82.  
  83. 1.0
  84. 2.236
  85. 2.236
  86. 3.606
  87. 4.243
  88. 5.0
  89. 5.831
  90. 6.325
  91. 7.071
  92. 8.602
  93.  
  94. >>> from functools import partial
  95.  
  96. >>> def fnx(a, b, c):
  97. return a + b + c
  98.  
  99. >>> fnx(3, 4, 5)
  100. 12
  101.  
  102. >>> pfnx = partial(fnx, a=12)
  103.  
  104. >>> pfnx(b=4, c=5)
  105. 21
  106.  
  107. >>> pfnx = partial(fnx, 12)
  108.  
  109. >>> pfnx(4, 5)
  110. 21
  111.  
  112. >>> pfnx = partial(fnx, a=12)
  113.  
  114. >>> pfnx(4, 5)
  115. Traceback (most recent call last):
  116. File "<pyshell#80>", line 1, in <module>
  117. pfnx(4, 5)
  118. TypeError: fnx() got multiple values for keyword argument 'a'
  119.  
  120. >>> import multiprocessing as MP
  121.  
  122. >>> # create a process pool:
  123. >>> ppool = MP.Pool()
  124.  
  125. >>> ppool.map(pfnx, [4, 6, 7, 8])
  126.  
  127. search(pattern, string, flags=0)
  128.  
  129. is_spaced_apart = partial(re.search, '[a-zA-Z]s=')
  130. is_grouped_together = partial(re.search, '[a-zA-Z]=')
  131.  
  132. is_spaced_apart(string, flags=0) # pattern '[a-zA-Z]s=' applied
  133. is_grouped_together(string, flags=0) # pattern '[a-zA-Z]=' applied
  134.  
  135. for text in lines:
  136. if is_grouped_together(text):
  137. some_action(text)
  138. elif is_spaced_apart(text):
  139. some_other_action(text)
  140. else:
  141. some_default_action()
  142.  
  143. from functools import partial
  144.  
  145. def foo(a,b):
  146. return a+b
  147.  
  148. bar = partial(foo, a=1) # equivalent to: foo(a=1, b)
  149. bar(b=10)
  150. #11 = 1+10
  151. bar(a=101, b=10)
  152. #111=101+10
  153.  
  154. from functools import partial
  155. def add(a,b):
  156. return a + b
  157.  
  158. def add2number(x,y,z):
  159. return x + y + z
  160.  
  161. if __name__ == "__main__":
  162. add2 = partial(add,2)
  163. print("result of add2 ",add2(1))
  164. add3 = partial(partial(add2number,1),2)
  165. print("result of add3",add3(1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement