[Tutor] Request for code critique

Lloyd Kvam pythontutor at venix.com
Wed Oct 22 17:58:04 EDT 2003


http://www.python.org/doc/current/ref/try.html#l2h-597
7.4 The try statement

I tried running the original code with no existing shelve file.  The
program failed and left behind an empty file which was invalid for
the next run.  The python garbage collection along with the well coded
modules will often do the right thing when your program fails.  However,
it's a good idea to use a try/finally to manage resources to ensure proper
shutdown even when the program fails.

Once the original script got turned into functions, it was necessary to
call the main function, in this case: main_shelve.  that could have been
done by simply adding this line at the left margin:

main_shelve()

However, this line would be executed if the module was imported.  The test
for __name__ == "__main__" allows us to distinguish between an import and a
direct execution.

http://www.python.org/doc/current/ref/naming.html#l2h-319
4.1 Naming and binding

Now you can import this script and call the main function with
a list of primes and bypass shelve.  Even better, you could break main
into useful functions (such as get_factors) that might be useful in
other contexts.

Someone else already suggested turning this into a class.  The prompt for
a number would go outside the class, but the use of shelve to cache primes
is a natural situation for using a class.  The class allows you to hide
the try/finally logic and the prime caching inside a simple interface where
a calling function would learn the factors or primality of a number.


Matt Hehman wrote:

> Sorry if this shows up twice, it got bounced once.
> 
> Okay, lots of good feedback to digest.  Lloyd Kvam
> sent me a complete revised program.  The highlights
> were as follows:
> 
> #retrieve list of known primes
> def main_shelve():
> 	try:
> 		d=shelve.open('primesfile')
> 		try:
> 			primes=d['primesfile']
> 		except KeyError:
> 			primes = [2,3,5]
> 			d['primesfile'] = primes
> 		main(primes)
> 	finally:
> 		d['primesfile'] = primes
> 		d.close()
> 
> #defines function for finding common factors
> def Euclid(a,b):
> <snip>
> 
> def main(primes):
> <snip a neatened up version of the rest of my program>
> 
> if __name__ = '__main__':
>         main_shelve()
> 
> The use of "try:" and "finally:" is new to me (docs?) 
> The last two lines are a total mystery, as is the fact
> that the program actually runs (and quite nicely) when
> all I can make out is that functions have been
> defined.  Why does it execute?
> 
> 
> 
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list