Guest User

Untitled

a guest
Jun 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import sys
  2. def get_caller(ext=False):
  3. """ Get the caller of the caller of this function. If the optional ext parameter is given, returns the line's text as well. """
  4. f=sys._getframe(2)
  5. s=(f.f_code.co_filename, f.f_lineno)
  6. del f
  7. if ext:
  8. import linecache
  9. s=(s[0], s[1], linecache.getline(s[0], s[1]))
  10.  
  11. return s
  12.  
  13. def post_event(e):
  14. caller=get_caller(True)
  15. print "Event %r posted from %r"%(e, caller)
  16.  
  17. ## Testing the functions.
  18.  
  19. def q():
  20. post_event("baz")
  21.  
  22. post_event("foo")
  23. print "Hello!"
  24. q()
  25.  
  26. Event 'foo' posted from ('getcaller.py', 20, 'post_event("foo")n')
  27. Hello!
  28. Event 'baz' posted from ('getcaller.py', 17, 'tpost_event("baz")n')
Add Comment
Please, Sign In to add comment