[mod_python] util.FieldStorage

Brian Bird Brian.Bird at securetrading.com
Thu Oct 26 05:14:26 EDT 2006


Is there anything in the works to make util.FieldStorage act more like a
real dictionary? Specifically, it doesn't support the __setitem__,
__delitem__ methods. Since the documentation states "it can be treated
like a dictionary" I was surprised to find these methods missing.

 

I know it's easy to change it to a real dictionary to get these methods,
but then you lose the (very useful) getlist() method. At first glance it
doesn't look like much work to add the extra methods (but I haven't
looked at how it may affect values which aren't StringField's).

 

I've just looked in the latest subversion and found that the items()
method is now in util.FieldStorage, as well as a method add_field()
which looks suspiciously like __setitem__ should, but no sign of a
__delitem__. However, there now seems to be a self.dictionary object
which could be useful - perhaps all unrecognised methods should be
passed onto self.dictionary in the __getattr__ instead of raising an
AttributeError?

 

I've put below what I thought needed adding to the util.FieldStorage
class before I noticed self.dictionary in case it's any help.

 

### Disclaimer - completely untested! :-) ###

 

def __setitem__(self, key, value):

    self.list.append(util.Field(key, cStringIO.StringIO(key),
"text/plain", {}, None, {}))

 

def __delitem__(self, key):

    for item in self.list:

        if item.name == key:

            self.list.remove(item)

 

def items(self):

    result = []

    for key in self.keys():

        result.append((key, self[key]))

    return result

 

def update(self, d):

    for k in d.keys():

        self[k] = d[k]

 

 

Thanks,

 

Brian

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20061026/29be8f68/attachment.html


More information about the Mod_python mailing list