[Tutor] while with function

fleet@teachout.org fleet@teachout.org
Mon Nov 18 15:44:16 2002


Maybe I'd better go into some background.

The "address book" entries will be created, maintained and utilized from a
command line.  The "add" function will display prompts for each field
(using raw_input) one field at a time.  I want to be able to display the
entries and be able to correct any of them.  I'm assuming, since I'm not
using any GUI, that I will have to change (if necessary) each entry
individually. Therefore, I need the ability to repeat the display however
many times is necessary. When I'm satisfied with the "add" information,
the variables will be concatenated (with pipe symbol delimiters) and added
to an address book file as a single line.

I should be able to use the editing function again if someone changes
address/phone/email/etc.

So maybe get_list() should be renamed edit_list(); but in any event, it
refers to the list of variables for each address book entry (name,
address, email, etc.).

I prefer not to mess with classes at this point in my python education.  I
don't understand them.  I chose a flatfile database over a dictionary
because it can be used also by things like sed, awk, grep and can be
easily edited with a text editor if I find my program interface too
unwieldy or, for some reason, inconvenient.  (IOW, if my program turns
into a piece of junk; I still have my data in a useable form :) )

I appreciate the input from all of you; but frankly I'm having trouble
following it in some cases.  And none of it appears to allow me to review
and correct the new address book entry before I "commit" it to the
database.

I probably should have explained better what I was attempting earlier; but
I was trying to be "brief."  I hope this clears things up a little.

Regards,
				- fleet -

On Mon, 18 Nov 2002 alan.gauld@bt.com wrote:

> > I split the functions because it seemed to me I *had* two functions.
> > add() allows input for an address book;
>
> OK, so in that case I'd expect the function look like this:
>
> def add(entry, add_book):....
>
> > get_list() allows me to repeatedly (if necessary)
> > display the list of variables and make corrections
>
> The interesting phrase here is "*the* list", not "a list".
>
> If a function is specific to a single list then it maybe
> shouldn't stand alone - ideally it should be a method of
> a class binding data and function together...
>
> Of course you could make it generic by passing a list
> of names, prompting for a value for each and returning
> a dictionary keyed by name....
>
> def get_values(namelist):
>     values = {}
>     for name in namelist:
>       val = raw_input(name+'? ')
>       values[name] = val
>     return values
>
> But this is limited to returning string values.
> If you passed in a list of name,type tuples then you
> could do the conversion there too but then the usability
> of your function becomes a bit strained...
>
> A better idea might be to get the data required for a
> single entry, in which case the function might be called
> get_entry and reurn the whole group of data as a tuple.
>
> def get_entry():
>    try:
>        ....
>        return (s_name, f_name, phone, etc....)
>    except: return None
>
> That way there is no need to couple the two functions
> together since the add entry function call becomes:
>
> add_entry(get_entry(), addressBook)
>
> Or you have a single while loop outside the
> functions like so:
>
> while 1:
>   entry = get_entry()
>   if not entry: break
>   else add_entry(entry, addressBook)
>
> Untangling the dependencies between functions is good
> design practice (with practice having both meanings here!)
>
> HTH,
>
> Alan g.
> Author of the 'Learning to Program' web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>