[Courses] [python] Lesson 9: More extras (requested topics)

Leslie leslie.brothers at verizon.net
Tue Aug 16 04:32:11 UTC 2011


This homework comes under the heading of 'fun'.  It started back in
exercise one of Lesson 6 homework (“write a function that takes a
dictionary as argument, chooses a random key from that dictionary, and
returns the key and its value as a tuple”). I made a dictionary of birds
species as keys and their nesting sites as values.

With successive lessons, I elaborated a 'bird' program on my own -- for
example,  adding object-oriented functions, passing optional parameters,
and using some ideas from other people's homework.  My latest goal was
to add pictures, but I couldn't make them go away -- which is essential
-- until I learned the magic of win.connect(“destroy”, gtk.main_quit) in
Akkana's  GUI article (first Linux Planet reference in this lesson).

There is nothing very advanced about this program, but the pictures are
pretty.  I don't know whether they will make it through onto the list
but I am going to try to attach them in case anyone wants to see how
they work.  They need to go in the same directory where the program is
run.  In fact, I am throwing in an extra picture of a scarlet tanager
because I like it so much, although it isn't included in the standard
gallery (but you can add it).

(Next on my list after this is to put these birds and their pictures in
a MySQL database, since I know a little about that, and learn to use
Python with a database.)



import gtk
import random


class Bird:
	location = ''
	tax_name = ''
	pic = ''
	def __init__(self, location, tax_name, pic):
		self.location = location
		self.tax_name = tax_name
		self.pic = pic

birds = {"yellow-nosed albatross": Bird("Tristan da Cunha", "Diomedea
chlororhyncos", "Yellow-nosedAlbatross.jpg"),
	"indigo bunting": Bird("Mexico","Passerina
cyanea","Indigo_Bunting.jpg"),
	"Scott's oriole": Bird("California","Icterus
parisorum","ScottOriole.jpg"),
	"common yellowthroat": Bird("Central America","Geothlypis trichas",
"Common_Yellowthroat.jpg")
	}

def showRandomBird(b):
	species = random.choice(b.keys())
	return (species, b[species]) 

def AddBird(name, location = "unknown", tax_name = "unknown", photo =
"unknown"):
	birds[name] = Bird(location, tax_name, photo)

while True:
	q = raw_input("Would you like to add a bird species to the gallery?
(it's optional)\n Enter 'y' if yes. Or press 'g' to just proceed to the
gallery.\n")
	if q == 'g':
		print "OK."
		break
	elif q == 'y':
		name = raw_input("Enter its common name.\n")
		loc = raw_input("Enter its range or hit 'enter' if unknown.\n")
		tax = raw_input("Enter its taxonomic name or hit 'enter' if
unknown.\n")
		photo = raw_input("Enter the name of its picture or hit 'enter' if
unknown.\n")
		AddBird (name, loc, tax, photo)
	else:
		print "Sorry, I didn't understand your entry."

w = len(birds)
print "You will see a random display of %d bird profiles.\n" %
(len(birds))

while birds:
	our_bird, birdinfo = showRandomBird(birds)
	if (len(birds)) == w:
		print "Our first bird:\t%s" % (our_bird)
	elif (len(birds)) == 1:
		print "Our last bird:\t%s" % (our_bird)
	else:
		print "Our next bird:\t%s" % (our_bird) 
	print "Nesting site:\t%s" % (birds[our_bird].location)
	print "Taxonomic name:\t%s" % (birds[our_bird].tax_name)


	x = raw_input("Press 'enter' to see its picture, or 'q' to quit\n")
	if x == 'q':
		print 'Bye'
		break

	print "Close picture to continue."

	win= gtk.Window(gtk.WINDOW_TOPLEVEL)
	win.set_size_request(300,260)

	vbox = gtk.VBox()
	win.add(vbox)

	button = gtk.Button()

	vbox.pack_start(button)
	button.show()
	buttonpic=gtk.Image()
	buttonpic.set_from_file(birds[our_bird].pic)
	button.add(buttonpic)
	buttonpic.show()
	win.connect("destroy", gtk.main_quit)
	del birds[our_bird]
	vbox.show()
	win.show()
	gtk.main()

if (len(birds)) == 0:
	print "We're out of birds!"


- Leslie
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses



More information about the Courses mailing list