Category: Programming

Programming Skills Test April 16th, 2007

This site is not of my creation but it is the type of website I’ve wished for for quite a while and have considered creating more than once. Project Euler hosts a series of math / computer programming problems that let a coder test his or her skills. As of 4/16/07, I’ve completed the first 14 problems.

Book Review: The Art of UNIX Programming March 30th, 2007

If you are a UNIX/Linux user of any kind, I highly recommend reading Eric S. Raymond’s The Art of UNIX Programming. While I was expecting his book to be fairly one-sided about the virtues of UNIX and the shortcomings of everything else, I found the entire book to be well-balanced, informative, and very readable. TAUP could easily be used as a text for a programming class, but it’s really more of a “philosophy of programming” book. This book is filled with tons of quotable material, so I will resist the urge to make a quote-fest of this book review…

Raymond begins with a solid chapter on the Philosophy behind UNIX. He provides a number of great guiding principles for developing smart, streamlined applications. Where he could be abstract and vague, he provides concrete, usable advice. One of the great things about the book is his ample use of quotations from other UNIX gurus. From Doug McIlroy‘s A Quarter Century of Unix:

This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

He also quotes Rob Pike on his 6 rules of C programming. I found the last two to be especially poignant:

Rule 5. Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

Rule 6. There is no Rule 6.

Raymond goes on to compare and contrast the major operating systems of the past 20 years, including VMS, MacOS, OS/2, Windows, BeOS, MVS, VM/CMS, and Linux. He provides a framework and vocabulary for discussing OSes. Concepts like multitasking, process-spawning, file formats (e.g. textual vs. binary), and internal system boundaries differ greatly from OS to OS and have profound effects on the end user experience. Again, Raymond covers fairly academic material, making rigorous arguments using carefully defined terms. But he presents the material in a very novel and personal way, making the book feel less like a textbook and more like a pleasure read.

Once we understand the type of development environments various OSes can offer us, Raymond begins to describe criteria which we can use to set quality software apart from average software, like compactness and orthogonality. Compact software has the property that it can fit conceptually inside a human’s head. Orthogonal software has the property that operations are atomic and have no unexpected side effects; they change one thing without affecting others. Often, the concepts for good software design are best-illustrated by example, and Raymond provides many such examples to drive each point home. One of my favorite things about this material is that it has changed my thinking about design/architectural elements in my own programs.

Again, I’ll avoid the urge to summarize every chapter, as much as I’d like to. There is something to be said for letting the next reader get as much out of the book as I did. There are, however, a couple other sections of the book that I felt were compelling and deserve discussion.

One of the more enjoyable sections of the book for me was that on Unix Interface Design Patterns. Those of us who have taken a software engineering class may already have been exposed to some of the popular design patterns, so most of the vocabulary is familiar. Raymond describes patterns like The Filter Pattern, of which grep is a good example. Among other patterns, he also describes The Cantrip Pattern, The Sink Pattern, and The Compiler Pattern. The Cantrip Pattern is the simplest Unix design pattern as it has no inputs or outputs; its behavior is only affected by startup conditions. Classic examples of the Cantrip Pattern include rm and touch. Raymond reminds us to resist the temptation to write more interactive programs when a Cantrip will suffice because the program will lose scriptability.

In a further discussion of the software complexity idea, Raymond uses his “Tale of Five Editors” to explore the trade-offs between interface and implementation complexity. The five editors he compares and contrasts are: ed, vi, Sam, Emacs (my personal preference), and Wily, an editor I had neither used nor heard of. Raymond gives accurate, matter-of-fact descriptions of each of the editors and shows how . When ultimately arriving at the question of the Right Size for Software, Raymond leaves us with the Rule of Parsimony: “Write a big program only when it is clear by demonstration that nothing else will do.”

For every interesting part of the book that I mentioned in this review, there are 5 or 6 that are equally thought-provoking and affirming to those of us that embrace the *nix culture. I can’t recommend this book any stronger to any hacker or fan of Unix.

8 Reasons Why Python Rocks November 27th, 2006

First Things First


8 Reasons to Use Python

  1. Lists, Tuples, and Dictionaries
  2. Intuitive Looping Techniques
  3. Text Processing and String Operations
  4. Exception Handling
  5. Sockets
  6. Tons of Standard Libraries
  7. Working with Files
  8. Simple HTTP/Web Services


1. Lists, Tuples, Dictionaries

  • Lists – basically arrays (actually linked lists), can contain multiple data types, accessible by index, can be used as a stack or queue, can be sliced/concatenated
  • append, insert, remove, pop, count, sort, reverse
  • Tuple – statically defined, can be accessed by index but cannot be modified after instantiation, can be used as dictionary keys
  • Dictionary – accessed by key, value pairs, constant lookup time, unordered in memory
  • keys, items, del


2. Intuitive Looping Techniques

  • for i in range(10):
  • for name in [‘karl’, ‘lenny’, ‘barney’, ‘moe’]:
  • for i, j in enumerate(myList):
    print i, j
  • for k, v in myDictionary.iteritems():
    print k + “‘s value is”, v
  • questions = [‘who’, ‘what’, ‘when’, ‘where’]
    answers = [‘Mr. Green’, ‘revolver’, ’11:00′, ‘Lounge’]
    for q, a in zip(questions, answers):
    print q + “:”, a


3. Text Processing and String Operations

  • Strings can be indexed like C, but substrings can also be specified with slice notation
  • s[0], s[:2], s[4:], s[-3:],…
  • Standard string library provides dozens of useful functions
  • split, join, lower, upper, replace, title, isalnum, isdigit,
    whitespace, punctuation, printable, etc., etc.
  • Very easy to implement “filter-type” programs like grep
  • input = sys.stdin.readlines()
  • makes parsing formatted text files trivial, e.g. Apache log parsing


4. Exception Handling

Very easy to catch exceptions and run separate code for error handling routines

try:
something = that_could + produce_an_exception
except:
this_code = gets_run( only_if_exception_thrown )


5. Simple Sockets

  • Very simple to implement TCP/IP sockets
  • Easy to add networking support to your Python scripts
  • Inexpensive way to communicate between programs, even locally
  • Server:
  • 1. bind
  • 2. listen
  • 3. accept
  • 4. recv/send
  • Client:
  • 1. connect
  • 2. send/recv


6. Tons of Standard Libraries

  • Python ships with over 300 standard libraries for programmers to use:
  • >>> help()
    help> modules
  • Just to name a few:
  • calendar, Cookie, cookielib, datetime, distutils, htmllib,
    HTMLParser, httplib, math, md5, optparse, os, random, re,
    smtplib, socket, string, struct, sys, time, urllib, urlparse,
    xml, xmllib


7. Working with Files

  • Reading and writing files is very simple in Python
  • open(), read(), write(), close()
  • os library provides many useful function calls for filesystem access
  • chdir, chmod, execv, getcwd, listdir, mkdir, remove, rmdir,
    rename, walk


8. Simple HTTP / Web Services

  • Almost everything provided to the programmer by httplib and other HTTP/URL libraries
  • httplib.HTTPConnection
  • request
  • getresponse
  • getheaders
  • status
  • reason
  • read
  • close


Putting it all Together

  • Easy to code ⇒ rapid prototyping of new functions/programs
  • Easy to read ⇒ whitespace dependence leads to highly readable and reusable code
  • Let’s write a couple of Python scripts
  • factorial.py
  • primefactors.py
  • feedMonitor.py

I am definitely open to discussion on this topic as it’s one that’s close to my heart. If you’re a Perl advocate or would otherwise like to make a case against the use of Python, I strongly encourage you to send me an email. I am more than happy to try to defend it 🙂

PHP Texas Hold ‘Em September 18th, 2006

Table cards:

10♣ 10♠ 6♣ 8♦ 8♥

Your cards:

Q♥ 9♥

Result: 2 Pair
Hands required: 1

Hand Type:

Any Pair 2 Pair 3 of a Kind Straight
Flush Full House 4 of a Kind Straight Flush Royal Flush

My eBay Watch List April 2nd, 2006

01-Sep-2009: Update

This code no longer works with the current eBay API, but I’ll leave it posted for reference.

eBay has some very cool functionality that allows application programmers to make API calls on a user’s behalf without using their site credentials. I decided to investigate the eBay API a bit and wrote a function that allows web developers to print the items listed in an eBay buyer’s My eBay section on third party web pages. Feel free to check out the code.

The eBay Developer Program has a guide to help developers begin using the API. You can make all kinds of unauthenticated function calls to the API, like listing search results, without using eBay’s Auth and Auth (Authentication and Authorization) system. But to make calls on behalf of a particular user, you will need to generate an auth token. This can be done using eBay’s Authentication Token Tool.

Essentially, this process works by entering an eBay user’s site crentials in the token generator and eBay returns a cryptographic token. This token can then be passed along with the API function calls to authenticate the caller without including the user’s login and password.

The function I wrote is essentially a wrapper to the API call GetMyeBayBuying. Calling my function, printMyEbay, will print the auctions that a user is a) watching, b) bidding on, and c) has won.

PyRSA – RSA in Python June 18th, 2005

PyRSA is a command line utility that allows users to digitally encrypt and sign messages using the public key encryption scheme, RSA. There are three basic functions that PyRSA performs: encryption, decryption, and key generation.

Downloads:

Source: pyrsa.py

Sample Use:

1. Generate a public and private key. In this example, we will specify a key of length 1024 bits. Allow several seconds of CPU time for the generation of the keys.

pyrsa.py -g 1024 Enter file identifier (i.e. first name): brandon

2. Now the files

brandon_privateKey.txt

and

brandon_publicKey.txt

are in the current directory. Next place the text we want to encrypt in a text file.

echo "The sky above the port was the color of television, tuned
to a dead channel." > message.txt

3. Encrypt the message using the public key and redirect the output to a text file.

pyrsa.py -e message.txt -k brandon_publicKey.txt > ciphertext.txt

4. At this point the file ciphertext.txt contains the encrypted message. The file can safely be sent to a recipient, i.e. as an email attachment, the contents utterly unreadable to anyone without the private key.

cat ciphertext.txt 32464047998704731086703458860763720628883125201
840735448292781611869424600546740055592235111171870058664751326891
416030992911165222195048303846516331939189036032662981573683210672
785053735077400433222553780571914729993485153779710689497701348386
214277988780913721453283666357504772556433129612632786845350983

5. Next we will assume the message has been sent to the individual who possesses the corresponding private key and he wants to decrypt the message.

pyrsa.py -d ciphertext.txt -k brandon_privateKey.txt
Decrypted text:
The sky above the port was the color of television, tuned to a dead channel.



East Bay Psychotherapist
Licensed Clinical Social Worker provides psychotherapy and counseling services for couples and individuals in the East Bay Area.