remove old fashioned string usage py33
authorrobin
Tue, 15 Oct 2013 14:55:31 +0100
branchpy33
changeset 3782 bb8cb5194b0f
parent 3781 df8b57380768
child 3783 b4d048dad7e6
remove old fashioned string usage
src/reportlab/lib/rparsexml.py
src/reportlab/platypus/xpreformatted.py
tools/docco/codegrab.py
tools/docco/yaml.py
--- a/src/reportlab/lib/rparsexml.py	Tue Oct 15 14:53:40 2013 +0100
+++ b/src/reportlab/lib/rparsexml.py	Tue Oct 15 14:55:31 2013 +0100
@@ -55,7 +55,6 @@
 
 RequirePyRXP = 0        # set this to 1 to disable the nonvalidating fallback parser.
 
-import string
 try:
     #raise ImportError, "dummy error"
     simpleparse = 0
@@ -88,11 +87,10 @@
 #replacelist = []
 def unEscapeContentList(contentList):
     result = []
-    from string import replace
     for e in contentList:
         if "&" in e:
             for (old, new) in replacelist:
-                e = replace(e, old, new)
+                e = e.replace(old, new)
         result.append(e)
     return result
 
@@ -118,12 +116,11 @@
 def skip_prologue(text, cursor):
     """skip any prologue found after cursor, return index of rest of text"""
     ### NOT AT ALL COMPLETE!!! definitely can be confused!!!
-    from string import find
     prologue_elements = ("!DOCTYPE", "?xml", "!--")
     done = None
     while done is None:
         #print "trying to skip:", repr(text[cursor:cursor+20])
-        openbracket = find(text, "<", cursor)
+        openbracket = text.find("<", cursor)
         if openbracket<0: break
         past = openbracket+1
         found = None
@@ -131,7 +128,7 @@
             le = len(e)
             if text[past:past+le]==e:
                 found = 1
-                cursor = find(text, ">", past)
+                cursor = text.find(">", past)
                 if cursor<0:
                     raise ValueError("can't close prologue %r" % e)
                 cursor = cursor+1
@@ -142,14 +139,13 @@
 
 def parsexml0(xmltext, startingat=0, toplevel=1,
         # snarf in some globals
-        strip=string.strip, split=string.split, find=string.find, entityReplacer=unEscapeContentList,
+        entityReplacer=unEscapeContentList,
         #len=len, None=None
         #LENCDATAMARKER=LENCDATAMARKER, CDATAMARKER=CDATAMARKER
         ):
     """simple recursive descent xml parser...
        return (dictionary, endcharacter)
        special case: comment returns (None, endcharacter)"""
-    #from string import strip, split, find
     #print "parsexml0", repr(xmltext[startingat: startingat+10])
     # DEFAULTS
     NameString = NONAME
@@ -158,13 +154,13 @@
         #if verbose: print "at top level"
         #if startingat!=0:
         #    raise ValueError, "have to start at 0 for top level!"
-        xmltext = strip(xmltext)
+        xmltext = xmltext.strip()
     cursor = startingat
     #look for interesting starting points
-    firstbracket = find(xmltext, "<", cursor)
+    firstbracket = xmltext.find("<", cursor)
     afterbracket2char = xmltext[firstbracket+1:firstbracket+3]
     #print "a", repr(afterbracket2char)
-    #firstampersand = find(xmltext, "&", cursor)
+    #firstampersand = xmltext.find("&", cursor)
     #if firstampersand>0 and firstampersand<firstbracket:
     #    raise ValueError, "I don't handle ampersands yet!!!"
     docontents = 1
@@ -196,7 +192,7 @@
             #print "in CDATA", cursor
             # skip straight to the close marker
             startcdata = firstbracket+9
-            endcdata = find(xmltext, CDATAENDMARKER, startcdata)
+            endcdata = xmltext.find(CDATAENDMARKER, startcdata)
             if endcdata<0:
                 raise ValueError("unclosed CDATA %s" % repr(xmltext[cursor:cursor+20]))
             NameString = CDATAMARKER
@@ -206,7 +202,7 @@
     # special case COMMENT
     elif afterbracket2char=="!-" and xmltext[firstbracket:firstbracket+4]=="<!--":
             #print "in COMMENT"
-            endcommentdashes = find(xmltext, "--", firstbracket+4)
+            endcommentdashes = xmltext.find("--", firstbracket+4)
             if endcommentdashes<firstbracket:
                 raise ValueError("unterminated comment %s" % repr(xmltext[cursor:cursor+20]))
             endcomment = endcommentdashes+2
@@ -217,7 +213,7 @@
             # get the rest of the tag
             #if verbose: print "parsing start tag"
             # make sure the tag isn't in doublequote pairs
-            closebracket = find(xmltext, ">", firstbracket)
+            closebracket = xmltext.find(">", firstbracket)
             noclose = closebracket<0
             startsearch = closebracket+1
             pastfirstbracket = firstbracket+1
@@ -229,7 +225,7 @@
                     #print "simple case", tagcontent
                     tagcontent = tagcontent[:-1]
                     docontents = None
-                name = strip(tagcontent)
+                name = tagcontent.strip()
                 NameString = name
                 cursor = startsearch
             else:
@@ -237,15 +233,15 @@
                     # check double quotes
                     stop = None
                     # not inside double quotes! (the split should have odd length)
-                    if noclose or len(split(tagcontent+".", '"'))% 2:
+                    if noclose or len((tagcontent+".").split('"'))% 2:
                         stop=1
                     while stop is None:
-                        closebracket = find(xmltext, ">", startsearch)
+                        closebracket = xmltext.find(">", startsearch)
                         startsearch = closebracket+1
                         noclose = closebracket<0
                         tagcontent = xmltext[pastfirstbracket:closebracket]
                         # not inside double quotes! (the split should have odd length)
-                        if noclose or len(split(tagcontent+".", '"'))% 2:
+                        if noclose or len((tagcontent+".").split('"'))% 2:
                             stop=1
                 if noclose:
                     raise ValueError("unclosed start tag %s" % repr(xmltext[firstbracket:firstbracket+20]))
@@ -258,12 +254,12 @@
                     tagcontent = tagcontent[:-1]
                     docontents = None
                 #tagcontent = xmltext[firstbracket+1:closebracket]
-                tagcontent = strip(tagcontent)
-                taglist = split(tagcontent, "=")
+                tagcontent = tagcontent.strip()
+                taglist = tagcontent.split("=")
                 #if not taglist:
                 #    raise ValueError, "tag with no name %s" % repr(xmltext[firstbracket:firstbracket+20])
                 taglist0 = taglist[0]
-                taglist0list = split(taglist0)
+                taglist0list = taglist0.split()
                 #if len(taglist0list)>2:
                 #    raise ValueError, "bad tag head %s" % repr(taglist0)
                 name = taglist0list[0]
@@ -281,7 +277,7 @@
                     #print "looking for attribute named", attributename
                     attentry = taglist[taglistindex]
                     taglistindex = taglistindex+1
-                    attentry = strip(attentry)
+                    attentry = attentry.strip()
                     if attentry[0]!='"':
                         raise ValueError("attribute value must start with double quotes" + repr(attentry))
                     while '"' not in attentry[1:]:
@@ -291,11 +287,11 @@
                         nextattentry = taglist[taglistindex]
                         taglistindex = taglistindex+1
                         attentry = "%s=%s" % (attentry, nextattentry)
-                    attentry = strip(attentry) # only needed for while loop...
-                    attlist = split(attentry)
+                    attentry = attentry.strip() # only needed for while loop...
+                    attlist = attentry.split()
                     nextattname = attlist[-1]
                     attvalue = attentry[:-len(nextattname)]
-                    attvalue = strip(attvalue)
+                    attvalue = attvalue.strip()
                     try:
                         first = attvalue[0]; last=attvalue[-1]
                     except:
@@ -310,7 +306,7 @@
         #print "now looking for end tag"
         ContentList = L
     while docontents is not None:
-            nextopenbracket = find(xmltext, "<", cursor)
+            nextopenbracket = xmltext.find("<", cursor)
             if nextopenbracket<cursor:
                 #if verbose: print "no next open bracket found"
                 if name==NONAME:
@@ -325,11 +321,11 @@
             # is it a close bracket?
             elif xmltext[nextopenbracket+1]=="/":
                 #print "found close bracket", repr(xmltext[nextopenbracket:nextopenbracket+20])
-                nextclosebracket = find(xmltext, ">", nextopenbracket)
+                nextclosebracket = xmltext.find(">", nextopenbracket)
                 if nextclosebracket<nextopenbracket:
                     raise ValueError("unclosed close tag %s" % repr(xmltext[nextopenbracket: nextopenbracket+20]))
                 closetagcontents = xmltext[nextopenbracket+2: nextclosebracket]
-                closetaglist = split(closetagcontents)
+                closetaglist = closetagcontents.split()
                 #if len(closetaglist)!=1:
                     #print closetagcontents
                     #raise ValueError, "bad close tag format %s" % repr(xmltext[nextopenbracket: nextopenbracket+20])
@@ -338,9 +334,9 @@
                 #if verbose: print "closetag name is", closename
                 if name!=closename:
                     prefix = xmltext[:cursor]
-                    endlinenum = len(split(prefix, "\n"))
+                    endlinenum = len(prefix.split("\n"))
                     prefix = xmltext[:startingat]
-                    linenum = len(split(prefix, "\n"))
+                    linenum = len(prefix.split("\n"))
                     raise ValueError("at lines %s...%s close tag name doesn't match %s...%s %s" %(
                        linenum, endlinenum, repr(name), repr(closename), repr(xmltext[cursor: cursor+100])))
                 remainder = xmltext[cursor:nextopenbracket]
@@ -364,7 +360,7 @@
                     L.append(parsetree)
         # maybe should check for trailing garbage?
         # toplevel:
-        #    remainder = strip(xmltext[cursor:])
+        #    remainder = xmltext[cursor:].strip()
         #    if remainder:
         #        raise ValueError, "trailing garbage at top level %s" % repr(remainder[:20])
     if ContentList:
@@ -380,23 +376,22 @@
         return parsedxml
     (name, attdict, textlist, extra) = parsedxml
     if not attdict: attdict={}
-    join = string.join
     attlist = []
     for k in attdict.keys():
         v = attdict[k]
         attlist.append("%s=%s" % (k, repr(v)))
-    attributes = join(attlist, " ")
+    attributes = " ".join(attlist)
     if not name and attributes:
         raise ValueError("name missing with attributes???")
     if textlist is not None:
         # with content
         textlistpprint = list(map(pprettyprint, textlist))
-        textpprint = join(textlistpprint, "\n")
+        textpprint = "\n".join(textlistpprint)
         if not name:
             return textpprint # no outer tag
         # indent it
-        nllist = string.split(textpprint, "\n")
-        textpprint = "   "+join(nllist, "\n   ")
+        nllist = textpprint.split("\n")
+        textpprint = "   "+ ("\n   ".join(nllist))
         return "<%s %s>\n%s\n</%s>" % (name, attributes, textpprint, name)
     # otherwise must be a simple tag
     return "<%s %s/>" % (name, attributes)
--- a/src/reportlab/platypus/xpreformatted.py	Tue Oct 15 14:53:40 2013 +0100
+++ b/src/reportlab/platypus/xpreformatted.py	Tue Oct 15 14:55:31 2013 +0100
@@ -3,11 +3,10 @@
 #history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/xpreformatted.py
 __version__=''' $Id$ '''
 __doc__='''A 'rich preformatted text' widget allowing internal markup'''
-import string
 from reportlab.lib import PyFontify
-from .paragraph import Paragraph, cleanBlockQuotedText, _handleBulletWidth, \
-     ParaLines, _getFragWords, stringWidth, _sameFrag, getAscentDescent, imgVRange, imgNormV
-from .flowables import _dedenter
+from reportlab.platypus.paragraph import Paragraph, cleanBlockQuotedText, _handleBulletWidth, \
+     ParaLines, _getFragWords, stringWidth, getAscentDescent, imgVRange, imgNormV
+from reportlab.platypus.flowables import _dedenter
 
 def _getFragLines(frags):
     lines = []
@@ -17,7 +16,7 @@
         w = W[0]
         t = w.text
         del W[0]
-        i = string.find(t,'\n')
+        i = t.find('\n')
         if i>=0:
             tleft = t[i+1:]
             cline.append(w.clone(text=t[:i]))
@@ -40,11 +39,11 @@
 
 # Will be removed shortly.
 def _countSpaces(text):
-    return string.count(text, ' ')
+    return text.count(' ')
 ##  i = 0
 ##  s = 0
 ##  while 1:
-##      j = string.find(text,' ',i)
+##      j = text.find(' ',i)
 ##      if j<0: return s
 ##      s = s + 1
 ##      i = j + 1
@@ -68,7 +67,7 @@
         n = n + stringWidth(text, f.fontName, f.fontSize)
 
         #s = s + _countSpaces(text)
-        s = s + string.count(text, ' ') # much faster for many blanks
+        s = s + text.count(' ') # much faster for many blanks
 
         #del f.text # we can't do this until we sort out splitting
                     # of paragraphs
@@ -77,7 +76,7 @@
 class XPreformatted(Paragraph):
     def __init__(self, text, style, bulletText = None, frags=None, caseSensitive=1, dedent=0):
         self.caseSensitive = caseSensitive
-        cleaner = lambda text, dedent=dedent: string.join(_dedenter(text or '',dedent),'\n')
+        cleaner = lambda text, dedent=dedent: '\n'.join(_dedenter(text or '',dedent))
         self._setup(text, style, bulletText, frags, cleaner)
 
     def breakLines(self, width):
@@ -136,12 +135,12 @@
                 fontName = f.fontName
                 ascent, descent = getAscentDescent(fontName,fontSize)
                 kind = 0
-                L=string.split(f.text, '\n')
+                L=f.text.split('\n')
                 for l in L:
                     currentWidth = stringWidth(l,fontName,fontSize)
                     requiredWidth = max(currentWidth,requiredWidth)
                     extraSpace = maxWidth-currentWidth
-                    lines.append((extraSpace,string.split(l,' '),currentWidth))
+                    lines.append((extraSpace,l.split(' '),currentWidth))
                     lineno = lineno+1
                     maxWidth = lineno<len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
                 blPara = f.clone(kind=kind, lines=lines,ascent=ascent,descent=descent,fontSize=fontSize)
@@ -222,9 +221,9 @@
         XPreformatted.__init__(self, text, style,bulletText=bulletText,dedent=dedent,frags=frags)
 
     def escapeHtml(self, text):
-        s = string.replace(text, '&', '&amp;')
-        s = string.replace(s, '<', '&lt;')
-        s = string.replace(s, '>', '&gt;')
+        s = text.replace('&', '&amp;')
+        s = s.replace('<', '&lt;')
+        s = s.replace('>', '&gt;')
         return s
 
     def fontify(self, code):
--- a/tools/docco/codegrab.py	Tue Oct 15 14:53:40 2013 +0100
+++ b/tools/docco/codegrab.py	Tue Oct 15 14:55:31 2013 +0100
@@ -29,7 +29,7 @@
 
     #might be a package.  If so, check the top level
     #package is there, then recalculate the path needed
-    words = string.split(modulename, '.')
+    words = modulename.split('.')
     if len(words) > 1:
         packagename = words[0]
         packagefound = imp.find_module(packagename, searchpath)
@@ -52,7 +52,7 @@
 
     #grab the code too, minus trailing newlines
     lines = open(pathname, 'r').readlines()
-    lines = list(map(string.rstrip, lines))
+    lines = list(map(str.rstrip, lines))
 
     result = Struct()
     result.functions = []
@@ -121,7 +121,7 @@
                         meth.proto = getFunctionPrototype(value2, lines)
                         if name2!=key2:
                             meth.doc = 'pointer to '+name2
-                            meth.proto = string.replace(meth.proto,name2,key2)
+                            meth.proto = meth.proto.replace(name2,key2)
                         else:
                             if value2.__doc__:
                                 meth.doc = dedent(value2.__doc__)
@@ -157,16 +157,16 @@
             lineNo = lineNo + 1
 
     usefulLines = lines[firstLineNo:lineNo+1]
-    return string.join(usefulLines, '\n')
+    return '\n'.join(usefulLines)
 
 
 def dedent(comment):
     """Attempts to dedent the lines to the edge. Looks at no.
     of leading spaces in line 2, and removes up to that number
     of blanks from other lines."""
-    commentLines = string.split(comment, '\n')
+    commentLines = comment.split('\n')
     if len(commentLines) < 2:
-        cleaned = list(map(string.lstrip, commentLines))
+        cleaned = list(map(str.lstrip, commentLines))
     else:
         spc = 0
         for char in commentLines[1]:
@@ -181,7 +181,7 @@
                 if line[0] in string.whitespace:
                     line = line[1:]
             cleaned.append(line)
-    return string.join(cleaned, '\n')
+    return '\n'.join(cleaned)
 
 
 
--- a/tools/docco/yaml.py	Tue Oct 15 14:53:40 2013 +0100
+++ b/tools/docco/yaml.py	Tue Oct 15 14:55:31 2013 +0100
@@ -39,7 +39,6 @@
 
 
 import sys
-import string
 import imp
 from . import codegrab
 
@@ -73,7 +72,7 @@
     def readLine(self, line):
         #this is the inner loop
         self._lineNo = self._lineNo + 1
-        stripped = string.lstrip(line)
+        stripped = line.lstrip()
         if len(stripped) == 0:
             if self._mode == PLAIN:
                 self.endPara()
@@ -82,7 +81,7 @@
         elif line[0]=='.':
             # we have a command of some kind
             self.endPara()
-            words = string.split(stripped[1:])
+            words = stripped[1:].split()
             cmd, args = words[0], words[1:]
 
             #is it a parser method?
@@ -98,7 +97,7 @@
                 # assume it is a paragraph style -
                 # becomes the formatter's problem
                 self.endPara()  #end the last one
-                words = string.split(stripped, ' ', 1)
+                words = stripped.split(' ', 1)
                 assert len(words)==2, "Style %s but no data at line %d" % (words[0], self._lineNo)
                 (styletag, data) = words
                 self._style = styletag[1:]
@@ -110,12 +109,12 @@
     def endPara(self):
         #ends the current paragraph, or preformatted block
 
-        text = string.join(self._buf, ' ')
+        text = ' '.join(self._buf)
         if text:
             if self._mode == PREFORMATTED:
                 #item 3 is list of lines
                 self._results.append(('Preformatted', self._style,
-                                 string.join(self._buf,'\n')))
+                                 '\n'.join(self._buf)))
             else:
                 self._results.append(('Paragraph', self._style, text))
         self._buf = []