I’m not sure whether I’ve just reinvented the wheel, but … a new way of building command-line interface in Python!
Well, it probably won’t work for everything. Where it works at it’s best is when you have a number of modules, which meant to do something, and you don’t want to build the CLI interface to each of them. Say, in my case — I have 5 modules for the running various machine-learning tasks, which act as wrappers around various Java applications (written by me and third-party). Each module has a number of functions, which have something in common — they take input file(s), process it and write output to another file.
Now, let’s get to the code.
tagger.py - performs feature extraction and invokation of Mallet tagger
Actual code is not included — just methods’ names.
import os
import sysfrom common import *def extractFeaturesForTagger(filename, output, jrunner = None):
...def invokeTagger(featuresFile, modelFile, train = True, output = None, jrunner = None):
...def despatch(args):
if len(args) < 3:
return False cmd = args[2]
if cmd == 'fe':
try:
(ifile, ofile) = args[3:]
except:
return False extractFeaturesForTagger(ifile, ofile)
elif cmd == 'train':
try:
(ifile, ofile) = args[3:]
except:
return False invokeTagger(ifile, ofile)
elif cmd == 'tag':
try:
(fts, model, ofile) = args[3:]
except:
return False invokeTagger(fts, model, False, ofile)
else:
print("Couldn't recognise the options: ")
print(args)
return False return True
def describe():
return ("tagger", [
("fe", "[I]uafFile, [O]featureVectorsFile", "Extract features for tagger"),
("train", "[I]featureVectorsFile, [O]modelFile", "Train the model"),
("tag", "[I]featureVectorsFile, [I]modelFile, [O]taggerOutputFile", "Tag locations in file")
])
What matters is two functions - despatch and describe. First one actually takes standard sys.argv, assumes that it applies to current module and tries to apply the arguments to it’s functions. When it works, it invokes an appropriate method. When it’s not, it returns False.
Second method returns a tuple of string and list, which, in it’s turn consists of tuples of three elements — subcommand name, parameters and short description. Now when we have this module, let’s get to the despatcher.py:
#!/usr/bin/env python
# encoding: utf-8
"""
despatcher.pyCreated by Roman Kirillov on 2009-09-25.
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
"""import sys
import osimport tagger
import disambiguatormodules = [tagger,disambiguator]def printHelp():
helpString = '\nUsage options: %s <module> <command> <command-specific-arguments>\n' % (sys.argv[0])
for m in modules:
(cmd, subcmds) = m.describe() helpString = helpString + '\n' + cmd + '\n' for scd in subcmds:
(s, params, descr) = scd
helpString = helpString + (s + "\t").rjust(15) # + params + " -- " + descr + "\n"
helpString = helpString + params + " -- " + descr + "\n" print(helpString)def canIHaz(val):
if not val:
printHelp()
sys.exit(3)def despatch():
if len(sys.argv) < 2 or sys.argv[1] == 'help':
printHelp()
sys.exit(0)
else:
for m in modules:
(cmd, subcmds) = m.describe()
if cmd == sys.argv[1]:
canIHaz(m.despatch(sys.argv))
if __name__ == '__main__':
despatch()
While it’s pretty easy to anyone who can read Python to understand, what’s this all about, let’s just show what happens when you run despatcher.py without parameters:
[kirillov@chiark integration]$ python despatcher.pyUsage options: despatcher.py <module> <command> <command-specific-arguments>tagger
fe [I]uafFile, [O]featureVectorsFile -- Extract features for tagger
train [I]featureVectorsFile, [O]modelFile -- Train the model
tag [I]featureVectorsFile, [I]modelFile, [O]taggerOutputFile -- Tag locations in filedisambiguator
fe [I]uafFile, [O]outputFileName, [I]useTaggedLocations (true/false) -- Invoke disambiguation feature extractor
import [I]featureVector, [O]malletImport -- Import feature vector to Mallet format
train [I]featureVector, [O]modelName -- Train disambiguators model
disambig [I]featureVector, [I]modelName, [O]output -- Run trained disambiguator over the feature vector
normalise [I]featureVector, [O]normalisedFeatureVector -- Normalises the feature vector
Pretty easy, huh? Of course, still needs a lot of tuning and polishing, but it feels so much better to have a centralised dispatcher, which takes care of printing help and handling options — rather than having a one for each module?
Posted via web from Roman’s blog | Comment »
What if you’re away from any wifi or desk phone with a fancy “All World Unlimited” price plan (i.e. your work phone), and you still need to make that long international call urgently? Apple says there’s nothing you can do about it and you must use your provider’s international tarif. Independent developers who don’t bother putting their app to AppStore disagree. 3G is just another source of the Internet, and if Steve, O2, AT&T & co doesn’t want youto use it the way you like - worse for them. Because tiny app Siphone (apparently, SIP + telephone) works nicely via your 3G connection, and all you need is a SIP account with some money on it (I use voipcheap.com for that backup purpose - they’re crap, but none of them is any better). This morning I had a call with my wife (who’s meeting with some of her clients in Moscow) and chatted for 15 minutes; guess how much should I pay if I use O2’s international tarif? Anyway, Siphone is a little gem and if you have your iPhone jailbroken, you have absolutely no reasons not to install it. Do it. Now.

Roman Kirillov
http://sigizmund.com
Totally completely utterly amazingly breathtakingly awesome! Proud to work for Y!
Hi guys, First - thanks again for a nice service, themes are really cool and I’m really going to do some heavy-weight customisation over the weekend! Now, two questions: 1. Is there any way to hide past of the post behind the “read more” link? Like in LiveJournal or ” in WordPress? That may definitely help to make Posterous more solid and professional blogging tool. 2. Is there any way to convert tags into twitter hashtags? So, if I tag this post (you see - I’m CCing post@posterous.com!) with “posterous” tag, can it be converted into #posterous on Twitter? 3. (oi! there’re three questions actually!) – can we have an option of sending content which will be processed by Markdown OR at least sending HTML source? Say, I want to include snippets of code into my post, so I’d prefer to have `int a = 128` or at least
int a = 128; feature. Thanks again! Cheers, Roman
Do you like “Edit in TextMate” command you can use in any Cocoa edit window, and write the text in your favourite text editor? (of course, TextMate that is). I like it too, and since I’ve switched to Firefox (after 1.5 years struggling with Safari and getting finally tired of it being SO slow sometimes!) I missed it a lot. Well, now you can do it, too – just follow the advice here: http://blog.circlesixdesign.com/2007/02/23/textmate-firefox/ Little tip: there’s no need to create any symbolic link, you can simply specify `/usr/local/bin/mate` as your editor, or, if you didn’t install `mate` command when installing TextMate (which I highly recommend to do), you can use `/Applications/TextMate.app/Content/MacOS/TextMate` to achieve nearly identical results.
<p>Mesmerising Flash Application from Roman Kirillov on Vimeo.</p>
Try it yourself, really.
Well, it can be annoying - it can be awfully annoying, in fact, to debug Hadoop applications. But sometimes you need it, because logging doesn’t show anything, and you’ve tried anything but still cannot get under the Hadoop’s cover. In this case, do few simple steps.
So, you would really like to now, how much longer can you use your inclusive minutes and text, or want to see, when your next bill is due or how much did you pay the last time? There’s an app for that! Recently released My O2 app is a tiny brilliant - does what it says, easily and simply – no more, and no less.

Main screen simply shows you the most important information – well, for me it’s not that bad, as the last bill was just yesterday and I am unlikely to put the needle to the left-most position. But, I can easily see what I want to, and that’s pretty much enough. You can also view your last bill summary, your recent charges (I found out how much one minute costs me when I’m in Spain!) – and even view your tariff details, which is something totally amazing if you need just a quick reference, on what you’re paying. Honestly, O2 - you did a great job and my congratulations are well-deserved!




See and download the full gallery on posterous
Grab it while it’s hot! (via O2 Blog)






I bet you’re surprised I do not look as smart as the rest of the guests (and not even nearly as smart as my beautiful wife). Well, that’s because I’ve been the most miserable person there – namely photographer, and spend the whole day jumping around with the camera (with two cameras, in fact), so at 12AM when guests were enjoying the Spanish wine and truly delicious food from the hotel’s restaurant, I fought myself to not fall asleep right where I was sitting.


