This set of IPython magic extensions is provided to the first year students enrolled in the algorithmics course at ISFATES (University of Lorraine).
In a Jupyter Notebook cell, simply paste this in a new cell and run it (shift-enter).
In [ ]:
!pip install algo_magic
Alternatively, under an open terminal:
pip install algo_magic
When you open or reopen a Jupyter notebook, you need to load the set of extensions before using any of them:
In [ ]:
%load_ext algo_magic
If you are tired of typing the previous command, you can make algo_magic
load automatically each time you open a new notebook. Execute the following command under an open terminal:
ipython profile create
Locate and edit the created file (e.g.: ~/.ipython/profile_default/ipython_config.py) and replace the following lines:
## A list of dotted module names of IPython extensions to load.
#c.InteractiveShellApp.extensions = []
by these ones:
## A list of dotted module names of IPython extensions to load.
c.InteractiveShellApp.extensions = [
"algo_magic",
]
Save the modified file and you are done.
In [ ]:
%%pep8
x = { 'a':37,'b':42,
'c':927}
y = 'hello ''world'
z = 'hello '+'world'
a = 'hello {}'.format('world')
class foo ( object ):
def f (self ):
return 37*-+2
def g(self, x,y=42):
return y
def f ( a ) :
return 37+-+a[42-x : y**3]
... will replace the cell contents with:
In [4]:
x = {'a': 37, 'b': 42, 'c': 927}
y = 'hello ' 'world'
z = 'hello ' + 'world'
a = 'hello {}'.format('world')
class foo(object):
def f(self):
return 37 * -+2
def g(self, x, y=42):
return y
def f(a):
return 37 + -+a[42 - x:y**3]
You also can load a reformatted version of any given file, without modifying it on disk:
In [ ]:
%pep8 shit.py
This extension visualizes the execution of a cell under Online Python Tutor, created by Philip Guo.
In [6]:
%%tutor 640x400
a = 1
b = 2
aux = a
a = b
b = aux
print(a, b)
Specifying the size of the embedded frame is optional.
With a single %
, you also can visualize the execution of a given Python file:
In [ ]:
%tutor swap.py
This extension generates the truth table of a given boolean formula.
In [8]:
%truth a and b
You may hide the result:
In [9]:
%truth? a and b
Replace False
and True
by conventional single characters:
In [10]:
%truth01 a and b
In [11]:
%truthFV a and b
In [12]:
%truthFT a and b
You can easily compare several boolean expressions, given on one or several lines:
In [13]:
%truth a ^ b, (a and not b) or (not a and b)
In [14]:
%truth a and b, ? not(a and b), not a, not b, ? not a or not b
In [15]:
%%truth
a and b
? not(a and b)
not a
not b
? not a or not b
Note that the generated tables can be pasted in a Markdown cell in order to appear as:
a | b | a and b | not(a and b) | not a | not b | not a or not b |
---|---|---|---|---|---|---|
False | False | False | True | True | ||
False | True | False | True | False | ||
True | False | False | False | True | ||
True | True | True | False | False |
Limitation. The boolean variables must consist in one single character.
A thin wrapper around the standard disassembler for CPython bytecode. Especially useful for comparing two variants of the same snippet, for instance:
In [1]:
%%dis
if max_so_far < current_value:
max_so_far = current_value
In [2]:
%dis max_so_far = max(max_so_far, current_value)