[Zope-Checkins] CVS: Zope2 - DT_Util.py:1.71.8.2

fred@digicool.com fred@digicool.com
Wed, 6 Jun 2001 13:18:30 -0400 (EDT)


Update of /cvs-repository/Zope2/lib/python/DocumentTemplate
In directory korak.digicool.com:/tmp/cvs-serv12662

Modified Files:
      Tag: zope-2_3-branch
	DT_Util.py 
Log Message:
Clean up the imports a little.  Do something evil: Use strop.replace()
instead of string.replace() if we can.  This changes a
Python->Python->C call sequence with a Python->C sequence in
html_quote().

html_quote():  Instead of using the find()/split()/join() string
    functions, just use replace().  This is a good speedup.

Use isinstance() and saved type objects consistently instead of
multiple type() calls.



--- Updated File DT_Util.py in package Zope2 --
--- DT_Util.py	2001/01/22 16:35:56	1.71.8.1
+++ DT_Util.py	2001/06/06 17:18:29	1.71.8.2
@@ -85,12 +85,25 @@
 '''$Id$''' 
 __version__='$Revision$'[11:-2]
 
-import regex, string, math, os
-from string import strip, join, atoi, lower, split, find
+import regex, string, math
+import whrandom
 import VSEval
 
-str=__builtins__['str'] # Waaaaa, waaaaaaaa needed for pickling waaaaa
+from __builtin__ import str  # XXX needed for pickling (legacy)
+from string import strip, atoi, lower, replace
 
+# strop.replace() is faster than string.replace() (when they aren't
+# the same), so use it if possible.
+try:
+    from strop import replace
+except ImportError:
+    pass
+
+
+ListType=type([])
+StringType=type('')
+TupleType=type(())
+
 ParseError='Document Template Parse Error'
 ValidationError='Unauthorized'
 
@@ -103,17 +116,17 @@
                        (('"'),    '"'))): #"
         text=str(v)
         for re,name in character_entities:
-            if find(text, re) >= 0: text=join(split(text,re),name)
+            text = replace(text, re, name)
         return text
 
-def int_param(params,md,name,default=0, st=type('')):
+def int_param(params,md,name,default=0, st=StringType):
     try: v=params[name]
     except: v=default
     if v:
         try: v=atoi(v)
         except:
             v=md[v]
-            if type(v) is st: v=atoi(v)
+            if isinstance(v, st): v=atoi(v)
     return v or 0
 
 _marker=[]
@@ -160,7 +173,7 @@
 def careful_getitem(md, mapping, key):
     v=mapping[key]
 
-    if type(v) is type(''): return v # Short-circuit common case
+    if isinstance(v, StringType): return v # Short-circuit common case
 
     validate=md.validate
     if validate is None or validate(mapping,mapping,None,v,md): return v
@@ -174,7 +187,7 @@
         v=seq[indexes[0]:]
     else: v=seq[:]
 
-    if type(seq) is type(''): return v # Short-circuit common case
+    if isinstance(seq, StringType): return v # Short-circuit common case
 
     validate=md.validate
     if validate is not None:
@@ -201,7 +214,6 @@
     if iLen >= RANGELIMIT: raise ValueError, 'range() too large'
     return range(iStart, iEnd, iStep)
 
-import string, math, whrandom
 
 try:
     import ExtensionClass
@@ -283,23 +295,23 @@
 def reorder(self, s, with=None, without=()):
     if with is None: with=s
     d={}
-    tt=type(())
+    tt=TupleType
     for i in s:
-        if type(i) is tt and len(i)==2: k, v = i
-        else:                           k= v = i
+        if isinstance(i, tt) and len(i)==2: k, v = i
+        else:                               k= v = i
         d[k]=v
     r=[]
     a=r.append
     h=d.has_key
 
     for i in without:
-        if type(i) is tt and len(i)==2: k, v = i
-        else:                           k= v = i
+        if isinstance(i, tt) and len(i)==2: k, v = i
+        else:                               k= v = i
         if h(k): del d[k]
         
     for i in with:
-        if type(i) is tt and len(i)==2: k, v = i
-        else:                           k= v = i
+        if isinstance(i, tt) and len(i)==2: k, v = i
+        else:                               k= v = i
         if h(k):
             a((k,d[k]))
             del d[k]
@@ -445,7 +457,6 @@
 
 """
 
-ListType=type([])
 def parse_params(text,
                  result=None,
                  tag='',
@@ -521,7 +532,7 @@
 
     if result.has_key(name):
         p=parms[name]
-        if type(p) is not ListType or p:
+        if not isinstance(p, ListType) or p:
             raise ParseError, (
                 'Duplicate values for attribute "%s"' % name, tag)