[Tutor] further ignorant babbling

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 2 Dec 2001 13:58:14 -0800 (PST)


On Sat, 1 Dec 2001, Kirk Bailey wrote:

> import strings

This might be a typo: you probably mean "import string" instead.



> import mail
> #
> # define path information and domain name in the lines below
> domainname = 'howlermonkey.net'
> pathtosubscribers = '/www/www.howlermonkey.net/cgi-bin/lists'
> #
> # receive the incoming letter from <stdin>
> incoming=raw_input()
> #
> # parse the letter into a dictionary
> letter{}=Mail.parse(incoming)

Python is "dynamically typed", so you don't need to tell Python that
"letter" will be a dictionary.  If 'Mail.parse(incoming)' returns a
dictionary, then:

    letter = Mail.parse(incoming)

should be enough.  Be careful!  'Mail' is uppercased here, but your import
uses a lowercased 'mail'.  I'm not sure which one is right, but you'll
need to check this.



> # clip off preceeding and trailing whitespace chars if any
> to=string.string(to)

You probably mean:

    to = string.strip(to)


There's some documentation on the 'string' module here:

    http://www.python.org/doc/lib/module-string.html



> # this next gives us the part after 'To:' and before '@'
> to = string.string( letter, string.pos("@",3, 'to:'))


Hmmm... You might want to write this as a separate function, because it
does seem like a useful thing to have.  Here's an example function that
knows how to strip the domain out of an email address:

###
def stripDomain(email):
    end = string.find(email, '@')
    return email[:end]
###

Once we have a function like this, we can use it as if it were part of
Python itself:

###
>>> stripDomain("deliberatus@my995internet.com")
'deliberatus'
###

Functions are often fun to write: by writing functions, we can expand the
number of "verbs" we can use in our programs.