Yak Shaving

just me

A Quick memcache demo for Python

without comments

So you need to use memcache with Python? Below is a brief intro I figured out in about 20 mins.

First install everything you need. I run Ubuntu so python-memcached memcached packages were required.

Next start the memcached server if its not already running:

/usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1

The above gets you a 64Mb server, more than enough to play on.

Next some python below is from the interpreter as I was in a hurry:

import memcache
memc = memcache.Client(['127.0.0.1:11211'])
class test():
    def __init__(self):
        self.m = "Hello, world"

t = test()
memc.set('cheese', t, 120)
True
r = memc.get('cheese')
r.m
'Hello, world'

The above instantiates an object and then saves it memcached with set() and then we get it back using get(). Dead simple. The usage is pretty, try and fetch from memcached if if fails fetch from your datasource and then save that document ready for next time.

memc.set(‘cheese’, t, 120) cheese is the reference, t is the object to store and 120 is the time to live. After 120 seconds the object is cleared from the cache.

Written by channam

June 21st, 2009 at 10:47 pm

Posted in Uncategorized

WebDriver for logging into Twitter

without comments

No real reason for choosing Twitter apart from its cool :) .

The code below uses unittest to run. It creates a new WebDriver object and users it to fetch http://twitter.com and submit a username and password. Once the details are added it “clicks” the “Sign In” button to login into Twitter.

#!/usr/bin/env python

import unittest
import logging
from webdriver_firefox.webdriver import FirefoxLauncher
from webdriver_firefox.webdriver import WebDriver

class TwitterTests (unittest.TestCase):

    def test_login_twitter(self):
        driver = WebDriver()
        driver.get("http://twitter.com")

        # find our elements - the html on the page with same ids
        username_element = driver.find_element_by_id('username')
        password_element = driver.find_element_by_id('password')

        # use this to toggle the remember me box
        remember_me_element = driver.find_element_by_id('remember')

        # type into the boxes
        username_element.send_keys('yourusername')
        password_element.send_keys('yourpassword')
        remember_me_element.toggle()

        # click Sign In and we should be logged in
        driver.find_element_by_id('signin_submit').click()

       # check that the title of the page is correct to see if we logged in
        self.assertEqual(driver.get_title(), 'Twitter / Home')

        # Extract from the html using xpath to find username and updates of the people on the screen
        updates = driver.find_elements_by_xpath("//span[@class='entry-content']");
        user = driver.find_elements_by_xpath("//a[@class='screen-name']");

        # display in the terminal the name and the update
        for i,update in enumerate(updates):
            print user[i].get_text() + ": " +update.get_text()

        # uncomment the following to close the window and finish
        #driver.quit()

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    unittest.main()

The docs are not great for WebDriver but reading the source is pretty simple. Being able to mentally parse Java to Python is also a big advantage!

Written by channam

June 20th, 2009 at 1:05 pm

Posted in webdriver

Webdriver and Python Bindings

without comments

I`m working on some WebDriver stuff and below is quick guide to get you started:

svn checkout http://webdriver.googlecode.com/svn/trunk/ webdriver
cd webdriver/
sudo python setup.py install

add the following to make sure you new libs can be found correctly

vi ~/.bashrc
export WEBDRIVER=/home/channam/Code/python/webdriver
. ~/.bashrc

and you are good to go.

Written by channam

June 20th, 2009 at 12:51 pm

Posted in Uncategorized

Odd Google App Engine Issue

without comments

I was having issues getting a url with urlfetch.fetch(url), it kept failing with:

[snip]
  File "/home/channam/Code/python/google_appengine/google/appengine/api/urlfetch.py", line 241, in fetch
    return rpc.get_result(allow_truncated)
  File "/home/channam/Code/python/google_appengine/google/appengine/api/urlfetch.py", line 388, in get_result
    self.check_success(allow_truncated)
  File "/home/channam/Code/python/google_appengine/google/appengine/api/urlfetch.py", line 356, in check_success
    raise DownloadError(str(e))
DownloadError: ApplicationError: 2

A little bit of poking found that the issue was caused by having a space in the url, something which I’m fairly certain was ok on early versions of GAE. Oh well you live and learn.

Written by channam

May 30th, 2009 at 8:44 pm

Posted in App Engine, python

Tagged with

Laconica Fix

without comments

Thought I would have a go at making a timesheet based twitter thing using Laconica. All went well until I tried to run after install. All my links were screwed as they were prefixed with index.ph. Bit of googling found this solution http://laconi.ca/trac/ticket/1345. Strangly the ticket is closed but I`m seeing the same issue but the fix works:

Add $config['site']['path'] = “”; to your config.php file. Hope the fun bit goes a little bit easier than the install.

Written by channam

May 30th, 2009 at 7:15 pm

Posted in Uncategorized

Doing a Presentation

without comments

First I’d like to make clear I have nothing but respect for anyone who does a presentation. Especially those who speak for free, just for the love of sharing their passion. I personally have only given small talks at work apart from ending up on stage at LUG Radio.

I have watched a quite a few presentations from lightning talks, 20:20 talks and Google developers. 20:20 are quite novel, 20 slides and 20 seconds on each. This makes for an interesting overview ideal for loud or time strapped meetings.

There are a few common mistakes that seem to keep coming up. So here are a few tips for anyone doing a talk from someone who never does it :)

  • Don’t read your slides! People are quite capable of reading them.
  • Prepare, don’t read from a script. If you must have some notes use small cards and don’t read directly from them.
  • Time yourself before hand. Bear in mind you might speak quicker live. Don’t over run especially if you are unlucky enough to be before lunch.
  • Try to leave questions to the end so you can maintain your flow.
  • Look at your crowd, people like to feel wanted.
  • Make sure your laptop works with the project in advance, amazing the number people who waste 10 minutes getting setup
  • TALK LOUD! People at the back need to hear too. Also it will stop people talking amongst themselves.
  • Don’t show lines of code, by all means show a snippet, for example how few lines are needed. Instead show the code working – if possible. You can provide links at the end for people to go and look at your example code later when they will be able to take it in.
  • Demonstrations are cool and are a chance to show what you can do with your chosen topic. MARE SURE IT WORKS! Under no circumstances make any chances the night before to the code for your demo, its only going to end badly.
  • Have a sense of humor. Being able to laugh at yourself will get you out of most problems that might occur.
  • If anyone gets lippy, rambles on for a question or keeps interrupting ask them to talk to you afterwards over a coffee or in the bar. As much as you might not want to talk to someone annoying it can be rewarding. If noone ever questioned things noone would ever learn, so treat it as a learning experience.

Written by channam

May 28th, 2009 at 7:53 pm

Posted in presentations

Tagged with

I love Python

with one comment

Compare two lists?

list(set(b).difference(set(a)))

done

Written by channam

May 21st, 2009 at 10:32 pm

Posted in Uncategorized

Tagged with

Datastore in App Engine

without comments

http://localhost:8080/_ah/admin/datastore

Written by channam

May 4th, 2009 at 3:43 pm

Posted in Uncategorized

Python 2.6 or Google App Engine FAIL

without comments

Just installed Ubuntu 9.04 and tried to fire up Google App Engine. Some of it worked but I got the following error:

: No module named _multiprocessing
      args = ('No module named _multiprocessing',)
      message = 'No module named _multiprocessing'

A little googling found the issue: GAE Issue. Long story short Python 2.6 is currently not supported, please use 2.5.

Written by channam

April 29th, 2009 at 8:20 pm

Posted in Uncategorized

New Job

without comments

Guess I should have mentioned it earlier: I can haz new job!
I am currently happier than a pig in muck. Still bedding in but I have a Ubuntu desktop and 2 HUGE monitors! Best of all its Python. I have made the change from hobbyist to professional!

Written by channam

March 16th, 2009 at 8:48 pm

Posted in Uncategorized