Q: Why does this work???
Jim Cerra
jc2astro at hotmail.com
Sun Jul 14 21:05:03 EDT 2002
I'm very new to Python, and I've been examing a bit of code... I can't
figure out why the following doesn't produce an error:
# begin code
# ----------------------------
import string
# --- Global variables
total_hits = 0
hits_by_page = {} # maps document URL to hit count for that document
# --- Functions
def read_logfile(filename):
inf = open(filename)
line = inf.readline()
while line != "":
fields = string.split(line)
process_request(fields[0], fields[3], fields[4], fields[5],
fields[6],
fields[7], fields[8], fields[9])
line = inf.readline()
inf.close()
def process_request(host, date, timezone, method, url, protocol, code,
bytes):
global total_hits
if code > "399": return # request was not successful, so don't count it
total_hits = total_hits + 1
url = string.split(url, "?")[0] # remove script arguments, if any
if hits_by_page.has_key(url):
hits_by_page[url] = hits_by_page[url] + 1
else:
hits_by_page[url] = 1
report_top = \
"""
<title>Web server statistics</title>
<h1>Web server statistics</h1>
<table>
<tr><th>Total number of hits: <td>%d
<tr><th>Total number of pages: <td>%d
</table>
"""
def produce_report():
print report_top % (total_hits, len(hits_by_page))
pages = []
for (url, hits) in hits_by_page.items():
pages.append((hits, url))
pages.sort()
pages.reverse()
print "<table>"
print "<tr><th>Page <th>Hits"
for (hits, url) in pages:
print "<tr><td>%s <td>%d" % (url, hits)
print "</table>"
# --- Main program
import sys
if len(sys.argv) != 2: # script name is sys.argv[0]
print "Usage: %s <log file name>" % sys.argv[0]
sys.exit(1)
read_logfile(sys.argv[1])
produce_report()
# ----------------------------
# end code
In the function, process_request(), why isn't the variable, hits_by_page,
declared as a global var? I tried putting in that code (" global
hits_by_page") and there is no effect. The script still works.
My question is Why???
Please excuse any ignorance on my part; Python is not my native programming
language. ;-)
--
Jimmy Cerra
"my mind is slipping away ... day by glorious day" - RG
More information about the Python-list
mailing list