From anonymous, 3 Years ago, written in Python.
This paste is a reply to Re: Re: Untitled from anonymous
- view diff
Embed
  1. #!/usr/bin/python
  2.  
  3. import functools
  4.  
  5.  
  6. def cb_func(callback):
  7.     """This is a library function, taking a callback."""
  8.     print("Before calling cb")
  9.     callback("calling cb")
  10.     print("After calling cb")
  11.  
  12.  
  13. def caller():
  14.     """This is calling the library function, but I want to simplify it"""
  15.     print("Caller before cb")
  16.     data = yield cb_func
  17.     print("data: {}".format(data))
  18.     print("Caller after cb")
  19.     return
  20.  
  21.  
  22. def wrap_cb(caller):
  23.     func = caller()
  24.     cb_func = next(func)
  25.  
  26.     def _send(*args):
  27.         try:
  28.             func.send(*args)
  29.         except StopIteration:
  30.             pass
  31.  
  32.     cb_func(_send)
  33.  
  34.  
  35. wrap_cb(caller)