Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49604d3

Browse files
committed
20% speedup by Fred
1 parent 151fcfd commit 49604d3

2 files changed

Lines changed: 82 additions & 98 deletions

File tree

Doc/partparse.py

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import sys, string, regex, getopt, os
1818

19+
from types import IntType, ListType, StringType, TupleType
20+
1921
# Different parse modes for phase 1
2022
MODE_REGULAR = 0
2123
MODE_VERBATIM = 1
@@ -53,11 +55,10 @@ def lle(lvl, buf, where):
5355

5456
# This class is only needed for _symbolic_ representation of the parse mode.
5557
class Mode:
56-
def init(self, arg):
58+
def __init__(self, arg):
5759
if arg not in the_modes:
5860
raise ValueError, 'mode not in the_modes'
5961
self.mode = arg
60-
return self
6162

6263
def __cmp__(self, other):
6364
if type(self) != type(other):
@@ -83,8 +84,7 @@ def __repr__(self):
8384
raise ValueError, 'mode not in the_modes'
8485

8586
# just a wrapper around a class initialisation
86-
def mode(arg):
87-
return Mode().init(arg)
87+
mode = Mode
8888

8989

9090
# After phase 1, the text consists of chunks, with a certain type
@@ -118,11 +118,10 @@ def mode(arg):
118118

119119
# class, just to display symbolic name
120120
class ChunkType:
121-
def init(self, chunk_type):
121+
def __init__(self, chunk_type):
122122
if chunk_type not in the_types:
123-
raise 'ValueError', 'chunk_type not in the_types'
123+
raise ValueError, 'chunk_type not in the_types'
124124
self.chunk_type = chunk_type
125-
return self
126125

127126
def __cmp__(self, other):
128127
if type(self) != type(other):
@@ -166,32 +165,32 @@ def __repr__(self):
166165
raise ValueError, 'chunk_type not in the_types'
167166

168167
# ...and the wrapper
169-
def chunk_type(type):
170-
return ChunkType().init(type)
168+
_all_chunk_types = {}
169+
for t in the_types:
170+
_all_chunk_types[t] = ChunkType(t)
171+
172+
def chunk_type(t):
173+
return _all_chunk_types[t]
171174

172175
# store a type object of the ChunkType-class-instance...
173176
chunk_type_type = type(chunk_type(0))
174-
177+
175178
# this class contains a part of the parsed buffer
176179
class Chunk:
177-
def init(self, chtype, where, data):
180+
def __init__(self, chtype, where, data):
178181
if type(chtype) != chunk_type_type:
179182
chtype = chunk_type(chtype)
180183
self.chtype = chtype
181-
if type(where) != type(0):
184+
if type(where) != IntType:
182185
raise TypeError, '\'where\' is not a number'
183186
self.where = where
184187
self.data = data
185-
##print 'CHUNK', self
186-
return self
187188

188189
def __repr__(self):
189190
return 'chunk' + `self.chtype, self.where, self.data`
190191

191192
# and the wrapper
192-
def chunk(chtype, where, data):
193-
return Chunk().init(chtype, where, data)
194-
193+
chunk = Chunk
195194

196195

197196
error = 'partparse.error'
@@ -637,14 +636,14 @@ def handlecs(buf, where, curpmode, lvl, result, end):
637636
raise error, `endverbstr` + ' not found.' + lle(lvl, buf, where)
638637
result.append(chunk(ENV, where, (envname, [chunk(PLAIN, newpos, (newpos, pos))])))
639638
newpos = pos + len(endverbstr)
640-
639+
641640
elif s(buf, saveddata) == 'begin':
642641
# start parsing recursively... If that parse returns
643642
# from an '\end{...}', then should the last item of
644643
# the returned data be a string containing the ended
645644
# environment
646645
newpos, data = parseit(buf, curpmode, newpos, lvl)
647-
if not data or type(data[-1]) != type(''):
646+
if not data or type(data[-1]) is not StringType:
648647
raise error, 'missing \'end\'' + lle(lvl, buf, where) + epsilon(buf, newpos)
649648
retenv = data[-1]
650649
del data[-1]
@@ -721,32 +720,25 @@ def handlecs(buf, where, curpmode, lvl, result, end):
721720

722721
# this is just a function to get the string value if the possible data-tuple
723722
def s(buf, data):
724-
if type(data) == type(''):
723+
if type(data) is StringType:
725724
return data
726-
if len(data) != 2 or not (type(data[0]) == type(data[1]) == type(0)):
725+
if len(data) != 2 or not (type(data[0]) is type(data[1]) is IntType):
727726
raise TypeError, 'expected tuple of 2 integers'
728727
x1, x2 = data
729728
return buf[x1:x2]
730-
729+
731730

732731
##length, data1, i = getnextarg(length, buf, pp, i + 1)
733732

734733
# make a deep-copy of some chunks
735734
def crcopy(r):
736-
result = []
737-
for x in r:
738-
result.append(chunkcopy(x))
739-
return result
740-
741-
735+
return map(chunkcopy, r)
736+
742737

743738
# copy a chunk, would better be a method of class Chunk...
744739
def chunkcopy(ch):
745740
if ch.chtype == chunk_type(GROUP):
746-
listc = ch.data[:]
747-
for i in range(len(listc)):
748-
listc[i] = chunkcopy(listc[i])
749-
return chunk(GROUP, ch.where, listc)
741+
return chunk(GROUP, ch.where, map(chunkcopy, ch.data))
750742
else:
751743
return chunk(ch.chtype, ch.where, ch.data)
752744

@@ -755,7 +747,7 @@ def chunkcopy(ch):
755747
# or return Command Sequence token, or give back one character
756748
def getnextarg(length, buf, pp, item):
757749

758-
##wobj = Wobj().init()
750+
##wobj = Wobj()
759751
##dumpit(buf, wobj.write, pp[item:min(length, item + 5)])
760752
##print 'GETNEXTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---'
761753

@@ -773,7 +765,7 @@ def getnextarg(length, buf, pp, item):
773765
pp[item:item] = newpp
774766
item = item + len(newpp)
775767
if len(newpp) < 10:
776-
wobj = Wobj().init()
768+
wobj = Wobj()
777769
dumpit(buf, wobj.write, newpp)
778770
##print 'GETNEXTARG: inserted ' + `wobj.data`
779771
return length, item
@@ -806,7 +798,7 @@ def getnextarg(length, buf, pp, item):
806798
# get a LaTeX-optional argument, you know, the square braces '[' and ']'
807799
def getoptarg(length, buf, pp, item):
808800

809-
wobj = Wobj().init()
801+
wobj = Wobj()
810802
dumpit(buf, wobj.write, pp[item:min(length, item + 5)])
811803
##print 'GETOPTARG, (len, item) =', `length, item` + ' ---> ' + wobj.data + ' <---'
812804

@@ -848,9 +840,8 @@ def getoptarg(length, buf, pp, item):
848840

849841
# Wobj just add write-requests to the ``data'' attribute
850842
class Wobj:
851-
def init(self):
852-
self.data = ''
853-
return self
843+
data = ''
844+
854845
def write(self, data):
855846
self.data = self.data + data
856847

@@ -879,7 +870,7 @@ def write(self, data):
879870
def flattext(buf, pp):
880871
pp = crcopy(pp)
881872
##print '---> FLATTEXT ' + `pp`
882-
wobj = Wobj().init()
873+
wobj = Wobj()
883874

884875
i, length = 0, len(pp)
885876
while 1:
@@ -917,7 +908,7 @@ def flattext(buf, pp):
917908
ch.chtype = chunk_type(PLAIN)
918909
markcmd = s(buf, ch.data)
919910
x = markcmds[markcmd]
920-
if type(x) == type(()):
911+
if type(x) == TupleType:
921912
pre, after = x
922913
str = pre+str+after
923914
elif x == 1:
@@ -1247,7 +1238,7 @@ def changeit(buf, pp):
12471238
ch = pp[i]
12481239
i = i + 1
12491240

1250-
if type(ch) == type(''):
1241+
if type(ch) is StringType:
12511242
#normally, only chunks are present in pp,
12521243
# but in some cases, some extra info
12531244
# has been inserted, e.g., the \end{...} clauses
@@ -1603,7 +1594,7 @@ def changeit(buf, pp):
16031594
pass
16041595

16051596
elif s(buf, ch.data) == 'e':
1606-
# \e --> \
1597+
# "\e" --> "\"
16071598
ch.data = '\\'
16081599
ch.chtype = chunk_type(PLAIN)
16091600
elif (s(buf, ch.data) == 'lineiii') or\
@@ -1614,7 +1605,7 @@ def changeit(buf, pp):
16141605
# a2 [ -- a3]
16151606
#
16161607
##print 'LINEIIIIII!!!!!!!'
1617-
## wobj = Wobj().init()
1608+
## wobj = Wobj()
16181609
## dumpit(buf, wobj.write, pp[i-1:i+5])
16191610
## print '--->' + wobj.data + '<----'
16201611
if not hist.inenv:
@@ -1636,7 +1627,7 @@ def changeit(buf, pp):
16361627
del pp[i:newi]
16371628
length = length - (newi-i)
16381629
## print 'ITEM ARG: --->',
1639-
## wobj = Wobj().init()
1630+
## wobj = Wobj()
16401631
## dumpit(buf, wobj.write, ingroupch)
16411632
## print wobj.data, '<---'
16421633
pp.insert(i, chunk(GROUP, ch.where, ingroupch))
@@ -2061,17 +2052,17 @@ def dumpit(buf, wm, pp):
20612052
(chunk_type(ENDLINE), chunk_type(DENDLINE)) \
20622053
and (pp[i-2].chtype != chunk_type(PLAIN) \
20632054
or s(buf, pp[i-2].data)[-1] != '\n'):
2064-
2055+
20652056
wm('\n')
20662057
wm('@' + s(buf, ch.data))
20672058
if i == length:
20682059
raise error, 'CSLINE expected another chunk'
20692060
if pp[i].chtype != chunk_type(GROUP):
20702061
raise error, 'CSLINE expected GROUP'
2071-
if type(pp[i].data) != type([]):
2062+
if type(pp[i].data) != ListType:
20722063
raise error, 'GROUP chould contain []-data'
2073-
2074-
wobj = Wobj().init()
2064+
2065+
wobj = Wobj()
20752066
dumpit(buf, wobj.write, pp[i].data)
20762067
i = i + 1
20772068
text = wobj.data
@@ -2162,4 +2153,5 @@ def main():
21622153

21632154
outf.close()
21642155

2165-
main()
2156+
if __name__ == "__main__":
2157+
main()

0 commit comments

Comments
 (0)