Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.7 KiB

  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. __ein_pytools_version = "1.1.0"
  17. try:
  18. from matplotlib import rc as __ein_rc
  19. from matplotlib import rcParams as __ein_rcParams
  20. __ein_matplotlib_available = True
  21. except ImportError:
  22. __ein_matplotlib_available = False
  23. def __ein_export_nb(nb_json, format):
  24. import IPython.nbconvert as nbconvert
  25. import IPython.nbformat as nbformat
  26. nb = nbformat.reads(nb_json, nbformat.NO_CONVERT)
  27. output = nbconvert.export_by_name(format, nb)
  28. print(output[0])
  29. def __ein_find_edit_target_012(*args, **kwds):
  30. from IPython.core.interactiveshell import InteractiveShell
  31. inst = InteractiveShell.instance()
  32. return inst._find_edit_target(*args, **kwds)
  33. def __ein_find_edit_target_013(*args, **kwds):
  34. from IPython.core.interactiveshell import InteractiveShell
  35. inst = InteractiveShell.instance()
  36. return CodeMagics._find_edit_target(inst, *args, **kwds)
  37. def __ein_find_edit_target_python(name):
  38. from inspect import getsourcefile, getsourcelines
  39. try:
  40. obj = eval(name)
  41. except NameError:
  42. return False
  43. else:
  44. sfile = getsourcefile(obj)
  45. sline = getsourcelines(obj)[-1]
  46. if sfile and sline:
  47. return(sfile, sline, False)
  48. else:
  49. return False
  50. try:
  51. from IPython.core.magics import CodeMagics
  52. __ein_find_edit_target = __ein_find_edit_target_013
  53. except ImportError:
  54. __ein_find_edit_target = __ein_find_edit_target_012
  55. def __ein_set_matplotlib_param(family, setting, value):
  56. settings = {}
  57. if __ein_matplotlib_available:
  58. settings[setting] = eval(value)
  59. __ein_rc(family, **settings)
  60. else:
  61. raise RuntimeError("Matplotlib not installed in this instance of python!")
  62. def __ein_set_figure_size(dim):
  63. __ein_set_matplotlib_param('figure', 'figsize', dim)
  64. def __ein_set_figure_dpi(dpi):
  65. __ein_set_matplotlib_param('figure', 'dpi', dpi)
  66. def __ein_get_matplotlib_params():
  67. if __ein_matplotlib_available:
  68. import json
  69. print(json.dumps([k for k in __ein_rcParams.keys()]))
  70. else:
  71. raise RuntimeError("Matplotlib not installed in this instance of python!")
  72. def __ein_find_source(name):
  73. """Given an object as string, `name`, print its place in source code."""
  74. # FIXME: use JSON display object instead of stdout
  75. ret = __ein_find_edit_target_python(name) or __ein_find_edit_target(name, {}, [])
  76. if ret:
  77. (filename, lineno, use_temp) = ret
  78. if not use_temp:
  79. print(filename)
  80. print(lineno)
  81. return
  82. raise RuntimeError("Source code for {0} cannot be found".format(name))
  83. def __ein_run_docstring_examples(obj, verbose=True):
  84. from IPython.core.interactiveshell import InteractiveShell
  85. import doctest
  86. inst = InteractiveShell.instance()
  87. globs = inst.user_ns
  88. return doctest.run_docstring_examples(obj, globs, verbose=verbose)
  89. def __ein_generate_oinfo_data(ostrings, locals=None):
  90. import json
  91. defined_objects = [__ein_maybe_undefined_object(obj, locals) for obj in ostrings
  92. if __ein_maybe_undefined_object(obj, locals) is not None]
  93. odata = [__ein_object_info_for(obj) for obj in defined_objects]
  94. print (json.dumps(odata))
  95. return odata
  96. def __ein_maybe_undefined_object(obj, locals=None):
  97. try:
  98. return eval(obj, None, locals)
  99. except Exception:
  100. return None
  101. except SyntaxError:
  102. return None
  103. def __ein_object_info_for(obj):
  104. import IPython.core.oinspect
  105. inspector = IPython.core.oinspect.Inspector()
  106. try:
  107. return inspector.info(obj)
  108. except Exception:
  109. return inspector.info(None)
  110. def __ein_print_object_info_for(obj):
  111. import json
  112. oinfo = __ein_object_info_for(obj)
  113. print (json.dumps(oinfo))
  114. def __ein_eval_hy_string(obj):
  115. try:
  116. import hy
  117. except ImportError:
  118. print("Hy not supported in this kernel. Execute `pip install hy` if you want this support.")
  119. expr = hy.read_str(obj)
  120. ret = hy.eval(expr)
  121. return ret