Baker – Expose Python to the Shell

It's been a long time since my last post, and it would be appropriate that I post about whatever it is that I've been working on. But I won't. I'm writing this post only to tell you about an interesting new python library I stumbled upon.

Baker, in their own words, "lets you easily add a command line interface".

In other words, it lets you expose your python utility functions to your favorite shell.

The only requirements are that:

  • Your function must accept string arguments (an exception: it accepts ints/floats if you provide a default argument)
  • Your function must print its output to stdout

Okay, so these are a little limiting. But the interesting part about Baker is not its implementation (which is still a bit clunky and basic), but rather its concept. Here's a small piece of code I wrote:

import baker

@baker.command
def substr(text, start, end, step=1):
    print text[int(start):int(end):step]

if __name__ == '__main__':
    baker.run()

And here's how I use it from the command line:

> baketest.py substr "Hello World!" 1 10
ello Worl

> baketest.py substr "Hello World!" 1 10 --step 2
el ol

The simplicity and intuitiveness of this interface really appealed to me. Hopefully this will catch on, and we'll see more python scripts providing command-line interface, just because it's very easy.

Tags: , , , ,

Categorised in:

1 Comment

  • lorg says:

    There was a similar idea named pyopt in the pyreddit not too long ago by a friend of mine.
    See http://code.google.com/p/pyopt/ .

    While cute, didn't yet get to use any of those. The last command line utility I wrote used fabric. (which arguably has a worse way to expose interfaces, but has a bit more functionality than just command line).

Leave a Reply to Felipe Cruz Cancel reply

Your email address will not be published. Required fields are marked *