[Zope-Checkins] CVS: Zope2 - DT_In.py:1.43.4.5

fred@digicool.com fred@digicool.com
Wed, 6 Jun 2001 12:54:22 -0400 (EDT)


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

Modified Files:
      Tag: zope-2_3-branch
	DT_In.py 
Log Message:

Performance improvement:  Use isinstance() and saved type objects instead
of calling type() repeatedly on literals.

Use {}.get() instead of {}.has_key()/{}[] to test presence/truth of an
entry in a mapping.  Cache the {}.get() method instead of performing
multiple retrievals in a loop.



--- Updated File DT_In.py in package Zope2 --
--- DT_In.py	2001/04/13 19:30:38	1.43.4.4
+++ DT_In.py	2001/06/06 16:54:22	1.43.4.5
@@ -392,8 +392,15 @@
 from DT_Util import ParseError, parse_params, name_param, str
 from DT_Util import render_blocks, InstanceDict, ValidationError, VSEval, expr_globals
 from string import find, atoi, join, split
+
+try:
+    from strop import join
+except ImportError:
+    pass
+
 import ts_regex
 from DT_InSV import sequence_variables, opt
+StringType=type('')
 TupleType=type(())
 
 class InFactory:
@@ -452,7 +459,7 @@
 
         if has_key('start'):
             v=args['start']
-            if type(v)==type(''):
+            if isinstance(v, StringType):
                 try: atoi(v)
                 except:
                     self.start_name_re=ts_regex.compile(
@@ -491,7 +498,7 @@
             if self.elses: return render_blocks(self.elses, md)
             return ''
 
-        if type(sequence) is type(''):
+        if isinstance(sequence, StringType):
             raise 'InError', (
                 'Strings are not allowed as input to the in tag.')
 
@@ -622,7 +629,7 @@
                             raise ValidationError, index
 
                     kw['sequence-index']=index
-                    if type(client)==TupleType and len(client)==2:
+                    if isinstance(client, TupleType) and len(client)==2:
                         client=client[1]
 
                     if mapping: push(client)
@@ -657,7 +664,7 @@
             if self.elses: return render_blocks(self.elses, md)
             return ''
 
-        if type(sequence) is type(''):
+        if isinstance(sequence, StringType):
             raise 'InError', (
                 'Strings are not allowed as input to the in tag.')
 
@@ -686,6 +693,7 @@
         push=md._push
         pop=md._pop
         render=render_blocks
+        get=self.args.get
 
         if cache: push(cache)
         push(vars)
@@ -701,14 +709,13 @@
                         try: vv=validate(sequence,sequence,None,client,md)
                         except: vv=0
                         if not vv:
-                            if (self.args.has_key('skip_unauthorized') and
-                                self.args['skip_unauthorized']):
+                            if get('skip_unauthorized'):
                                 if index==1: kw['sequence-start']=0
                                 continue
                             raise ValidationError, index
 
                     kw['sequence-index']=index
-                    if type(client)==TupleType and len(client)==2:
+                    if isinstance(client, TupleType) and len(client)==2:
                         client=client[1]
 
                     if mapping: push(client)
@@ -740,7 +747,7 @@
         s=[]
         for client in sequence:
             k = None
-            if type(client)==TupleType and len(client)==2:
+            if isinstance(client, TupleType) and len(client)==2:
                 if isort: k=client[0]
                 v=client[1]
             else:
@@ -786,12 +793,12 @@
 basic_type={type(''): 1, type(0): 1, type(0.0): 1, type(()): 1, type([]): 1,
             type(None) : 1 }.has_key
 
-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