Skip to main content


Showing posts from June, 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.

Use Queues to communicate between main thread and sub-threadUse wm_protocol/protocol to handle quit eventUse Event to pass a message to sub-threadimport 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(): q.put(time.strftime('%H:%M:%S')) 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_thread.start() self.poll_interval = 250 self.poll() self.root.wm_protocol("WM_DELETE…

Calling a function periodically in Tkinter (Polling)

Use the after 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') self.poll() def poll(self): print time.strftime('%H:%M:%S'), time.strftime('%H:%M:%S') + '\n') self.root.after(1000, self.poll) app = App() app.root.mainloop() 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.cancel.pack(side='left') self._poll_job_id = self…

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() app.root.mainloop()

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.

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 (Change c:\windows to where ever your windows install is.

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.

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 …

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 …

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 make install Make sure this directory is in your path such that you can call python.
#Add this path to .bash_profile PATH=$HOME/local/Python/bin:$PATH Python has a very neat, principled, way of storing its infrastructure (modules) that is detailed here. A quick way to …

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 installWhat I really like is the ability to patch into a running Ipython kernel and run code from inside the IDEI 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 wit…