From 81a8174a3f4ed67099e84ef8b3b9efeb25579630 2011-11-24 06:52:00 From: Matthias BUSSONNIER Date: 2011-11-24 06:52:00 Subject: [PATCH] fix docstrig, replace eval by regExp replace the `eval` to transform the `repr()` of the magic list send by the kernel by a regular expression, change the docstring to be numpy complient (I hope so) --- diff --git a/IPython/frontend/qt/console/frontend_widget.py b/IPython/frontend/qt/console/frontend_widget.py index c3c7e9e..1f62432 100644 --- a/IPython/frontend/qt/console/frontend_widget.py +++ b/IPython/frontend/qt/console/frontend_widget.py @@ -316,11 +316,25 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): def _silent_exec_callback(self, expr, callback): """Silently execute `expr` in the kernel and call `callback` with reply - `expr` : valid string to be executed by the kernel. - `callback` : function accepting one string as argument. + the `expr` is evaluated silently in the kernel (without) output in + the frontend. Call `callback` with the + `repr `_ as first argument + + Parameters + ---------- + expr : string + valid string to be executed by the kernel. + callback : function + function accepting one arguement, as a string. The string will be + the `repr` of the result of evaluating `expr` The `callback` is called with the 'repr()' of the result of `expr` as - first argument. To get the object, do 'eval()' on the passed value. + first argument. To get the object, do 'eval()' onthe passed value. + + See Also + -------- + _handle_exec_callback : private method, deal with calling callback with reply + """ # generate uuid, which would be used as a indication of wether or not @@ -334,23 +348,26 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): def _handle_exec_callback(self, msg): """Execute `callback` corresonding to `msg` reply, after ``_silent_exec_callback`` - `msg` : raw message send by the kernel containing an `user_expressions` + Parameters + ---------- + msg : raw message send by the kernel containing an `user_expressions` and having a 'silent_exec_callback' kind. + Notes + ----- This fonction will look for a `callback` associated with the corresponding message id. Association has been made by - ``_silent_exec_callback``. `callback`is then called with the `repr()` + `_silent_exec_callback`. `callback` is then called with the `repr()` of the value of corresponding `user_expressions` as argument. `callback` is then removed from the known list so that any message coming again with the same id won't trigger it. + """ - cnt = msg['content'] - ue = cnt['user_expressions'] - for i in ue.keys(): - if i in self._callback_dict: - self._callback_dict[i](ue[i]) - self._callback_dict.pop(i) + user_exp = msg['content']['user_expressions'] + for expression in user_exp: + if expression in self._callback_dict: + self._callback_dict.pop(expression)(user_exp[expression]) def _handle_execute_reply(self, msg): """ Handles replies for code execution. diff --git a/IPython/frontend/qt/console/mainwindow.py b/IPython/frontend/qt/console/mainwindow.py index aaa4c46..eaa5fe1 100644 --- a/IPython/frontend/qt/console/mainwindow.py +++ b/IPython/frontend/qt/console/mainwindow.py @@ -20,6 +20,7 @@ Authors: # stdlib imports import sys +import re import webbrowser from threading import Thread @@ -547,10 +548,23 @@ class MainWindow(QtGui.QMainWindow): def _make_dynamic_magic(self,magic): """Return a function `fun` that will execute `magic` on active frontend. - `magic` : valid python string + Parameters + ---------- + magic : string + string that will be executed as is when the returned function is called + + Returns + ------- + fun : function + function with no parameters, when called will execute `magic` on the + current active frontend at call time - return `fun`, function with no parameters + See Also + -------- + populate_all_magic_menu : generate the "All Magics..." menu + Notes + ----- `fun` execute `magic` an active frontend at the moment it is triggerd, not the active frontend at the moment it has been created. @@ -566,8 +580,13 @@ class MainWindow(QtGui.QMainWindow): def populate_all_magic_menu(self, listofmagic=None): """Clean "All Magics..." menu and repopulate it with `listofmagic` - `listofmagic` : string, repr() of a list of strings. + Parameters + ---------- + listofmagic : string, + repr() of a list of strings, send back by the kernel + Notes + ----- `listofmagic`is a repr() of list because it is fed with the result of a 'user_expression' """ @@ -577,8 +596,8 @@ class MainWindow(QtGui.QMainWindow): # list of protected magic that don't like to be called without argument # append '?' to the end to print the docstring when called from the menu protected_magic = set(["more","less","load_ext","pycat","loadpy","save"]) - - for magic in eval(listofmagic): + magics=re.findall('\w+', listofmagic) + for magic in magics: if magic in protected_magic: pmagic = '%s%s%s'%('%',magic,'?') else: @@ -590,8 +609,14 @@ class MainWindow(QtGui.QMainWindow): alm_magic_menu.addAction(xaction) def update_all_magic_menu(self): + """ Update the list on magic in the "All Magics..." Menu + + Request the kernel with the list of availlable magic and populate the + menu with the list received back + + """ # first define a callback which will get the list of all magic and put it in the menu. - self.active_frontend._silent_exec_callback('get_ipython().lsmagic()',self.populate_all_magic_menu) + self.active_frontend._silent_exec_callback('get_ipython().lsmagic()', self.populate_all_magic_menu) def init_magic_menu(self): self.magic_menu = self.menuBar().addMenu("&Magic") @@ -638,7 +663,7 @@ class MainWindow(QtGui.QMainWindow): statusTip="List interactive variable with detail", triggered=self.whos_magic_active_frontend) self.add_menu_action(self.magic_menu, self.whos_action) - + def init_window_menu(self): self.window_menu = self.menuBar().addMenu("&Window") if sys.platform == 'darwin':