More efficient FieldStorage, StringField and Field classes
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Web Servers reviews > Apache Server configuration support > Apache Mod-Python > More efficient FieldStorage, StringField and Field classes




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    More efficient FieldStorage, StringField and Field classes  
Mike Looijmans


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 07:46 AM

Inspired by our FieldStorage work with file uploads, I also made some
implementation changes to Field and StringField in util.py. In essense,
that will make things like using form['key'] multiple times much more
efficient. Also, FieldStorage will return the same object if called twice.

I'd like some feedback from the group on these changes, and also some
directions on what I'd need to do to get them integrated in the
mod_python beta. I have no experience whatsoever in contributing to open
source projects.

Major change is the Field and FieldStorage implementation:

class Field:

filename = None
headers = {}

def __init__(self, name):
self.name = name

def __repr__(self):
"""Return printable representation."""
return "Field(%s, %s)" % (`self.name`, `self.value`)

def __getattr__(self, name):
if name != 'value':
raise AttributeError, name
if self.file:
self.file.seek(0)
self.value = self.file.read()
self.file.seek(0)
else:
self.value = None
return self.value

def __del__(self):
self.file.close()

class StringField(str):
""" This class is basically a string with
added attributes for compatibility with std lib cgi.py. Basically, this
works the opposite of Field, as it stores its data in a string, but
creates
a file on demand. Field creates a value on demand and stores data
in a file.
"""
filename = None
headers = {}
ctype = "text/plain"
type_options = {}
disposition = None
disp_options = None

# I wanted __init__(name, value) but that does not work
(apparently, you
# cannot subclass str with a constructor that takes >1 argument)
def __init__(self, value):
'''Create StringField instance. You'll have to set name
yourself.'''
str.__init__(self, value)
self.value = value

def __getattr__(self, name):
if name != 'file':
raise AttributeError, name
self.file = cStringIO.StringIO(self.value)
return self.file

I've attached a working util.py (based on 3.1.4, so there are also the
changes to allow large uploads in the file).







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:48 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register