goldb.org home

AS OF MAY 2008, THIS BLOG IS NO LONGER BEING UPDATED.
Visit the new blog at: http://coreygoldberg.blogspot.com



 Sunday, March 02, 2008

Python - Palindrome Checker

A palindrome is a sequence that reads the same in either direction.

Here is function I wrote to check if a phrase is a palindrome:


import re

def is_palindrome(txt):
    txt = re.sub('\W+', '', txt).lower()
    return txt == txt[::-1]



phrase = "Go hang a salami, I'm a lasagna hog"
print is_palindrome(phrase)

>> True
#    Comments [4] |
 Monday, February 25, 2008

Geolocation Mashup To Visualize My Blog Visitors

I just did a Geolocation mashup to visualize where my blog readers are coming from on a particular day. Below is a visualization of my US/Canada visitors from February 14, 2008.

All of the data is based off of IP addresses from my referrer logs.

To see how to do this, check out: http://www.goldb.org/geo_maps

#    Comments [0] |
 Thursday, February 21, 2008

Novell == Suckers?

I bet that interoperability deal doesn't sound so great now.
#    Comments [0] |
 Tuesday, February 19, 2008

Etymology of "Foo"

"Foo" is my favorite word. I use it all over the place. When programming, it is the name of my quick junk files... it is a temporary variable name while i figure out code. We programmers love our foo.

For those that want the origins and etymology, there is a great RFC about it:

http://www.ietf.org/rfc/rfc3092.txt


#    Comments [0] |
 Friday, February 15, 2008
 Tuesday, February 12, 2008

Python - 15 Line HTTP Server - Web Interface For Your Tools

I write a lot of command line tools and scripts in Python. Sometimes I need to kick them off remotely. A simple way to do this is to launch a tiny web server that listens for a specific request to start the script.

I add a "WebRequestHandler" class to my script and call it from my main method. There is a "do_something()" method in the class. You call your code from this method.

All you have to do is launch your script and it will sit there and wait for requests. If the request is bad, it spits back a 404 error. If the request path matches what we are looking for (in this case "/foo"), the code is launched.

Now you have an easy way to call your script remotely. Just open a browser and type in the URL: http://your_server/foo, or call it with a tool like 'wget' or 'curl'.


import BaseHTTPServer

class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/foo':
            self.send_response(200)
            self.do_something()
        else: 
            self.send_error(404)
            
    def do_something(self):
        print 'hello world'
        
server = BaseHTTPServer.HTTPServer(('',80), WebRequestHandler)
server.serve_forever()

(this was adapted from a code sample in "Python In A Nutshell" by Alex Martelli)

#    Comments [1] |
 Monday, February 11, 2008

Python - Terminating Threads - Boolean Flag and threading.Event()

In many programming languages you can't terminate a thread directly.  Python is no different.  Rather than termintaing a thread from the code that spawned it, you just a pass a flag to the thread that tells it to terminate itself.  Typically a thread will run in a loop, periodically checking this flag so it knows if it should continue or not.  To terminate the thread from the outside, you just set its flag to die.

I was using this idiom in Python by setting a boolean flag in my spawned thread.

So a simplified thread class would look something like this:


class MyThread(threading.Thread):
    def __init__(self, num):
        threading.Thread.__init__(self)
        self.running = True
        self.num = num
        
    def stop(self):
        self.running = False
        
    def run(self):
        while self.running:
            print 'hello from thread %d' % self.num
            time.sleep(1)

I just read an old post in comp.lang.python that pointed to a recipe in the Python Cookbook that suggests using threading.Event() rather than a simple boolean flag.

So the thread class would look something like this:


class MyThread(threading.Thread):
    def __init__(self, num):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()
        self.num = num
        
    def stop(self):
        self.stop_event.set()
        
    def run(self):
        while not self.stop_event.isSet():
            print 'hello from thread %d' % self.num
            time.sleep(1)

They work exactly the same.

I am just wondering what other flexibility threading.Event() gives you, and if there is anything bad about using simple boolean checks to kill threads. I guess I will have to look it up and play around a bit.

#    Comments [5] |
 Sunday, February 10, 2008

External USB Hard Drive From A Bricked iPod

Here is my bricked iPod. It won't even turn on.  It is time to retire it. However, it has a perfectly good 30 gig hard drive inside.  Gotta be good for something, right?

First I crack it open and get a look at the insides.

The Toshiba 2.5" IDE hard drive sits right near the top.

Next, I bought a 2.5" USB external IDE hard drive enclosure.

I installed the hard drive into the new enclosure, and it's all done. Now I have a nice external storage device.

#    Comments [0] |

Rockin' Python 3000 Alpha (3.0a2)

I just installed the latest Alpha of Python 3000.

So far so good...

#    Comments [0] |
 Friday, February 08, 2008

Test Automation Term: Programmatic Testing - One End Of The Spectrum

Programmatic Testing:  A style of software testing where tests are executed without human interaction.

When I say "this test can be automated", I don't mean a computer can magically replace a sapient process to create a useful test.  All test design and methodology is devised using a sapient processes (obviously).  However, testcase generation, data generation, test execution, results analysis, etc, can often be done without human interaction.  This has been described using terms like: "Automated Testing, "Test Automation", etc.  Among some of the more vocal people in the testing community, these terms are considered confusinginaccurate, harmful, or just a cover up for what is actually a non-scripted test

Automated and Manual Testing are not mutually exclusive.  Rather, they are at either end of a continuum.  A test isn't "automated" or "manual".  It falls somewhere on that continuum.  Each step of the process has some mixture of the elements.

I have also heard the term: "Computer Assisted Testing".  I don't like this term because it tries to place a name on the entire testing process.  I am not referring to the entire process, so this becomes confusing talk to me.  I am referring to a style of test execution.

Therefore, to clear up ambiguity of terminology, I now refer to "Automation" as "Programmatic Testing".  This refers to a style of test execution where tests are executed in a non-interactive manner.

I you have a tool, a test framework, a software library, a program/script, functions for checking conditions, or a mechanism for reporting errors and stats, then you are doing a form of programmatic testing.

#    Comments [1] |
 Wednesday, February 06, 2008

C# .NET 2.0 HTTP GET Class

Sending HTTP Requests from a C# program seems unnecessarily hard.  I wrote a small helper class to deal with sending and timing GET requests:
http://www.goldb.org/httpgetcsharp.html

You use it like this:


public class Program
{
    static void Main(string[] args)
    {
        HTTPGet req = new HTTPGet();
        req.Request("http://www.google.com");
        Console.WriteLine(req.StatusLine);
        Console.WriteLine(req.ResponseTime);
    }
}
#    Comments [2] |
 Tuesday, February 05, 2008

Python - Convert Secs Into Human Readable Time String (HH:MM:SS)

Convert a number of seconds into a human readable time string HH:MM:SS

7046 seconds is: 1 hour 57 mins 26 secs, or 01:57:26

The Function:

def humanize_time(secs):
mins, secs = divmod(secs, 60)
hours, mins = divmod(mins, 60)
return '%02d:%02d:%02d' % (hours, mins, secs)

The Output:

print humanize_time(7046)
>> 01:57:26
#    Comments [5] |
 Saturday, February 02, 2008

wxPython Installer on Windows Vista?

Bug Report To LazyWeb:

Has anyone had success installing wxPython on Windows Vista using the binary installer package?  I get a generic Windows error and the install crashes.  I'm running Python 2.5 and trying to install wxPython 2.8 (wxPython2.8-win32-ansi-2.8.7.1-py25.exe)

I have never tried wx on Vista.  Has anyone else encountered this?


Update 03/05/08: the installer now works fine on Vista

#    Comments [6] |
 Monday, January 28, 2008

Subway Chat With Richard Stallman

I just took the Subway (the "T" here in Boston) home from work and ran into Richard Stallman.  I saw the big RMS beard flowing and I flagged him down.  The next train was running late so we got to chat for a while about GPLv3 adoption and some other Free Software issues.

It's pretty cool to have a conversation with somebody who has influenced my life and ideals so very strongly.

It wasn't the first time I had met him, but it was the longest and most pleasant conversation I have had with him.

Good stuff!


O'Toole's Corollary of Finagle's Law: “The perversity of the Universe tends towards a maximum”



(no i didn't take his freakin picture today :)

#    Comments [0] |
 Tuesday, January 15, 2008

Farewell Old Laptop

Goodbye my trusty Dell 600M:

You served me well for 2+ years in all your single-core glory.  Thousands and thousands of lines of code.. music.. movies.. entertainment.. information.. communication.

Unfortunately your keyboard broke and this is where we part ways.

You have been replaced by a shiny new Dell Vostro 1500 (Core 2 Duo).

#    Comments [0] |
 Monday, January 14, 2008

Boston Snow Pics - Back Bay Buried

Snow day today in Boston...

They declared a Snow Emergency last night and we are getting dumped on as I write this.

Here are some pics of the Boston city plows and salters/sanders getting going:


#    Comments [0] |
 Wednesday, January 02, 2008

New Year's Resolution

Just ordered a new laptop... so my new year's resolution will be:

1680 x 1050 (WSXGA+)

#    Comments [1] |
 Tuesday, December 18, 2007

The Python Papers - Screen Scraping Article

The new issue of the Python Papers is out.  It includes a small article I wrote called: Screen Scraping Web Pages

The issue can be downloaded here:  The Python Papers, Volume 2, Issue 4 (pdf)

This tutorial shows how to programmatically retrieve a stock quote from Google Finance.  It uses Python's high level Web API and screen scraping with regular expressions.
#    Comments [2] |
 Monday, December 17, 2007

Python Experts - Why They Do Python

I was recently interviewed for the article:
Python Experts - Why They Do Python

I don't think I am even close to an "expert", but it was nice being asked to participate.

#    Comments [0] |
 Friday, December 14, 2007

Technical Skills For Performance Testers

A performance engineer is a little bit of a jack-of-all-trades.  Rather than focusing on small technological niche, performance testers must have a very wide range of technical skills to understand the inner working of a complex system under test.

As far as skill go, Scott Barber has said that you need to be a "mid-level everything":

"Become a "Mid-Level Everything" – Developer, DBA, Network Admin, Systems Admin, Architect, Business Analyst, etc."

If you want to become proficient in analyzing system performance and scalability, there are many technical areas you should study.  Here are some skills I have found to be invaluable in my success as a performance engineer:


Performance Concepts:

  • Methodology
  • Load Generation Tools
  • User/Workload Modeling
  • Results Analysis (Latency, Throughput, Metrics)
  • Bottleneck Detection
  • Code Profiling
  • Scalability
  • Concurrency
  • Charting/Graphing
  • Statistics

Operating Systems and Servers:

  • Monitoring (CPU/Network/Mem/Disk/etc)
  • System Tuning
  • Web/Application/Middleware Server Tuning
  • System Administration
  • Virtualization
  • OS Concepts (CPU Scheduling, Memory Management, etc)

Database:

  • SQL
  • Stored Procedures
  • Monitoring
  • Tuning

Network:

  • Topology
  • Monitoring
  • Load Balancing
  • TCP/IP
  • HTTP
  • Packet Sniffing and Protocol Analysis
  • Caching
  • OSI Model

Programming:

  • Proficiency in at least on general programming language. Preferably a dynamic scripting language (Python/Perl/Ruby/etc)
  • Code/Algorithm Analysis


(Note: There are lots of "soft skills" a performance tester would need to be successful. This post focuses only on technical skills)

#    Comments [2] |
 Tuesday, December 11, 2007

Copyright - Music For Music's Sake - Grateful Dead and Woody Guthrie

Sometimes music is treated as art, not as a vehicle for hoarding money and restricting consumers.

This is exemplified by the copyright policies of certain performers.  Here are my favorite policies from some influential acts:

Grateful Dead's Mp3 Policy:

"The Grateful Dead and our managing organizations have long encouraged the purely non-commercial exchange of music taped at our concerts and those of our individual members. That a new medium of distribution has arisen - digital audio files being traded over the Internet - does not change our policy in this regard."

Woody Guthrie's Standard Copyright Notice:

"This song is Copyrighted in U.S., under Seal of Copyright # 154085, for a period of 28 years, and anybody caught singin’ it without our permission, will be mighty good friends of ourn, cause we don’t give a dern. Publish it. Write it. Sing it. Swing to it. Yodel it. We wrote it, that’s all we wanted to do."

#    Comments [1] |
 Friday, December 07, 2007

Lies, Damned Lies, And Statistics

The 3 most important caveats to be aware of when dealing with statistics:

  • Correlation does not imply causation.
  • You can make statistics tell you nearly anything you want.
  • Statistics without proper context are meaningless.
#    Comments [2] |
 Wednesday, December 05, 2007

Open Source Testing - Community Donations Program

Open Source Testing is a great resource that lists most of the open source tools available to testers.  The site is run by Mark Aberdour and has been around since 2003.

Mark's contributions to the testing and open source communities have been very valuable.

Well... he just stepped it up a notch by posting details of his new Community Donations Program:

"during 2007 Open Source Testing has begun to generate fairly regular revenue. It has always been my aim, should the site become commercially viable, to put some profits back into the open source community. I will be aiming to make bi-monthly donations (funds providing) to open source testing projects and open source organisations of my own choosing. The donations will not be earth shattering, but whether they cover hosting and hardware costs, contractor costs, publicity, trips to events or just some extra motivation, they will certainly make a difference."

Great work Mark!

#    Comments [0] |
 Tuesday, November 27, 2007

Python - Extracting Files From Zip Archives

Here is a way to unzip files in Python.  If you have a zip containing multiple files, you can unzip it like this:

import zipfile

fh = open('foo.zip', 'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
outfile = open(name, 'wb')
outfile.write(z.read(name))
outfile.close()
fh.close()
#    Comments [6] |
 Monday, November 26, 2007

wxPython - Hello World!

Here is a simple example for those getting started with Python GUI Programming, wxWidgets, and the wxPython Bindings.

This small program will display a Frame and the static text "Hello World!", positioned with a BoxSixer.

Output looks like this:



#!/usr/bin/env python

import wx

class Application(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, 'My GUI', size=(300, 200))
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(sizer)
        txt = wx.StaticText(panel, -1, 'Hello World!')
        sizer.Add(txt, 0, wx.TOP|wx.LEFT, 20)
        self.Centre()
        self.Show(True)

app = wx.App(0)
Application(None)
app.MainLoop()
#    Comments [0] |