ein.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Python utilities to use it from ein.el
  3. Copyright (C) 2012- Takafumi Arakaki
  4. Author: Takafumi Arakaki <aka.tkf at gmail.com>
  5. ein.py is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. ein.py is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with ein.py. If not, see <http://www.gnu.org/licenses/>.
  15. """
  16. def _find_edit_target_012(*args, **kwds):
  17. from IPython.core.interactiveshell import InteractiveShell
  18. inst = InteractiveShell.instance()
  19. return inst._find_edit_target(*args, **kwds)
  20. def _find_edit_target_013(*args, **kwds):
  21. from IPython.core.interactiveshell import InteractiveShell
  22. inst = InteractiveShell.instance()
  23. return CodeMagics._find_edit_target(inst, *args, **kwds)
  24. try:
  25. from IPython.core.magics import CodeMagics
  26. _find_edit_target = _find_edit_target_013
  27. except ImportError:
  28. _find_edit_target = _find_edit_target_012
  29. def find_source(name):
  30. """Given an object as string, `name`, print its place in source code."""
  31. # FIXME: use JSON display object instead of stdout
  32. ret = _find_edit_target(name, {}, [])
  33. if ret:
  34. (filename, lineno, use_temp) = ret
  35. if not use_temp:
  36. print filename
  37. print lineno
  38. return
  39. raise RuntimeError("Source code for {0} cannot be found".format(name))
  40. def run_docstring_examples(obj, verbose=True):
  41. from IPython.core.interactiveshell import InteractiveShell
  42. import doctest
  43. inst = InteractiveShell.instance()
  44. globs = inst.user_ns
  45. return doctest.run_docstring_examples(obj, globs, verbose=verbose)