Thursday, June 27, 2013

Oracle's VirtualBox in Mac OS X: "VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE)" error

Problem: VirtualBox VM does not start after updating VirtualBox. Says "VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE)".

Solution: From here: For some reason the permissions under /Applications/ is changed to world writable. You can fix the permission by running Disc Utility and Repair Permissions. This fixes this problem.

Sunday, June 23, 2013

exiftool batch mode

exiftool has a batch mode. If you pass the argument -stay_open True, exiftool accepts multiple commands. This is invaluable if you call exiftool from another program because you avoid the overhead of loading/unloading the program everytime. exiftool can also return data formatted as JSON, which python knows how to handle, allowing us to pass formatted data back and forth rather easily. An example of this all working together nicely is here.

Running a task in a separate thread in a Tkinter app.

  1. Use Queues to communicate between main thread and sub-thread
  2. Use wm_protocol/protocol to handle quit event
  3. Use Event to pass a message to sub-thread

import Tkinter as tki, threading, Queue, time

def thread(q, stop_event):
  """q is a Queue object, stop_event is an Event.
  stop_event from
  while(not stop_event.is_set()):
    if q.empty():

class App(object):

  def __init__(self):
    self.root = tki.Tk() = tki.Text(self.root, undo=True, width=10, height=1)'left')

    self.queue = Queue.Queue(maxsize=1)
    self.poll_thread_stop_event = threading.Event()
    self.poll_thread = threading.Thread(target=thread, name='Thread', args=(self.queue,self.poll_thread_stop_event))

    self.poll_interval = 250

    self.root.wm_protocol("WM_DELETE_WINDOW", self.cleanup_on_exit)

  def cleanup_on_exit(self):
    """Needed to shutdown the polling thread."""
    print 'Window closed. Cleaning up and quitting'
    self.root.quit() #Allow the rest of the quit process to continue

  def poll(self):
    if self.queue.qsize():
      self.selected_files = self.queue.get(),tki.END), self.selected_files)
    self._poll_job_id = self.root.after(self.poll_interval, self.poll)

app = App()

Saturday, June 22, 2013

Calling a function periodically in Tkinter (Polling)

Use the

method. From a discussion here.

import Tkinter as tki, time

class App(object):

  def __init__(self):
    self.root = tki.Tk() = tki.Text(self.root, undo=True, width=20, height=5)'top', expand=True, fill='both')

  def poll(self):
    print time.strftime('%H:%M:%S'), time.strftime('%H:%M:%S') + '\n')
    self.root.after(1000, self.poll)

app = App()

From a further discussion here, use the after_cancel method to cancel the polling. after returns a job id that you need for the cancellation

import Tkinter as tki, time

class App(object):

  def __init__(self):
    self.root = tki.Tk() = tki.Text(self.root, undo=True, width=10, height=1)'left')
    self.cancel = tki.Button(self.root,text='Stop',command=self.stop)
    self._poll_job_id = self.poll()

  def poll(self):
    print time.strftime('%H:%M:%S'),tki.END), time.strftime('%H:%M:%S') + '\n')
    self._poll_job_id = self.root.after(1000, self.poll)

  def stop(self):

  def go(self):
    self._poll_job_id = self.poll()

app = App()

Text edit box with scroll bars in Tkinter

Tkinter supplies us a widget (Text) that is very powerful. However, it lacks scroll bars. While you could bundle a scroll bar and hook up events yourself, you could also save yourself some time and use ScrolledText. In Python3 this is available as part of Tkinter, for earlier versions you have to import ScrolledText. (From the discussion here) Thus:
import Tkinter as tki
from ScrolledText import ScrolledText #ScrolledText = tki.scrolledtext for Python 3

class App(object):

  def __init__(self):
    self.root = tki.Tk()

    # create a Text widget with a Scrollbar attached
    #Courtesy Honest Abe (
    self.query_win = ScrolledText(self.root, undo=True, width=50, height=5)
    self.query_win.pack(side='top', expand=True, fill='both')

app = App()

Embedding an IPython shell in the middle of python code

For a long time I had been looking for a satisfactory way to export a variable from some python code to the main python workspace. This stackoverflow thread sort of got at it, but not really. I love pdb, especially, however, sometimes you just need the full capability of IPython, including object introspection.

Well, there is an amazing solution. In the place in the code where you would do
import pdb;pdb.set_trace()
, if you do instead
from IPython import embed; embed()
, your code will pause at that place and you will get a fully functional IPython shell. When you exit from that shell you code will continue as usual.

Friday, June 21, 2013

Collapse a python list that contains lists within

Use itertools
import itertools
a = [[1,2],[2],[3,4,5,6],[7],[8,9],[10]]

Thursday, June 20, 2013

Win 8 boot menu

Problem: I installed windows 8 on a computer that previously had win xp. Now, when I boot up the computer I get a text screen with a bunch of OS options with my old windows (Win XP) as the default. I have to select Windows 8 manually to boot. I wanted to get rid of this/or make win 8 the default option Solution: (From here) I ran, from an administrative prompt (WINDOWS KEY + x + a)
bcdboot C:\Windows
to where ever your windows install is.

Tuesday, June 18, 2013

Applescript to show selected file in finder

tell application "Finder" to reveal alias "Users:kghose:Path:To:My:file.txt"
This will select the given file in finder (opening a finder window if needed)

Saturday, June 15, 2013

Using Apple Script to get list of files selected in finder

set theFiles to {}
repeat with itemAlias in (get selection of application "Finder")
   set end of theFiles to POSIX path of (itemAlias as text)
end repeat

Wednesday, June 12, 2013

Finding if a python object is a string, either unicode or not.

Problem: you want to test to see if an object is a string or not. However your input could either be Python's 'str' type or the 'unicode' type

Solution: Use basestring
 e.g.: isinstance(o, basestring)

Tuesday, June 11, 2013

More funding for the FDA, less funding for the NSA

I don't have a complicated thesis here. I simply think, in the long run, we will end up with a much better society if we don't end up like the Soviets or the East Germans or a multitude of other organizations which had people spying on each other, sapping the vitality of the country socially and therefore economically.

We are much better served pouring money into an organization like the FDA which truly acts on behalf of us the people in monitoring practices that affect us and our children directly and daily - our food and medicines.

We need the FDA to protect us from the carelessness and callousness of companies and individuals who put our lives at clear and present danger by contaminating our food and medicine supply.

Talk to your representatives. Encourage them to spend your money on things that are useful and productive.

Saturday, June 8, 2013

Simple sketching using a track pad.

I like to illustrate text with sketches. I think there are many instances when it is easier and quicker to convey ideas. I used to think that I need a graphics tablet for this. Previously I would sketch with pen and paper and then scan the drawing. This added an extra step, was a motivation barrier and was not as flexible has doing things digitally (where you can delete/edit stuff without having to start all over again). I tried a bit with the mouse, but found it very unwieldy.

Recently, however, I have discovered that for the simple line sketches I do a track pad works surprisingly better than I expected. I used Inkscape and the laptop track pad to do the sketch below. I can totally see how a real sketch artist who is interested in effectively using line weight/thickness to convey information will find this method unimpressive, and in the end I do envision buying a graphics tablet, but for now I will keep practicing with the track pad.

I think the track pad works well for me because it is a bit like finger painting and I can control my finger tip more finely than I can control a whole mouse, which has inertia and makes it difficult for me to do fast movements.

ipython notebook

I'm, of course, a Python groupie. Many of you perhaps use Matlab or Mathematica as an interactive environment to explore math (or algebra). Python has had the regular python shell and then Ipython, which allows such easy interactive exploration.

However, when using the python shell, I always feel the need to open up a separate editor and write scripts when the concepts get a little complicated, and this takes away a little of the interactiveness.

Ipython notebook changes all that. You should really try it out. The Ipython notebook uses your browser as a GUI (something I heartily support). It breaks everything up into 'cells'. Some cells can be text and some can be code. Each code cell can be executed on its own but as part of a common workspace.

This is ideal for me as I write this blog: I type in some code and execute it to see results (including graphs). Then I type up some text explaining the what I just did. I then move on to another cell for the next part. I can go back to an earlier cell and change code and then rerun everything, or just a part of it. It's like one giant interactive script. I love it.

There still is not a polished way to convert the notebook to say a wordpress post (though there is a pipeline for blogger). This script works for me to convert the figures into .png format. I simply copy and paste by code and text into this blog from the notebook and then attach the figure .png files. Janky and increases the time spent, but whatever.

Setting up Python/Pylab environment on machine with no root access

Often you will get the opportunity to run your code on a cluster, or even simply a guest computer, where you don't have root access (sudo does not work). The great thing about Python is that Python and any modules can be completely installed in user space (in your account) without disturbing any one else. In the computer I am accessing (Partners' HPC setup) they are hip and have all versions of Python. In case your setup doesn't you first want to do:
cd /tmp #(We have write access here)
curl -O #Or whatever the version you want
tar -xzf Python-2.7.5.tar.bz2 
cd Python-2.7.5
./configure --prefix=$HOME/local/Python #Or where ever you want it
make install
Make sure this directory is in your path such that you can call python.
#Add this path to .bash_profile
Python has a very neat, principled, way of storing its infrastructure (modules) that is detailed here. A quick way to figure out where this is is to do
python -m site --user-site
We need to add this to our path too. In my case is was ~/.local/bin
#Add this path to .bash_profile
Next you will want pip, which is a more modern package manager than easy_install, but first you'll want setup tools:
wget -O - | python

cd /tmp #(We have write access here)
curl -O
tar -xzf pip-1.3.tar.gz
cd pip-1.3/
python install --user
Python informs us pip is now installed under
. The magic from now on is to simply add --user to our pip install commands to install packages in our userspace. Let's try this out:
pip install matplotlib --user
pip install ipython --user
pip install --upgrade nose --user #(The server had an older version of nose)
And we have our environment. The HPC cluster I went into already had numpy and scipy, but you may need to install those as well.

Wednesday, June 5, 2013

Spyder (A really nice python IDE)

I've started using Spyder recently and I really like it.

The features I enjoy the most are
  • Quick response. PyCharm is great but has a tendency to freeze up on me. It' my fault - I have a lot of applications open at the same time and I switch between them. PyCharm has a lot of useful stuff going on beneath the hood, so there are many processes that swap in and out when I switch, but I relish the speed of Spyder now.
  • There is a Mac .dmg which I can just download and install
  • What I really like is the ability to patch into a running Ipython kernel and run code from inside the IDE
  • I can just fire up a new file and code right away without having to create a project etc etc. But I can still make files parts of projects if I want.
The things I miss (and PyCharm does really well):
  • There is no git integration (and the creators say they won't do it). I completely understand this decision and it's not a problem, but PyCharm has the git integration down SO well, it's hard to do without. 
  • PyCharm will analyze your code to the extent that it will give you hints to keyword arguments for your functions. I LOVE this. I NEED this. Spyder will display the function help, which is OK.
  • PyCharm will auto complete functions defined in the current context. I have come to rely on this to speed up my coding. Spyder makes you type out the whole function name (though it will do dot completion)
  •  PyCharm will analyze imports from your libraries and auto complete on that, Spyder does not.
Spyder is also rough around the edges - the user interface will freeze sometimes in unpredictable ways, so some work is needed there.

Accessing reject list on Samsung feature phones

Menu->Settings->Application->Call->All Calls->Auto reject

Tuesday, June 4, 2013