auto.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Implementation of magic functions that control various automatic behaviors.
  2. """
  3. from __future__ import print_function
  4. from __future__ import absolute_import
  5. #-----------------------------------------------------------------------------
  6. # Copyright (c) 2012 The IPython Development Team.
  7. #
  8. # Distributed under the terms of the Modified BSD License.
  9. #
  10. # The full license is in the file COPYING.txt, distributed with this software.
  11. #-----------------------------------------------------------------------------
  12. #-----------------------------------------------------------------------------
  13. # Imports
  14. #-----------------------------------------------------------------------------
  15. # Our own packages
  16. from IPython.core.magic import Bunch, Magics, magics_class, line_magic
  17. from IPython.testing.skipdoctest import skip_doctest
  18. from logging import error
  19. #-----------------------------------------------------------------------------
  20. # Magic implementation classes
  21. #-----------------------------------------------------------------------------
  22. @magics_class
  23. class AutoMagics(Magics):
  24. """Magics that control various autoX behaviors."""
  25. def __init__(self, shell):
  26. super(AutoMagics, self).__init__(shell)
  27. # namespace for holding state we may need
  28. self._magic_state = Bunch()
  29. @line_magic
  30. def automagic(self, parameter_s=''):
  31. """Make magic functions callable without having to type the initial %.
  32. Without argumentsl toggles on/off (when off, you must call it as
  33. %automagic, of course). With arguments it sets the value, and you can
  34. use any of (case insensitive):
  35. - on, 1, True: to activate
  36. - off, 0, False: to deactivate.
  37. Note that magic functions have lowest priority, so if there's a
  38. variable whose name collides with that of a magic fn, automagic won't
  39. work for that function (you get the variable instead). However, if you
  40. delete the variable (del var), the previously shadowed magic function
  41. becomes visible to automagic again."""
  42. arg = parameter_s.lower()
  43. mman = self.shell.magics_manager
  44. if arg in ('on', '1', 'true'):
  45. val = True
  46. elif arg in ('off', '0', 'false'):
  47. val = False
  48. else:
  49. val = not mman.auto_magic
  50. mman.auto_magic = val
  51. print('\n' + self.shell.magics_manager.auto_status())
  52. @skip_doctest
  53. @line_magic
  54. def autocall(self, parameter_s=''):
  55. """Make functions callable without having to type parentheses.
  56. Usage:
  57. %autocall [mode]
  58. The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
  59. value is toggled on and off (remembering the previous state).
  60. In more detail, these values mean:
  61. 0 -> fully disabled
  62. 1 -> active, but do not apply if there are no arguments on the line.
  63. In this mode, you get::
  64. In [1]: callable
  65. Out[1]: <built-in function callable>
  66. In [2]: callable 'hello'
  67. ------> callable('hello')
  68. Out[2]: False
  69. 2 -> Active always. Even if no arguments are present, the callable
  70. object is called::
  71. In [2]: float
  72. ------> float()
  73. Out[2]: 0.0
  74. Note that even with autocall off, you can still use '/' at the start of
  75. a line to treat the first argument on the command line as a function
  76. and add parentheses to it::
  77. In [8]: /str 43
  78. ------> str(43)
  79. Out[8]: '43'
  80. # all-random (note for auto-testing)
  81. """
  82. if parameter_s:
  83. arg = int(parameter_s)
  84. else:
  85. arg = 'toggle'
  86. if not arg in (0, 1, 2, 'toggle'):
  87. error('Valid modes: (0->Off, 1->Smart, 2->Full')
  88. return
  89. if arg in (0, 1, 2):
  90. self.shell.autocall = arg
  91. else: # toggle
  92. if self.shell.autocall:
  93. self._magic_state.autocall_save = self.shell.autocall
  94. self.shell.autocall = 0
  95. else:
  96. try:
  97. self.shell.autocall = self._magic_state.autocall_save
  98. except AttributeError:
  99. self.shell.autocall = self._magic_state.autocall_save = 1
  100. print("Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall])