Common Modules


Graphics module

John Zelle wrote a nice graphics module to go along with his text book for computer programming. It's open source and is compatible with both Python 2 and 3 and on Windows as well as Linux. We've starting converting projects to use it instead of Pygame which seems to no longer be in active developement.

graphics.py is available here and it is also included in the zip files of pprojects that use the module.

The best documentation for John's module is his own 8-page pdf file

Button module

Small extensions graphics...

The button module uses the graphics module. It simply draws a rectangle with text in the center. When the mouse is clicked our program can query each button to see if the click happened there. The mouse click x/y coordinates are sent and each button checks to see if the click was inside its rectangle.

button.py is available here,

Simple Argument Extraction

The module, sarg, is used quite a lot in new and revised projects. It lets us access key=value arguments from the command line and builds a dictionary for later look-ups. It's to how Python uses optional arguments in functions.

The sarg module makes program invocations in the documentation very readable and is especially handy when lots of values may be specified. It requires hardly any coding overhead and makes modifying programs quick and easy. If a key=value pair is left out, defaults are provided. Arguments without an = are ignored as is any unused pair.

sarg.py is available here

A typical command line with arguments might look like this...

$ python kmeans.py pixels=300 prefix=kmeans maxpics=40

and later in your Python program calls to extract the values might look like

pixels  = sarg.Int("pixels", 600) # 600 is the default value should “pixels” be missing
prefix  = sarg.Str("prefix")
maxpics = sarg.Int("maxpics", 20)

When sarg is imported for the first time a dictionary is assembled from appropriate arguments on the command line.

In your Python program passing a key to sarg.Int(), sarg.Float(), or sarg.Str() from the program receives a converted value or the default.

When calling sarg functions, a default value may be provided as the second argument (as with pixels above) to handle the case where a key-value pair is missing. If a key-value pair is missing and no default value is provided then sarg.Str(), returns an empty string, sarg.Int() returns 0 and sarg.Float() returns 0.0.

You can download lib.zip here

If you have comments or suggestions email me at mail me

Copyright © 2014-2021 Chris Meyers and Fred Obermann