Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a58fe90

Browse files
VishnuDuttSharmanorvig
authored andcommitted
Python implementation for psource (aimacode#613) (aimacode#619)
* Added python code for psource (aimacode#613) * added psource code with optional highlighting (aimacode#613)
1 parent b47888c commit a58fe90

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

notebook.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from inspect import getsource
2+
13
from utils import argmax, argmin
24
from games import TicTacToe, alphabeta_player, random_player, Fig52Extended, infinity
35
from logic import parse_definite_clause, standardize_variables, unify, subst
@@ -24,6 +26,23 @@ def psource(*functions):
2426
# ______________________________________________________________________________
2527

2628

29+
def psource(*functions):
30+
"Print the source code for the given function(s)."
31+
source_code = '\n\n'.join(getsource(fn) for fn in functions)
32+
try:
33+
from pygments.formatters import HtmlFormatter
34+
from pygments.lexers import PythonLexer
35+
from pygments import highlight
36+
37+
display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True))))
38+
39+
except ImportError:
40+
print(source_code)
41+
42+
43+
# ______________________________________________________________________________
44+
45+
2746
def show_iris(i=0, j=1, k=2):
2847
"""Plots the iris dataset in a 3D plot.
2948
The three axes are given by i, j and k,

0 commit comments

Comments
 (0)