[Tutor] Need help wrapping my head around duck typing

Wayne Werner waynejwerner at gmail.com
Wed May 25 04:37:01 CEST 2011


On Tue, May 24, 2011 at 9:17 PM, Cory Teshera-Sterne <ctsterne at gmail.com>wrote:

> Hello,
>
> Thanks for the input. I guess you're right, this is more of a case of
> argument assertion - but then I'm not sure how to do error handling here,
> because, for example, multiple issues that should be dealt with in very
> different ways could raise the same error (as in the original example).
>

Well, there are (usually) built-in exceptions that work quite well. Wrong
type? Raise a TypeError. Value out of bounds? Raise a ValueError. Usually it
turns out that if you're raising more than one error, you probably have your
function doing too many things.

I'm also not sure how using glob would work any differently here - wouldn't
> I still need to iterate over a specified section the directory tree, and
> therefore have to figure out how to specify it? (Admittedly, I've only
> played with it for a few minutes, so I might be missing something obvious -
> and file creation dates don't mean anything in this particular context.)
>

 Specifying a directory tree is quite simple - ask the user!

Then you assume that the directory is correct/exists and you check the
files, either with glob or os.listdir, and return a list of anything that
matches. In the case that they enter an incorrect directory? If you use
glob, you'll get nothing. If you use os.listdir, you'll get something like
this:

>>> os.listdir('/home/flugle/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/home/flugle/'

Which you can handle in whatever way makes the most sense.

Alternatively, if you don't mind using Tkinter, you could do something like:

import tkFileDialog as fg
import Tkinter as tk

root = tk.Tk()
root.withdraw()
filename = fd.askdirectory()
root.quit()
# Go and do something with filename.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110524/4618d41d/attachment.html>


More information about the Tutor mailing list