Class FieldStorage: add two new methods, getfirst() and getlist(),

that provide a somewhat more uniform interface to getting values.

This is from SF patch #453691.
This commit is contained in:
Guido van Rossum 2001-09-05 19:45:34 +00:00
parent 09f1ad8542
commit 1bfb388d86
1 changed files with 22 additions and 0 deletions

View File

@ -564,6 +564,28 @@ class FieldStorage:
else:
return default
def getfirst(self, key, default=None):
""" Return the first value received."""
if self.has_key(key):
value = self[key]
if type(value) is type([]):
return value[0].value
else:
return value.value
else:
return default
def getlist(self, key):
""" Return list of received values."""
if self.has_key(key):
value = self[key]
if type(value) is type([]):
return map(lambda v: v.value, value)
else:
return [value.value]
else:
return []
def keys(self):
"""Dictionary style keys() method."""
if self.list is None: