[Tutor] Re: Using a dictionary to keep a count

Levy Lazarre levy.lazarre@mfms.com
Wed Aug 6 18:11:59 EDT 2003


On Sat, 02 Aug 2003 21:07:41 +0200,   sigurd@12move.de  wrote:  

>(c) So a function to process log files and print the values could be
>    written like that: 

>def process_log_file(name):
>    d = {}
>    f = open(name)
>    for line in f:
>        script, address = process_line(line)
>        d[address] = d.setdefault(address,0) + 1
>    f.close()
>    for (key, value) in d.items():
>        print key, " => ", value

Thanks for the clarification and suggestion, however this approach doesn't
work for it doesn't create the intended dictionary of dictionaries
structure.
What I needed was the count per script per site, something like:
dic =  {
       '/cgi-bin/script1.cgi' => {
                                       'alpha.umn.edu' => 2,
                                       'rohcs.ats.com' => 2
                                        },
      '/cgi-bin/script2.cgi' => {
                                       'rohcs.ats.com' => 1,
                                       'idg.com' => 1
                                       }
 }

The following approach using exceptions seems to work:
###############################################
dic ={} 

# Read the file line by line and extract the script name and the address.
f = file('access_log')
for line in f :
    script, address = process_line(line)   
    try:             
      dic[script]                       # Has this script already been seen?
    except KeyError:     
      dic[script] = {}                  # first time script is seen, set
empty dict for it 
      
    try:      
      dic[script][address] += 1         # Has this address already been seen
for this script?
                                                  # if so, increment its
count
    except KeyError:    
       dic[script][address] = 1         # first time address seen for this
script, count = 1
f.close()
# Now print the dictionary to verify what we have done:

for key in dic.keys():
    print key, " => "
    for value in dic[key].keys():
        print value+":", dic[key][value]
     
    print "\n"    
#############################################################
Any comments? Is this a bad style? I was trying to make the code shorter
with setdefault(), but it appears very difficult to use with nested
structures.

Thanks,
Levy Lazarre
llazarre@yahoo.com


Confidentiality Notice: 
This is a transmission from a Winter Haven Hospital facility.  This message and any attached documents may be confidential and contain information protected by state and federal medical privacy statutes.  They are intended only for the use of the addressee.  If you are not the intended recipient, any disclosure, copying, or distribution of this information is strictly prohibited.  If you received this transmission in error, please accept our apologies and notify the sender.

This Outbound Message has been scanned for Viruses and objectionable content by McAfee WebShield.






More information about the Tutor mailing list