def __call__(self, twitter, options):
statusTxt = (" ".join(options['extra_args'])
if options['extra_args']
else str(input("message: ")))
replies = []
ptr = re.compile("@[\w_]+")
while statusTxt:
s = ptr.match(statusTxt)
if s and s.start() == 0:
replies.append(statusTxt[s.start():s.end()])
statusTxt = statusTxt[s.end() + 1:]
else:
break
replies = " ".join(replies)
if len(replies) >= 140:
# just go back
statusTxt = replies
replies = ""
splitted = []
while statusTxt:
limit = 140 - len(replies)
if len(statusTxt) > limit:
end = string.rfind(statusTxt, ' ', 0, limit)
else:
end = limit
splitted.append(" ".join((replies, statusTxt[:end])))
statusTxt = statusTxt[end:]
if options['invert_split']:
splitted.reverse()
for status in splitted:
twitter.statuses.update(status=status)
python类rfind()的实例源码
def __call__(self, twitter, options):
statusTxt = (" ".join(options['extra_args'])
if options['extra_args']
else str(input("message: ")))
replies = []
ptr = re.compile("@[\w_]+")
while statusTxt:
s = ptr.match(statusTxt)
if s and s.start() == 0:
replies.append(statusTxt[s.start():s.end()])
statusTxt = statusTxt[s.end() + 1:]
else:
break
replies = " ".join(replies)
if len(replies) >= 140:
# just go back
statusTxt = replies
replies = ""
splitted = []
while statusTxt:
limit = 140 - len(replies)
if len(statusTxt) > limit:
end = string.rfind(statusTxt, ' ', 0, limit)
else:
end = limit
splitted.append(" ".join((replies, statusTxt[:end])))
statusTxt = statusTxt[end:]
if options['invert_split']:
splitted.reverse()
for status in splitted:
twitter.statuses.update(status=status)
def get_extension(path):
dirsep = string.rfind(path, '/')
dotsep = string.rfind(path, '.')
if dotsep > dirsep:
return path[dotsep + 1:]
else:
return ''
def compFiles(res, expected, base1, base2):
l1 = len(base1)
exp = expected.readlines()
expected.close()
# the "relativisation" is done here
for i in range(len(res)):
j = string.find(res[i],base1)
if (j == 0) or ((j == 2) and (res[i][0:2] == './')):
col = string.find(res[i],':')
if col > 0:
start = string.rfind(res[i][:col], '/')
if start > 0:
res[i] = res[i][start+1:]
for i in range(len(exp)):
j = string.find(exp[i],base2)
if (j == 0) or ((j == 2) and (exp[i][0:2] == './')):
col = string.find(exp[i],':')
if col > 0:
start = string.rfind(exp[i][:col], '/')
if start > 0:
exp[i] = exp[i][start+1:]
ret = 0
# ideally we would like to use difflib functions here to do a
# nice comparison of the two sets. Unfortunately, during testing
# (using python 2.3.3 and 2.3.4) the following code went into
# a dead loop under windows. I'll pursue this later.
# diff = difflib.ndiff(res, exp)
# diff = list(diff)
# for line in diff:
# if line[:2] != ' ':
# print string.strip(line)
# ret = -1
# the following simple compare is fine for when the two data sets
# (actual result vs. expected result) are equal, which should be true for
# us. Unfortunately, if the test fails it's not nice at all.
rl = len(res)
el = len(exp)
if el != rl:
print 'Length of expected is %d, result is %d' % (el, rl)
ret = -1
for i in range(min(el, rl)):
if string.strip(res[i]) != string.strip(exp[i]):
print '+:%s-:%s' % (res[i], exp[i])
ret = -1
if el > rl:
for i in range(rl, el):
print '-:%s' % exp[i]
ret = -1
elif rl > el:
for i in range (el, rl):
print '+:%s' % res[i]
ret = -1
return ret
# Separate threads to handle stdout and stderr are created to run this function
def binput(outarray,fname,packtype=None,writetype='wb'):
"""
Unravels outarray and writes the data to a file, always in LittleEndian
format, along with a header file containing the original data shape. Default
is overwrite the destination file. Tries to figure out packtype from
4th-to-last character in filename. Thus, the routine understands these
file formats ...
1bin=Int8, sbin=int16, ibin=Int32, fbin=Float32, dbin=Float64, etc.
Usage: binput(outarray,filename,packtype=None,writetype='wb')
"""
if not packtype:
packtype = fname[-4]
# a speck of error checking
if packtype == N.int16 and outarray.typecode() == 'f':
# check to see if there's data loss
if max(N.ravel(outarray)) > 32767 or min(N.ravel(outarray))<-32768:
print "*** WARNING: CONVERTING FLOAT DATA TO OUT-OF RANGE int16 DATA"
outdata = N.ravel(outarray).astype(packtype)
# force the data on disk to be LittleEndian (for more efficient PC/Linux use)
if not N.LittleEndian:
outdata = outdata.byteswapped()
outdata = outdata.tostring()
outfile = open(fname,writetype)
outfile.write(outdata)
outfile.close()
# Now, write the header file
try:
suffixindex = string.rfind(fname,'.')
hdrname = fname[0:suffixindex+2]+'hdr' # include .s or .f or .1 or whatever
except ValueError:
hdrname = fname
hdr = outarray.shape
print hdrname
outfile = open(hdrname,'w')
outfile.write(pstat.list2string(hdr))
outfile.close()
return None