[Tutor] further ignorant babbling

Kirby Urner urnerk@qwest.net
Sat, 01 Dec 2001 20:32:20 -0800

At 09:20 PM 12/1/2001 -0500, Kirk Bailey wrote:
>OK, newest and latest shot at looking like an idiot in
>front of my betters.

We're all idiots of one kind or another, not to worry.

># parse the letter into a dictionary

I'm not able to do a comprehensive debug of your pseudocode,
but want to point out that you're not quite getting dictionary
syntax.  Curly braces are never used in this subscript

># clip off preceeding and trailing whitespace chars if any

string doesn't have a 'string' method.  If 'to' is already
a string and you want to clip off any whitespace, the
function to use is string.strip().

Also, in recent Pythons, most string functions may be
invoked as methods against primitive string objects, without
importing the string module; in other words:

   >>> '  baaaa   '.strip()

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

If you're *sure* that "To:" and "@" will be in 'to', then
you can do it in one line with string methods:

   >>> to = "To:  joeblow@smallworld.com"
   >>> to
   'To:  joeblow@smallworld.com'

   >>> to[to.index("To:")+3:to.index("@")].strip()

(in this case, we're basically getting to[3:12] and
stripping off the leading spaces).  to.index("To:")
returns where "To:" begins in to (position 0).

># ok, we now know the name of the list to referr to.

open(filename,'r') returns a file object to a variable.
This file object now has methods applied to it, such
as readlines().

So you probably want to go:

   in = open(whatever,'r')

'to' just contains the name of one email person, as per
your above example.  Does that person have his own file?

># Dig, this tupple is the total subscriber file loaded,
># and is referred to with a index number!
>subscribers = in

You might be wanting

   subscribers = in.readlines()

which will make a list containing every line in whatever
(the file opened above) as an element in the list.

>close in


># the subscriber list is now in the tupple 'in'

No, 'in' is now closed and done with.  We read the
subscriber list into 'subscribers' using in.readlines()
-- I think is what you're getting at.

># note the 'from' field is LISTNAME.DOMAINNAME!
>letter{'From:') = to + domainname

Again, illegal syntax.  If letter is a dictionary, you
key into it using square brackets.  Example:

    >>> mydict = {}  # empty dictionary
    >>> mydict['to'] = 'joeblow@mydomain.com'
    >>> mydict['subject'] = 'The end of the world'

... like that (showing it in shell mode, interactively
-- the best place to experiment with Python syntax
even as you write longer code in another window, to
test out whether you're getting it right).

># append [listname] in FRONT of the existing subject.
>X = '['+ to + ']' + letter{Subject:}

Yeah, you can do something like this.  Remember to
insert '\n' strings where you want to code a line
break (newline).

>letter{subject:} = X

Confusing.  letter['subject'] should just be the subject,
no?  We don't want to stick the 'to' stuff in there too.

Can't we keep letter['to'], letter['subject'], letter['body']
and letter['footer'] all as separate entries until later?

# append footer to the end of 'Body:' in dictionary 'letter'.
>letter{'Body:'} = letter{'Body:'}+footer

   body = open(somepath/file,'r')
   letter['body'] = body.readlines()

Except now letter['body'] keys to a list, not one long text
string.  If you need to convert a list of text lines into
one long string, with newlines between each line, you can
do something like this:

   >>> linelist = ['this is a line', 'this is another line']
   >>> letter = {}
   >>> letter['body'] = '\n'.join(linelist)
   >>> letter['body']
   'this is a line\nthis is another line'
   >>> print letter['body']
   this is a line
   this is another line


   footer = raw_input('Enter footer:  ')
   letter['footer'] = footer

>#now send a seperate envlope and letter to each person in that
>subscriber file
>for i in subscribers():
>         letter{'To:'}=subscribers[i]
>         # This feeds a copy of the complete letter to sendmail
>         # with To: coming from the tupple subscribers
>         mail.send(letter{})

'subscribers' is a list, so you don't want to put () after it,
as if it were a function.  Your indexing i will become each
subscriber in turn, so just go letter('to') = i -- and when
you pass your letter to mail.send, just send 'letter' -- no
need for the {} tacked on (Python knows it's a dictionary).

This presumes mail.send is something *you* write.  If its
part of a Standard Library module named mail, then
mail.send(letter) will only work if send() expects a dictionary
with those specific entries.  Is that what's happening, or
was the idea of formatting a letter as a dictionary your
idea (not a bad one, by the way -- certainly workable)?

 > Oh, and the library called, and said the interlibrary
 > loan about python just arrived, I will pick it up monday.

You're doin' great.  The kind of program you're writing is
quite ambitious and wouldn't normally be tackled by a
beginner to *any* language (except maybe rebol).  That you
are willing to tackle such a thing is testament both to
Python's power, and your bravery as would-be snake charmer.