[Tutor] Iterators, example (need help)

Bryan A. Zimmer baz at comcast.net
Tue Oct 23 00:30:44 CEST 2012


Hello, all.


I am a long-time programmer with little experience in Python, but am
trying to learn. The example program, attached, is a toy app with a
GUI that merely prints out environment keys and their associated
values.

I know there are errors in the program, but I wanted to see if someone
can tell me something about the iterator "magic" that this program is
trying to use.

The whole program was designed on, and written for cygwin (bash shell)
under Windows 7. The python version is 2.6.3. I haven't tried it on
other platforms but it "should" work under Windows and Linux.

I want to understand what I stumbled into here in order to develop the
code and use similar techniques in the future. Specifically, is it
really necessary to use __iter__ and __next__ as system functions
(with the double underscores)? I can program the basic program in any
number of ways but was enthralled by the concept of iterators and
generators.

I know I missed the mark while trying to use the pythonic idiom, but maybe
you can help me see where I went astray.

Hopefully you will forgive my home-grown oop and GUI skills, I am from
the old school of programming, prior to when these became necessities.

The original inspiration for the code came from "Dive Into Python",
from the section about iterators and classes, from the fibonacci2.py
example.

Thank you

Bryan A. Zimmer
--------------------------------------------------------------------

from Tkinter import *
import os

ignore1='''

This program works to do what I set out to do as a first step
BAZ 10/19/2012

'''


class Env:
    ''' This class represents the environment of the current process'''
    def __init__(self, master=None):
        self.d = os.environ.keys()
        self.v = os.environ.values()
        self.i = 0
        self.y = self.v[self.i]
        self.x = self.d[self.i]


    def __iter__(self):
        self.x = self.d[self.i]
        self.y = self.v[self.i]
        return self



    def __next__(self,master=None):
        global s1
        global s2

        self.i += 1 
        if (self.i < len(self.d)):
            self.x = self.d[self.i]
            self.y = self.v[self.i]
            
        if (self.x):
            print self.x, '==>', self.y
            s1.set(self.x)
            s2.set(self.y)
        else:
            print ("raising Stop Iteration")
            raise StopIteration
        return ((self.x, self.y))


class App(object):
    ''' This is the main GUI app'''
    def __init__(self,master):
        self.createWidgets()

    def createWidgets(self,master=None):
        global s1
        global s2
        self.fm1 = Frame(master)
        self.fm2 = Frame(master)
        self.fm1.pack(side = TOP, anchor=NW, fill=BOTH,expand=YES)
        self.fm2.pack(side = TOP, anchor=SW, fill=BOTH,expand=YES)
 
        s1=StringVar()
        s1.set(env.x)
        self.l1=Label(self.fm1,text='Var:')
        self.l1.pack(side=LEFT,fill=X,expand=NO,padx=10)  
        e1=Entry(self.fm1,textvariable=s1,width=40)
        e1.pack(side=LEFT,fill=X,expand=YES,padx=10)
 
        self.l2=Label(self.fm2,text='Val: ')
        self.l2.pack(side=LEFT,fill=X,expand=NO,padx=10) 
        s2=StringVar()
        s2.set(env.y)

        e2=Entry(self.fm2,textvariable=s2, width=40)
        e2.pack(side=LEFT,fill=X,expand=YES,padx=9)
       
        self.b1=Button(self.fm2,text='Next',command=env.__next__)
        self.b1.pack(side=TOP,anchor=S,fill=X,expand=YES,padx=10)


if __name__=='__main__':
    env=Env()
    root = Tk()
    root.option_add('*font', ('verdana', 12, 'bold'))
    root.title("Pack - Example 12")
    app=App(root)    
    root.mainloop()
----------------------------------------------------------------------------
-------



More information about the Tutor mailing list