Category Archives: Python

Ad-Positioner Update

I have started a version of the Ad-Positioner script that uses easygui because it is entertaining and I can check code locally.

We have a new mascot for the project: Rockie the Flying Squirrel Cat

Rocket J Squirrel Cat

Rockie, Our New Mascot

There are currently 2 flavours of Ad-positioner in process.

  • ad-positioner_0.3.0 is available here, until we use so much bandwidth that my webhost complains, at which point we have to figure out how to put it up on sourceforge. 0.3.0 uses EasyGUI
  • ad-positioner_0.2.0 is available here. 0.2.0 is command-line python.

This is not as unsafe as you might imagine, because I have got index pages in all these directories.  For instance: http://sourcefreedom.com/wp-content/uploads/2009/12/ shows that index page.

Here is a view of the first dialog of the script.  Click on it to see a full-sized version.

First screen of ad-positioner

The current py-script opens a cute set of dialogs, but it doesn’t do a whole lot yet… current-flow is a pdf to show you what is planned.

Exit Dialog for Rockie's Ad-PositionerMore info soon..

If you want to keep track of our updates, please subscribe to the site or the rss feed.

A Fun Python Password Program

This started out as a very simple example of a bash-script example for a simple password-required app. I started thinking that it would be pretty easy to crack the password, and thinking about different ways that the password could be hidden better. Since it has no password-recovery function (yet), I was thinking “Who you gonna call!” to help with getting the password back after you lose it.
The obvious answer was “Ghostbusters!” Then I added an easter egg. With apologies to all the affronted parties involved:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# keymaster.py
password="xul"
password2="moo"
answer=""
# Loop around forever (until the break statement is used)
while password2==password2:
# Ask the user for the password
answer = raw_input("\nI am the gatekeeper. Are you the Keymaster?\nIf you are the Keymaster, enter the key: -> " )
# Read in what they type, and put in it $answer
# If the answer is the password, break out of the while loop
if answer == password:
break
if answer == password2:
print '''

ascii art cow

ascii art cow

....\"Have you mooed today?\"...
.... \"I am a Little Cow!\n\"...'''
print "\nMoo? Moo??! \nI asked if you were the Keymaster, not the CowMaster!!\nThe keymaster would never say \"",answer,".\" \nGo and find the keymaster! \nDon't come grazing around here until you do!\n\n"
# If they get to here, they must really be the Keymaster,
# and not just a little cow
# otherwise the script would keep looping
print "\nWelcome Keymaster.\nWe must make the way ready for the return of Xul!"

The three possible outputs look like this

outputs of keymaster.py

outputs of keymaster.py

Python EasyGUI Quick Review

I was googling about for a way to make beginning Python more fun.  I teach an introductory class on “thinking like a programmer,” and some students are not instantly captivated by the command-line environment. We use the Python Language because it is probably the easiest, least obscure language available. I have been looking for a way to jazz up the class for a while. The concepts are hard enough without having to surmount the students’ feeling that anything on the command line must be dull and old-fashioned. I have looked at several options for GUI-fying Python.
There are a number of IDEs (development environments) and add-ons listed at Python.org. I have looked at several of them with the idea that one or another will make the output more interesting by adding GUI outputs.  I do not want the course to devolve into strictly an intro to the details of the programming language.  It is challenging to think about things in such a way as to set up problems which can be addressed programatically.  Because of the challenge, students often look for less-alien things to focus upon.  The nuts and bolts of the programming language are safer ground, so many stay there, and miss the point entirely.  You cannot just learn the language and expect it to magically clarify your thinking.

I discovered easygui at http://easygui.sourceforge.net today. It will probably take some more study, but I believe it answers the question well enough. My course is only eleven weeks long, and there just isn’t time to learn a whole programming language in that time. There certainly isn’t time to do it and at the same time cover all the logical basics we are tasked to cover.

Just as a quick example, I ran part of their tutorial, and changed it around to be more entertaining to me.  This was extremely simple to figure out and I think it will be much more rewarding for a beginner than what I am currently teaching, where the most graphical element is the turtle module.

The steps:

1. First you have to go to http://easygui.sourceforge.net and get the newest version of easygui for your version of Python.  Because easygui is itself a python script, you should be able to get it to work well regardless of what operating system you are using.  I am using Ubuntu 9.10.  My editor of choice is Kate.  Kate is the default text editor on the KDE desktop environment.  If you are on a Windows machine, you can use Idle, which comes bundled with Python, or you might consider getting Notepad++, which is a free high-functionality replacement for notepad.exe.

2. Once you have downloaded and uncompressed easygui, you need to put it somewhere in your PATH where Python can find it. The creator of EasyGUI suggests putting it in your Python site-packages directory.

On Windows, the site-packages folder is typically

C:\pythonXX\Lib\site-packages

For Python version 2.6, for example, the site-packages folder is

C:\python26\Lib\site-packages

The Ubuntu Linux location changed between Python version 2.5 and version 2.6.

  • For version 2.5 it is: /usr/lib/python2.5/site-packages
  • For version 2.6 it is: /usr/local/lib/python2.6/dist-packages

Before you can put anything into the dist-packages directory, you will probably have to do:

sudo chmod 755 /usr/local/lib/python2.6/dist-packages

I found I had to use sudo to put the file in the dist-packages directory, even with the directory permissions adjusted. 755 means the owner can read, write to or execute the file, while others can only read and execute. That means I had to be root to move the file into that directory. I used the cp command to place the easygui.py file in the folder.

sudo cp /home/wolf/Documents/Projects/py-proj/easygui.py /usr/local/lib/python2.6/dist-packages

3. Once that is done, you can check the installation by typing the following in the Python Interactive window:

>>>import easygui
>>>easygui.egdemo()

You can also do it this way, if you don’t want to use the dotted notation…

>>>from easygui import *
>>>egdemo()

This will bring up a GUI window with examples of all the possible EasyGUI modules. Using the latter import structure makes your own source code shorter. You will not have to mention the imported module over and over. The former import syntax helps make it plain where the functionality is coming from, but requires a lot of extra typing.

EasyGUI Modules - egdemo() -

EasyGUI Modules - egdemo() -

4. I copied the first example from the EasyGUI tutorial link and adjusted the indentations to 4 spaces, which is standard on my system.  I then changed the dialog’s wording to make it more entertaining. I used triple-quotes ”’ to get the line-breaks where I wanted them, because hard returns inside triple-quotes do not break the string.  Inside single or double quotes, a hard return will break the string, and cause a traceback error. Triple-quotes are made by hitting the single-quote key 3 times.

Kate window

Kate window

Feel free to change things around, when you run this exercise.  You don’t have to use the topic of Ice Cream, for instance…

The first window used to be a “Hello World” window. It appears in the code shown in the Kate window at about line five. I changed it to say more about the program.

1st easyGUI dialog

1st easyGUI dialog

The second dialog, I created by copying the first and changing the info between the triple-quotes. The code starts on line ten. Notice that the triple-quoted text doesn’t follow the indentation rules at all. It is all the way to the left.

Second EasyGUI Dialog

Second EasyGUI Dialog

The third dialog is a list of Ice Cream flavors. The choices apear in the code at around line 18, and are in no particular order. I added a few flavors, also in no particular order. In the outputted dialog they show in alphabetical order, even though the list is not alphabetical.

Pick-List Dialog

Pick-List Dialog

The next dialog comes from line 23, where a single line in the code can make a whole GUI dialog. It ffirming your choice of flavor.
ex4
The final dialog, from line 27 in the code, allows you to escape “Cancel” or run the program again, “Continue.”
ex5
This took just a few minutes to do and has given me ideas for all sorts of ways the basic CLI Python exercises could be more fun.