[Tutor] problem with mmap
Ertl, John C CIV 63134
john.ertl at navy.mil
Wed May 30 19:35:57 CEST 2007
All,
I am trying to work an example that I found at http://bitworking.org/news/132/REST-Tips-URI-space-is-infinite
When I try the code below I get an error and I am not able to figure it out.
Thanks for any and all help. I have two png and a text file (sortzip.txt) in the same dir as this code but I do not even get to that part...the mmap call is bad. I have never used mmap before so this is new to me. I am running python 2.4
John
############# code
from mmap import mmap
import os
from bisect import bisect_left
import sys
class Zipcodes(object):
"""Use mmap to treat the sorted file of zipcodes
as an array"""
def __init__(self):
self.f = open("sortzips.txt", "r+")
self.size = os.path.getsize("sortzips.txt")
self.m = mmap(self.f.fileno(), self.size)
def __getitem__(self, i):
self.m.seek(6*i)
return self.m.read(5)
def __del__(self):
self.m.close()
self.f.close()
def __len__(self):
return self.size / 6
zipcodes = Zipcodes()
target = os.environ.get('PATH_INFO', '/')[1:]
found = ( zipcodes[bisect_left(zipcodes, target)] == target )
print "Status: " + ( found and "200 Ok" or "404 Not Found" )
print "Cache-control: max-age=172800"
print "Content-type: image/png"
print ""
file = open(found and "good.png" or "bad.png", "r")
png = file.read()
file.close()
sys.stdout.write(png)
##########################
error message when I try to run this.
Traceback (most recent call last):
File "./zipcode.cgi", line 23, in ?
zipcodes = Zipcodes()
File "./zipcode.cgi", line 14, in __init__
self.m = mmap(self.f.fileno(), self.size)
EnvironmentError: [Errno 22] Invalid argument
Exception exceptions.AttributeError: "'Zipcodes' object has no attribute 'm'" in <bound method Zipcodes.__del__ of <__main__.Zipcodes object at 0xbdf080cc>> ignored
###########################
John Ertl
Meteorologist
FNMOC
7 Grace Hopper Ave.
Monterey, CA 93943
(831) 656-5704
john.ertl at navy.mil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 6578 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20070530/3479d7ff/attachment-0001.bin
More information about the Tutor
mailing list