Thursday, November 12, 2009

From the lessons of peers


FUBARLabs has twice a month a Python study group, where we teach other various tricks of the trade in Python and cover many of the all-too-cool Python modules out there. All skill levels are welcome, but those of us that regularly attend tend to be regular code monkeys. Here's some of the things we've covered (so you don't think I've been lazy - ha!):


Optparse

Actually built into Python (no install required), optparse allows you to easily create a terminal menu for passing information to your script program, and allowing the user to easily see how to use it.

A quick test program, that only prints out what is passed to it (I used it to test whether or not a number is passed as a string. It is.)
#! /usr/bin/python

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-t", "--test", dest="testvalue", help="the test value that will be printed.")

(options, args) = parser.parse_args()

print parser.values.testvalue

Rick, the member who did a presentation on this (and many of the tidbits I'm sharing with you) has another example located on our forums.


Python-Twitter

There is some fascination with giving the ability to tweet to random pieces of hardware and software. Anyway, here's a way to do it easily with python. By installing python-twitter (sudo apt-get install python-twitter for me), we had an example program up and running in about two minutes time... example code like this:


#! /usr/bin/python

import twitter

def createApp(username, password):
    twitterSystem = twitter.Api(username, password)
    return twitterSystem

user = raw_input("Enter your username - ")
pword = raw_input("Enter your password - ")

message = raw_input("Enter your message - ")

twitterSystem = createApp(user, pword)
twitterSystem.PostUpdate(message)


Simply chmod +x that and you're good to go. Otherwise just run it as a normal python script. You're limited to 100 tweets an hour, and you have complete access to your friends list and a lot of other cool tidbits.


PyFacebook and Django

It is kind of odd - both of these are practically dependent on one another. The presentation for this was tonight, and in fifteen minutes we had a working Facebook application. The applications are apparently hosted by the application writer, giving them (us) access to way more information than we thought. There has been a recent update that blocks us to some of that information, but application writers still can grab a lot about you. I'm glad I've since deleted my facebook, but I wonder if it's a bit too late.

This is something that will deserve it's own blogpost down the road, so I'll just leave this at "we did it". Not impressive, but it worked.

And if you are a web developer (or interested in web development), I highly recommend you check out Django.


Google API

Apparently Google allows you to talk to their cloud office suite and create, modify, and read their documents via a Python module (sudo apt-get install python-gdata). Why is this incredibly cool? Imagine a robot gathering sensor data. Now imagine that robot reporting it back not in a terminal, not in a CSV file, but live, through Google Spreadsheet (so hundreds can view it simultaneously and it can share with varying permissions with ease) and generates a chart for its data so us humans can quickly understand what it's seeing.

Yah - very cool.

In fact, much of Google's services have Python wrappers. I did not know that until recently, and will be keeping an eye out on how to make this useful to my projects.

And so ends a very quick update used to try and kill some midweek insomnia. Coming soon - BeagleBoard information!

No comments:

Post a Comment