SVN: r25340 - in trunk/quixote: . form2
Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 14 Oct 2004 13:19:26 -0400
| Newsgroups | gmane.comp.web.quixote.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: nascheme
Date: 2004-10-14 13:19:15 -0400 (Thu, 14 Oct 2004)
New Revision: 25340
Modified:
trunk/quixote/form2/widget.py
trunk/quixote/http_request.py
trunk/quixote/http_response.py
trunk/quixote/publish.py
Log:
Use builtin types and isinstance().
Modified: trunk/quixote/form2/widget.py
===================================================================
--- trunk/quixote/form2/widget.py 2004-10-14 17:17:51 UTC (rev 25339)
+++ trunk/quixote/form2/widget.py 2004-10-14 17:19:15 UTC (rev 25340)
@@ -6,7 +6,6 @@
"""
import struct
-from types import FloatType, IntType, ListType, StringType, TupleType
from quixote import get_request
from quixote.html import htmltext, htmlescape, htmltag, TemplateIO, stringify
from quixote.upload import Upload
@@ -368,7 +367,7 @@
values = []
descriptions = []
keys = []
- if type(first) is TupleType:
+ if isinstance(first, tuple):
if len(first) == 2:
for value, description in options:
values.append(value)
@@ -456,7 +455,7 @@
def _parse(self, request):
parsed_key = request.form.get(self.name)
if parsed_key:
- if type(parsed_key) is ListType:
+ if isinstance(parsed_key, list):
self.error = "cannot select multiple values"
else:
self.value = self._parse_single_selection(parsed_key)
@@ -521,7 +520,7 @@
allowed_values = self.get_allowed_values()
if value in allowed_values:
self.value = [ value ]
- elif type(value) in (ListType, TupleType):
+ elif isinstance(value, (list, tuple)):
self.value = [ element
for element in value
if element in allowed_values ] or None
@@ -537,7 +536,7 @@
def _parse(self, request):
parsed_keys = request.form.get(self.name)
if parsed_keys:
- if type(parsed_keys) is ListType:
+ if isinstance(parsed_keys, list):
self.value = [value
for value, description, key in self.options
if key in parsed_keys] or None
@@ -628,7 +627,6 @@
# these class attributes:
TYPE_OBJECT = None # eg. int, float
TYPE_ERROR = None # human-readable error message
- TYPE_CONVERTER = None # eg. int(), float()
def __init__(self, name, value=None, **kwargs):
assert self.__class__ is not NumberWidget, "abstract class"
@@ -642,7 +640,7 @@
StringWidget._parse(self, request)
if self.value is not None:
try:
- self.value = self.TYPE_CONVERTER(self.value)
+ self.value = self.TYPE_OBJECT(self.value)
except ValueError:
self.error = self.TYPE_ERROR
@@ -652,8 +650,7 @@
Instance attributes:
value : float
"""
- TYPE_OBJECT = FloatType
- TYPE_CONVERTER = float
+ TYPE_OBJECT = float
TYPE_ERROR = "must be a number"
@@ -662,8 +659,7 @@
Instance attributes:
value : int
"""
- TYPE_OBJECT = IntType
- TYPE_CONVERTER = int
+ TYPE_OBJECT = int
TYPE_ERROR = "must be an integer"
@@ -695,7 +691,7 @@
def _parse(self, request):
parsed_key = request.form.get(self.name)
if parsed_key:
- if type(parsed_key) is ListType:
+ if isinstance(parsed_key, list):
self.error = "cannot select multiple values"
else:
self.value = self._parse_single_selection(parsed_key)
Modified: trunk/quixote/http_request.py
===================================================================
--- trunk/quixote/http_request.py 2004-10-14 17:17:51 UTC (rev 25339)
+++ trunk/quixote/http_request.py 2004-10-14 17:19:15 UTC (rev 25340)
@@ -28,7 +28,6 @@
import time
import urlparse, urllib
from cgi import FieldStorage
-from types import ListType
from quixote.http_response import HTTPResponse
@@ -133,7 +132,7 @@
def add_form_value(self, key, value):
if self.form.has_key(key):
found = self.form[key]
- if type(found) is ListType:
+ if isinstance(found, list):
found.append(value)
else:
found = [found, value]
Modified: trunk/quixote/http_response.py
===================================================================
--- trunk/quixote/http_response.py 2004-10-14 17:17:51 UTC (rev 25339)
+++ trunk/quixote/http_response.py 2004-10-14 17:19:15 UTC (rev 25340)
@@ -25,7 +25,6 @@
import time
from rfc822 import formatdate
-from types import StringType, IntType
status_reasons = {
100: 'Continue',
@@ -159,7 +158,7 @@
reason phrase for its group of status codes will be used; eg.
if status == 493, the reason for status 400 will be used.
"""
- if type(status) is not IntType:
+ if not isinstance(status, int):
raise TypeError, "status must be an integer"
if not (100 <= status <= 599):
raise ValueError, "status must be between 100 and 599"
@@ -281,7 +280,7 @@
def redirect(self, location, permanent=False):
"""Cause a redirection without raising an error"""
- if not isinstance(location, StringType):
+ if not isinstance(location, str):
raise TypeError, "location must be a string (got %s)" % `location`
# Ensure that location is a full URL
if location.find('://') == -1:
Modified: trunk/quixote/publish.py
===================================================================
--- trunk/quixote/publish.py 2004-10-14 17:17:51 UTC (rev 25339)
+++ trunk/quixote/publish.py 2004-10-14 17:19:15 UTC (rev 25340)
@@ -104,7 +104,7 @@
raise RuntimeError, "only one instance of Publisher allowed"
_publisher = self
- if type(root_namespace) is types.StringType:
+ if isinstance(root_namespace, str):
self.root_namespace = _get_module(root_namespace)
else:
# Should probably check that root_namespace is really a
@@ -743,7 +743,7 @@
if value == name:
internal_name = name
break
- elif type(value) is types.TupleType:
+ elif isinstance(value, tuple):
if value[0] == name:
internal_name = value[1] # internal name is different
break
@@ -791,7 +791,7 @@
else:
# check for an explicit external to internal mapping
for value in container._q_exports:
- if type(value) is types.TupleType:
+ if isinstance(value, tuple):
if value[0] == component:
internal_name = value[1]
break
@@ -892,14 +892,5 @@
return session.user
-if sys.hexversion >= 0x02020000: # Python 2.2 or greater
- def isstring(x):
- return isinstance(x, (basestring, htmltext))
-else:
- if hasattr(types, 'UnicodeType'):
- _string_types = (types.StringType, types.UnicodeType)
- else:
- _string_types = (types.StringType,)
-
- def isstring(x):
- return type(x) in _string_types
+def isstring(x):
+ return isinstance(x, (basestring, htmltext))