Reverse engineering included

With ipython you can write a function, like:

prompt [1]# def parse(line):
    ip, host = line.split()
    return "{host} IN PTR {ip}".format(host=host,ip=ip)

To edit our function, just use %edit and reference the line

prompt [2]# %edit 1

Once you modify the function, you cannot reference the newer code with edit, as

prompt [3]# %edit 2

just references “%edit 1” and not the newer code.

In this case we can simply recover the last code of our function with

prompt [4]# from inspect import getsourcelines
prompt [5]# getsourcelines(parse)
(['def parse(line):\n',
'    ip, host = line.split()[:2]\n',
'    return "{host} IN PTR {ip}".format(host=host,ip=ip)\n'],
1)

bashing ipython

iPython is a wonderful tool that avoids continuosly switching from bash to other utilities like bc, perl & co.

One of its limitation is the I/O redirection – at which bash is really good of. As iPython py-shell profile uses /bin/sh by default – thru os.system, I implemented a quick and dirty system replacement that diverts it into bash.

I added the following line here .ipython/profile_pysh/ipython_config.py

import os
def system2(cmd):
  pid = os.fork()
  if pid == 0:
    args = ['/bin/bash', '-c', cmd ]
    return os.execvp("/bin/bash", args)
  else:
    c_pid, status = os.waitpid(pid, 0)
    return status

print("Overriding os.system with bash")
os.system = system2

# or you can simply use subprocess if available
os.system = lambda cmd: subprocess.call(cmd, shell=True,executable='/bin/bash')

While py-shell profile runs commands for every call outside python globals(), other profiles use the `bang` syntax.
Eg. ! ls -l

To work it out too, I just changed the following line in
/usr/lib/python2.7/dist-packages/IPython/utils/_process_common.py
71 p = subprocess.Popen(cmd, shell=True,
72 executable='/bin/bash',
73 stdin=subprocess.PIPE,