[Tutor] while with function

Magnus Lycka magnus@thinkware.se
Mon Nov 18 17:36:02 2002


At 15:42 2002-11-18 -0500, fleet@teachout.org wrote:
>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 howeve=
r
>many times is necessary. When I'm satisfied with the "add" information,
>the variables will be concatenated (with pipe symbol delimiters) and add=
ed
>to an address book file as a single line.

raw_input is very crude.

Another option could be to create a temporary file with
something like:

first_name: Magnus
last_name: Lucka
email: magnus@thinkware.se

and open it in an editor, vi or notepad or whatever,
depending on platform.

Then people make whatever changes they like, and close
the editor. (I'd change Lucka to Lyck=E5.)

After that, you open the file again, parse the text,
and if it makes sense, you store the changed data. That
way the user gets access to reasonable editing
capabilities. Notepad _is_ better than raw_input()

With this approach you can show the entire record (well
how ever much data you like, really) and let the user
change whatever he likes, and leave whatever is ok already.
He can cut and paste, search and replace etc.

 >>> import tempfile
 >>> fn =3D tempfile.mktemp('txt')
 >>> f =3D open(fn, 'wt')
 >>> print >> f, "Hello World"
 >>> f.close()
 >>> import os
 >>> os.system('notepad %s' % fn)

[Now I'm changing the file in notepad]

0
 >>> print open(fn, 'rt').read()
Hello Dear



--=20
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se