Can someone fix my code (Part II)

Joshua Macy amused at webamused.com
Sat Feb 5 01:48:44 EST 2000


Names are created when assigned, but must exist when referenced.
where_temp is local to ssi_or_exit, so it's not in any namespace that
the references to it in ssi can read (remember that attempts to find
names look first to the local namespace, then the global one, then the
builtins).  You either have to pull where_temp out to the module level
(e.g. stick it after import sys and before def ssi_or_exit) which is
ugly and would require you declaring it global within ssi_or_exit and
ssi, or pass it as a parameter (e.g. def ssi_or_exit(where_temp): ), or
make both functions part of a class and where_temp a member of that
class.

I notice that you still have that rather unfortunate recursion going on,
where ssi_or_exit calls ssi which calls ssi_or_exit which calls ssi
which calls ssi_or_exit...and so on.  That's a bad habit to get into,
and could very well cause your code to blow up if you ran it long
enough.  You should probably follow one of the suggestions you were
given to rewrite it as something like:

while 1:
    ssi_or_exit()
    ssi()


Joshua

bovinesoft at my-deja.com wrote:
> 
> When I last posted to this newsgroup, I had a problem and I think I
> fixed it (Thanks to all who helped!).  Now, however, I am having new
> difficulties on my new code.  The error message that I got from the code
> below was: "NameError: where_temp".  If anyone can help me out, I would
> be greatly appreciative.  Thanks again!
> 
> import sys  # to use the exit() function
> 
> def ssi_or_exit():
>    sorx = raw_input("Would you like to generate HTML <g> or exit <x>? ")
>    if sorx == "g":  # if they want to generate a web page
>       body_path = raw_input("Please type the path of the body section:
> ")
>       body = open(body_path, "r")  # open the text of the web page body
>       body2 = body.readlines()  # body2 is the body text variable
>       temp_path = raw_input("Please type the path of the HTML template:
> ")
>       temp = open(temp_path, "r")  # open the HTML template
>       temp2 = temp.readlines()  # temp2 is the template text variable
>       new_path = raw_input("Please type the path of the new HTML file:
> ")
>       new = open(new_path, "w")  # create new file at the specified path
>       where_temp = raw_input("Put the template before <b> or after <a>
> the body? ")
>       ssi()
>    elif sorx == "x":  # if they want to exit
>       sys.exit("Thank you for using Bovine No Server SSI!")
> 
> def ssi():
>    if where_temp == "b":  # for template before body
>       for line in temp2:
>          new.write(line)
>       for line in body2:
>          new.write(line)
>    elif where_temp == "a":  # for body before template
>       for line in body2:
>          new.write(line)
>       for line in temp2:
>          new.write(line)
>    body.close()
>    temp.close()
>    new.close()
>    ssi_or_exit()
> 
> ssi_or_exit()
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.



More information about the Python-list mailing list