Macroware Technology

Welcome Portlet


Welcome to Macroware Technology Blog.

The purposes of this engineering blog:

- Share knowledge
- Learn
- Have fun
- Document what I was doing / thinking

Search Box

 

Private Area

Mailing List

Engineering Education

Engineering Organizations

Professional Career Networking Groups

Electronics Hobby

Trade Publications

Python Scripting

posted Friday, 31 October 2008

I have been meaning to learn a scripting language for at least two years to automate and lean out some tasks related to high speed digital design.  When I set this objective a couple of years ago, I did some research to determine what language would be a good choice.  Perl is commonly used at my company for engineering scripts.  I have looked at some of the code over the years, and it is virtually unreadable and unmaintainable.  I even bought a Perl book one time (someone stole it, and at the time I thought good riddance!)  So after some research I came to the conclusion that Python had a lot of math extensions, a nice readable syntax, and widespread support on different operating systems and platforms.   I bought a book called Python Essential Reference almost exactly two years ago while on a business trip and never got around to opening it until now [1].

Well necessity is always a driving force in engineering, and in this case necessity was driven by my son.  He loves numbers: "Dad, after the tens comes the hundreds, and then after the hundreds comes the thousands, and then after the thousands comes the millions. Dad I'm going to count to 1 milllllioooooooon!"  He was very disappointed when I told him that it would take too long for him to count to 1 million and he wouldn't be able to do it.  I suggested that we make the computer count to 1 million and then see how long it took.  So the challenge was presented.  I quickly went out to the internet to www.python.org and downloaded the Windows version of Python and installed it.  Then I looked through some examples and was quickly able to construct a simple program directly in the interpreter that did the counting printing out each number from 0 to 1 million.  This is a testament to how simple Python is to use - try learning a new programming language while an impatient 5 year old watches over your shoulder.  We timed the counting with a stop watch at about 1 and a half minutes.  Then excited at what we had just done he said, "Dad, I want the computer to count to 1 billion."  I figured that would take much longer, and we did a quick calculation to determine how long.  We ran the program for counting to 1 billion and let it run all night.  By morning, it had reached 1 billion.  The excitement that this generated was very cool.

Today I polished the program into an actual script, incorporating user input (asks what number you want to count to), as well as a built in timer to time how long it takes.  Here is my first Python script (count.py):

 

import time
import sys

a = 0
counter = long(raw_input('What number would you like me to count to?  '))
print '\n'
start_time = time.clock()
while (a < counter + 1):
    print a
    a = a + 1

end_time = time.clock()
duration = end_time - start_time
if duration < 60:     
        print 'Done in ', duration, 'seconds'
elif (duration / 60) < 60:
        print 'Done in ', duration/60, 'minutes'
else:
        print 'Done in ', duration/60/60, 'hours'

print 'press any key to end...'
temp = sys.stdin.read(1)

In the process of writing and debugging this program I learned that the read command does not really work inside the Python IDE shell.  A reason for this was explained in [1].  Also, even when running the script outside the IDE, the read command doesn't behave as I had expected.  It does read in one character, however it waits until enter is pressed to proceed.  Based on my understanding of the documentation, it sounded like it should grab the first key pressed and then move on.

Now it's on to something a bit more practical and useful.

 

[1] Beazley, David M. 2006. Python Essential Reference Third Edition.  Indianapolis, Indiana: Sams Publishing.