[Tutor] failing to learn python

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Apr 11 01:20:37 CEST 2006


> I am a parttime sys admin so I want system admin problem which usually I 
> do through shell scripts like parsing logs, generating reports, greping 
> with regexes etc.

Hi Payal,

You might also find David Mertz's book "Text Processing With Python" to be 
useful for you:

     http://gnosis.cx/TPiP/

I agree with the other recommendation on "Dive into Python": it provides a 
lot of good practical programming knowledge.

     http://diveintopython.org/



> The only thing I don't want is silly problems like generate fibonnacci 
> series, add numbers from 0-n etc. non required silly stuff.

Just as a counterpoint: some of those silly things aren't useful in their 
own right, but they're finger exercises to help one really get mastery 
over the language.  Recursion is one of those things that are core 
concepts, and most Python tutorials try to cover it by talking about 
factorials.

It's very unfortunate that fibonacci numbers are one of the "classical" 
examples of recursion, since they are useless to most people.  But don't 
discount recursion altogether.

We can look at concrete example of recursion that might be more 
applicable.  One common system administration question that pops up every 
once in a while is: "How do I do [some_action] to every file in my 
directory?"

We can either know magically that os.walk() will do the hard work for us, 
or we can use a combination of os.listdir() and os.path.isdir():

#############################################
### pseudocode
def my_walk(some_action, dir_or_file):
     if os.path.isfile(dir_or_file):
         some_action(dir_or_file)
     else:
         for thing in os.listdir(dir_or_file):
             my_walk(some_action, thing)
#############################################

in which there's a specific "base" case for handling regular files, and an 
"inductive" case for handling directories.

Another example of this kind of processing involves things like XML or 
HTML processing.  If we wanted to get all the anchor link elements in an 
HTML document, how would we do this?  We again break it into two cases: 
one to handle anchor elements, and another to handle anything else:

##############################################
## Pseudocode
def get_anchors(element):
     if element is an anchor:
         return [element]
     else:
         anchors = []
         for child in element.children:
             anchors.extend(get_anchors(child))
         return anchors
##############################################

We're in a slightly different domain, but if we look at this with a 
critical eye, we should see that the code structure is similar to the 
directory-walking example.  There's a case for handling really simple 
problems, and another case for handling slightly harder problems.


That's the key insight you should have been getting.  If the tutorials 
that you're reading haven't been delving into this, that means that those 
tutorials aren't doing a good job.  The fact that factorial() looks like:

################################
def factorial(n):
     if n == 0:
         return 1
     else:
         return n * factorial(n-1)
################################

is pretty darn worthless in itself.  But the idea that we can break things 
down into two categories (simple cases, slightly complex cases) and handle 
them in some structured way is a valuable concept.


If you see something that's looks trivially un-useful when you're reading 
a tutorial, bring it up on this list.  They're bound to be some kind of 
real application for it.  *grin* And if you have any other questions, feel 
free to ask.


Good luck!


More information about the Tutor mailing list