Google Calculator via Python

April 7, 2009 at 7:47 pm 5 comments

(Wow, a month? Sorry, guys.)

Randall Munroe of xkcd (fantastic comic by the way) requested a command-line interface to Google Calculator (which is a fantastic tool if you don’t use it), and I delivered.

An example of Google Calculator:
(radius of Earth * 2 * pi) / the speed of light = 0.133675091 seconds

Of course, nobody wants to fire up a big, slow web browser just to do some calculations. The solution, thanks to my modest Python skillz:

The script will, by default, loop and repeatedly ask for input:
> 2+2
2 + 2 = 4
> 17 meters in microparsecs
17 meters = 5.50932003E-10 microParsecs

You can also ask it to evaluate a single input string:
C:\Python26\googcalc> "17 meters in microparsecs"
17 meters = 5.50932003E-10 microParsecs


It handles bad strings nicely:
C:\Python26\googcalc> "does not compute"
Google Calculator results not found.


4/8/09: I’ve created a few versions:
Original: (takes an argument string) 441441
Interactive: (repeatedly loops, asking for input) 441428
Hybrid: (takes an argument, defaults to loop if none given) 441432 (recommended)

Randall now has this aliased to “c” for easy access; this is effectively the xkcd Seal of Approval :)


Entry filed under: Uncategorized. Tags: , , , , .

Smiley physics update! “Hooke’s Revenge” ping-pong ball gun

5 Comments Add your own

  • 1. Randall Munroe  |  April 9, 2009 at 8:36 am

    In practical use, I just ran into a big problem — there’s no entry buffer in Python, so you can’t hit “up” to scroll through previous things — and you can’t go back and edit what you’re tying with “left”.

    I’m googling a bit, but I don’t know how to make that kind of thing.

  • 2. Randall Munroe  |  April 9, 2009 at 9:18 am

    Cool, fixed by launching it with ‘rlwrap’

  • 3. Randall Munroe  |  April 9, 2009 at 9:31 am

    Actually, never mind that — instead, just add

    import readline

    to the top of the script, so it uses the python readline stuff and gives you a proper buffer. Now it’s everything I want

    You might also wanna make ctrl-C (and commands like “quit” and “exit” and Esc) close the program gracefully.

    Oh, and to those who say ‘why not just use units’, I challenge them to do any of these in units:

    sqrt(2/(pi*18000/(mi^2)*80/year*30 minutes)

    2*pi*6 meters/(day*arcsec(6 meters/(radius of earth)+1))

    12*arcsec(15+5 days/1 day)+15 amperes/5 amperes

    None work in units, for various reasons, and all work in Google Calculator.

  • 4. Thomas Rinsma  |  April 21, 2009 at 12:27 pm

    Let’s beg Google to open-source their code for this :D

  • 5. Griff  |  July 27, 2009 at 5:45 am

    Save yourself some bandwidth and add &num=0 to the arguments so that you don’t bother grabbing search results.

    Also, for the sake of being used as a calculator, it wouldn’t be too difficult to add some sort of answer referencing. (ans*2 = previous result times 2, etc).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

Human Souls

wordpress visitor counter souls and counting...

%d bloggers like this: