In [ ]:
try:
    get_ipython
except:
    import sys
    def get_ipython():
        class dummy(object):
            def run_cell_magic(*args):
                print "No IPython; therefore no magic!"
            magic = run_cell_magic
        return dummy()

Test Module for importing


In [2]:
def foo():
    return 'bar'

In [3]:
%ls

Work out a scheme s.t. cell magics %%test do not get written to .py file


In [1]:
from IPython.core.magic import register_cell_magic
@register_cell_magic
def test(line,cell):
    g = get_ipython().user_global_ns
    code = get_ipython().input_transformer_manager.transform_cell(cell)
    path = '?'
    ec = '?'
    ccode = compile(code,'<file: {0} cell {1}>'.format(path,ec),'exec')
    v = eval(ccode,g)
    return code

In [2]:
%%test
a = 3
b = 4
a+b


Out[2]:
u'a = 3\nb = 4\na+b\n'

In [ ]:
##test

In [7]:
%lsmagic


Out[7]:
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %install_default_config  %install_ext  %install_profiles  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%test  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

In [4]:
#%%sh
#ls -l *

In [5]:
%%bash
ls -l
/bin/pwd
echo "foo bar"


total 24
-rw-r--r-- 1 nholtz nholtz    5 May 14 08:32 __init__.py
-rw-r--r-- 1 nholtz nholtz  123 May 14 08:32 __init__.pyc
-rw-r--r-- 1 nholtz nholtz  876 May 14 08:31 Test Module.ipynb
-rw-r--r-- 1 nholtz nholtz 2540 May 15 10:31 TestModule.ipynb
-rw-r--r-- 1 nholtz nholtz  194 May 15 10:31 TestModule.py
-rw-r--r-- 1 nholtz nholtz  235 May 15 10:31 TestModule.pyc
/files/home/nholtz/work/git/structural-analysis/matrix-methods/frame2d/V04/Fred/George
foo bar

In [6]:
## Test Section:

# this doesn't get imported

def bar():
    return 'foo'

In [7]:
# neither does this:

In [8]:
def zap():
    return

In [9]:
#foo

In [ ]: