Tuesday, January 24, 2012

Fun with vim and IPython kernel

#First of all, get the development version of ipython

cd /home/philip/Packages
git clone https://github.com/ipython/ipython.git


# install this into your own virtualenv (I'm assuming at this point that you have created a virtualenv and that you have activated it).

pip install /home/philip/Packages/ipython

# do a 'which ipython' to check that it worked


# you will need development libraries for zeromq
sudo apt-get install libzmq-dev
pip install pyzmq

# check pyzmq was installed into the virtualenv

(py26)philip@desktop:~/git/project$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on desktop
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>> zmq.__path__
['/home/philip/git/project/py26/lib/python2.6/site-packages/zmq']


# get the vim-ipython plugin
pip install git+https://github.com/ivanov/vim-ipython.git


# start up an IPython kernel.
 ipython kernel

# and you get output like:
(py26)philip@desktop:~/git/project$ ipython kernel
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-32459.json


# startup vim and connect to the ipython kernel
gvim -c "IPython --existing kernel-32459.json"

or startup gvim and enter
:IPython --existing kernel-32459.json

In vim add this line:
3 + 4
Hit ctrl-s while still on this line and notice that a new window opens up with the result from the ipython kernel

now enter
a = 1

# from another shell
ipython qtconsole --existing kernel-32459.json

# to start up a qtconsole and connect it to the kernel
pip install pyside
ipython qtconsole --existing kernel-32459.json

do:
print a

And notice that the result of a is 1 because that is what we set a to from our vim session.


Dealing with Tuples in Erlang


Say in Python we have a function defined like this:
def foo(**kwargs):
    pass

In languages like Python, we can pass keyword arguments to a function like:
foo(a = 1, b = 2, c = 3, d = 1)



In Erlang the function may look like this:
foo(Args) -> ok.

A call to foo in Erlang would look like:
foo([{a, 1}, {b, 2}, {c, 3}, {d, 1}]).




In Python we could check if 'a' was passed in as a keyword argument with:
return 'a' in kwargs


Whereas in Erlang we need to do:
lists:keyfind('a', 1, Args)

Note that we are looking at the 1st element in our tuple (hence the 1 in the second argument and not a zero).

keyfind returns false if the key was not found, otherwise it returns the tuple




In Python, to find all keys with the value 1 we do:
[k for k, v in kwargs.items() if v == 1]


In Erlang we do:
lists:filter(fun({Key, Value}) -> Value == 1 end, Args).

and just in case you thought that
lists:keyfind(1, 2, Args).
would find all tuples with the second element equal to 1, it only returns the first tuple that it finds.










   


Tuesday, January 3, 2012

find command syntax

The find command is very powerful, but sometimes it trips me up when I forget to escape parenthesis, or add space inside parenthesis or forget to quote paths which contain an asterisk.

Here are some of the cases that get me most often:

 
# the -path option expects the full path:
find . -path "py26/*" -print     #does not work
find . -path "./py26/*" -print   #works !

# find files in either directory 
find . -path "./py26/*" -o -path "./.git/*" -print

# same as the previous example, but using parenthesis '()'
find . \( -path  "./py26/*" -o -path "./.git/*" \) -print

# the above command won't work if we leave out the spaces after the first parenthesis and before the last one.
find . \(-path  "./py26/*" -o -path "./.git/*"\) -print

# find all files except for files in py26 or .git
find . \( -path  "./py26/*" -o -path "./.git/*" \) -prune -o -print


Note, you don't need to add the -print at the end of the command (it is used by default, but shown here for clarity)