From d2e32ec74c37e8e265e3d0c2da55d651e649b9a3 Mon Sep 17 00:00:00 2001 From: VishnuDuttSharma Date: Fri, 11 Aug 2017 20:28:11 +0530 Subject: [PATCH 1/2] Added python code for psource (#613) --- notebook.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/notebook.py b/notebook.py index bfc34651f..cbb69c03d 100644 --- a/notebook.py +++ b/notebook.py @@ -1,4 +1,10 @@ from IPython.display import HTML, display + +from inspect import getsource +from pygments.formatters import HtmlFormatter +from pygments.lexers import PythonLexer +from pygments import highlight + from utils import argmax, argmin from games import TicTacToe, alphabeta_player, random_player, Fig52Extended, infinity from logic import parse_definite_clause, standardize_variables, unify, subst @@ -15,6 +21,14 @@ # ______________________________________________________________________________ +def psource(*functions): + "Print the source code for the given function(s)." + display(HTML(highlight(''.join(getsource(fn) for fn in functions), PythonLexer(), HtmlFormatter(full=True)))) + + +# ______________________________________________________________________________ + + def show_iris(i=0, j=1, k=2): '''Plots the iris dataset in a 3D plot. The three axes are given by i, j and k, From a5af1c1b49387b2b8d1083e7c5c21040d4f9bd8e Mon Sep 17 00:00:00 2001 From: VishnuDuttSharma Date: Sun, 13 Aug 2017 10:06:20 +0530 Subject: [PATCH 2/2] added psource code with optional highlighting (#613) --- notebook.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/notebook.py b/notebook.py index cbb69c03d..902ce1040 100644 --- a/notebook.py +++ b/notebook.py @@ -1,9 +1,6 @@ from IPython.display import HTML, display from inspect import getsource -from pygments.formatters import HtmlFormatter -from pygments.lexers import PythonLexer -from pygments import highlight from utils import argmax, argmin from games import TicTacToe, alphabeta_player, random_player, Fig52Extended, infinity @@ -23,7 +20,16 @@ def psource(*functions): "Print the source code for the given function(s)." - display(HTML(highlight(''.join(getsource(fn) for fn in functions), PythonLexer(), HtmlFormatter(full=True)))) + source_code = '\n\n'.join(getsource(fn) for fn in functions) + try: + from pygments.formatters import HtmlFormatter + from pygments.lexers import PythonLexer + from pygments import highlight + + display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True)))) + + except ImportError: + print(source_code) # ______________________________________________________________________________