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

Skip to content

Commit 65230a2

Browse files
committed
Remove uses of the string and types modules:
x in string.whitespace => x.isspace() type(x) in types.StringTypes => isinstance(x, basestring) isinstance(x, types.StringTypes) => isinstance(x, basestring) type(x) is types.StringType => isinstance(x, str) type(x) == types.StringType => isinstance(x, str) string.split(x, ...) => x.split(...) string.join(x, y) => y.join(x) string.zfill(x, ...) => x.zfill(...) string.count(x, ...) => x.count(...) hasattr(types, "UnicodeType") => try: unicode except NameError: type(x) != types.TupleTuple => not isinstance(x, tuple) isinstance(x, types.TupleType) => isinstance(x, tuple) type(x) is types.IntType => isinstance(x, int) Do not mention the string module in the rlcompleter docstring. This partially applies SF patch http://www.python.org/sf/562373 (with basestring instead of string). (It excludes the changes to unittest.py and does not change the os.stat stuff.)
1 parent a401ae4 commit 65230a2

15 files changed

Lines changed: 43 additions & 71 deletions

Lib/ConfigParser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
write the configuration state in .ini format
8484
"""
8585

86-
import string, types
86+
import types
8787
import re
8888

8989
__all__ = ["NoSectionError","DuplicateSectionError","NoOptionError",
@@ -223,7 +223,7 @@ def read(self, filenames):
223223
configuration files in the list will be read. A single
224224
filename may also be given.
225225
"""
226-
if type(filenames) in types.StringTypes:
226+
if isinstance(filenames, basestring):
227227
filenames = [filenames]
228228
for filename in filenames:
229229
try:
@@ -454,7 +454,7 @@ def __read(self, fp, fpname):
454454
# ';' is a comment delimiter only if it follows
455455
# a spacing character
456456
pos = optval.find(';')
457-
if pos and optval[pos-1] in string.whitespace:
457+
if pos and optval[pos-1].isspace():
458458
optval = optval[:pos]
459459
optval = optval.strip()
460460
# allow empty values

Lib/StringIO.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
bytes that occupy space in the buffer.
2929
- There's a simple test set (see end of this file).
3030
"""
31-
import types
3231
try:
3332
from errno import EINVAL
3433
except ImportError:
@@ -50,7 +49,7 @@ class StringIO:
5049
"""
5150
def __init__(self, buf = ''):
5251
# Force self.buf to be a string or unicode
53-
if not isinstance(buf, types.StringTypes):
52+
if not isinstance(buf, basestring):
5453
buf = str(buf)
5554
self.buf = buf
5655
self.len = len(buf)
@@ -151,7 +150,7 @@ def write(self, s):
151150
raise ValueError, "I/O operation on closed file"
152151
if not s: return
153152
# Force s to be a string or unicode
154-
if not isinstance(s, types.StringTypes):
153+
if not isinstance(s, basestring):
155154
s = str(s)
156155
if self.pos > self.len:
157156
self.buflist.append('\0'*(self.pos - self.len))

Lib/formatter.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
manage and inserting data into the output.
1919
"""
2020

21-
import string
2221
import sys
23-
from types import StringType
2422

2523

2624
AS_IS = None
@@ -119,7 +117,7 @@ def add_label_data(self, format, counter, blankline = None):
119117
self.writer.send_line_break()
120118
if not self.para_end:
121119
self.writer.send_paragraph((blankline and 1) or 0)
122-
if type(format) is StringType:
120+
if isinstance(format, str):
123121
self.writer.send_label_data(self.format_counter(format, counter))
124122
else:
125123
self.writer.send_label_data(format)
@@ -176,16 +174,13 @@ def format_roman(self, case, counter):
176174
return label.upper()
177175
return label
178176

179-
def add_flowing_data(self, data,
180-
# These are only here to load them into locals:
181-
whitespace = string.whitespace,
182-
join = string.join, split = string.split):
177+
def add_flowing_data(self, data):
183178
if not data: return
184179
# The following looks a bit convoluted but is a great improvement over
185180
# data = regsub.gsub('[' + string.whitespace + ']+', ' ', data)
186-
prespace = data[:1] in whitespace
187-
postspace = data[-1:] in whitespace
188-
data = join(split(data))
181+
prespace = data[:1].isspace()
182+
postspace = data[-1:].isspace()
183+
data = " ".join(data.split())
189184
if self.nospace and not data:
190185
return
191186
elif prespace or self.softspace:
@@ -411,7 +406,7 @@ def send_literal_data(self, data):
411406

412407
def send_flowing_data(self, data):
413408
if not data: return
414-
atbreak = self.atbreak or data[0] in string.whitespace
409+
atbreak = self.atbreak or data[0].isspace()
415410
col = self.col
416411
maxcol = self.maxcol
417412
write = self.file.write
@@ -427,7 +422,7 @@ def send_flowing_data(self, data):
427422
col = col + len(word)
428423
atbreak = 1
429424
self.col = col
430-
self.atbreak = data[-1] in string.whitespace
425+
self.atbreak = data[-1].isspace()
431426

432427

433428
def test(file = None):

Lib/hmac.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
Implements the HMAC algorithm as described by RFC 2104.
44
"""
55

6-
import string
7-
86
def _strxor(s1, s2):
97
"""Utility method. XOR the two strings s1 and s2 (must have same length).
108
"""
@@ -82,7 +80,7 @@ def digest(self):
8280
def hexdigest(self):
8381
"""Like digest(), but returns a string of hexadecimal digits instead.
8482
"""
85-
return "".join([string.zfill(hex(ord(x))[2:], 2)
83+
return "".join([hex(ord(x))[2:].zfill(2)
8684
for x in tuple(self.digest())])
8785

8886
def new(key, msg = None, digestmod = None):

Lib/markupbase.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Shared support for scanning document type declarations in HTML and XHTML."""
22

33
import re
4-
import string
54

65
_declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match
76
_declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match
@@ -151,7 +150,7 @@ def _parse_doctype_subset(self, i, declstartpos):
151150
j = j + 1
152151
elif c == "]":
153152
j = j + 1
154-
while j < n and rawdata[j] in string.whitespace:
153+
while j < n and rawdata[j].isspace():
155154
j = j + 1
156155
if j < n:
157156
if rawdata[j] == ">":
@@ -160,7 +159,7 @@ def _parse_doctype_subset(self, i, declstartpos):
160159
self.error("unexpected char after internal subset")
161160
else:
162161
return -1
163-
elif c in string.whitespace:
162+
elif c.isspace():
164163
j = j + 1
165164
else:
166165
self.updatepos(declstartpos, j)
@@ -203,7 +202,7 @@ def _parse_doctype_attlist(self, i, declstartpos):
203202
j = rawdata.find(")", j) + 1
204203
else:
205204
return -1
206-
while rawdata[j:j+1] in string.whitespace:
205+
while rawdata[j:j+1].isspace():
207206
j = j + 1
208207
if not rawdata[j:]:
209208
# end of buffer, incomplete
@@ -268,7 +267,7 @@ def _parse_doctype_entity(self, i, declstartpos):
268267
c = rawdata[j:j+1]
269268
if not c:
270269
return -1
271-
if c in string.whitespace:
270+
if c.isspace():
272271
j = j + 1
273272
else:
274273
break

Lib/nntplib.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
# Imports
3232
import re
3333
import socket
34-
import types
3534

3635
__all__ = ["NNTP","NNTPReplyError","NNTPTemporaryError",
3736
"NNTPPermanentError","NNTPProtocolError","NNTPDataError",
@@ -218,7 +217,7 @@ def getlongresp(self, file=None):
218217
openedFile = None
219218
try:
220219
# If a string was passed then open a file with that name
221-
if isinstance(file, types.StringType):
220+
if isinstance(file, str):
222221
openedFile = file = open(file, "w")
223222

224223
resp = self.getresp()

Lib/popen2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import os
1010
import sys
11-
import types
1211

1312
__all__ = ["popen2", "popen3", "popen4"]
1413

@@ -57,7 +56,7 @@ def __init__(self, cmd, capturestderr=0, bufsize=-1):
5756
_active.append(self)
5857

5958
def _run_child(self, cmd):
60-
if isinstance(cmd, types.StringTypes):
59+
if isinstance(cmd, basestring):
6160
cmd = ['/bin/sh', '-c', cmd]
6261
for i in range(3, MAXFD):
6362
try:

Lib/pyclbr.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,10 @@ def readmodule_ex(module, path=[], inpackage=0):
208208
f.close()
209209

210210
# To avoid having to stop the regexp at each newline, instead
211-
# when we need a line number we simply string.count the number of
211+
# when we need a line number we simply count the number of
212212
# newlines in the string since the last time we did this; i.e.,
213-
# lineno = lineno + \
214-
# string.count(src, '\n', last_lineno_pos, here)
213+
# lineno += src.count('\n', last_lineno_pos, here)
215214
# last_lineno_pos = here
216-
countnl = string.count
217215
lineno, last_lineno_pos = 1, 0
218216
i = 0
219217
while 1:
@@ -226,9 +224,7 @@ def readmodule_ex(module, path=[], inpackage=0):
226224
# found a method definition or function
227225
thisindent = _indent(m.group("MethodIndent"))
228226
meth_name = m.group("MethodName")
229-
lineno = lineno + \
230-
countnl(src, '\n',
231-
last_lineno_pos, start)
227+
lineno += src.count('\n', last_lineno_pos, start)
232228
last_lineno_pos = start
233229
# close all classes indented at least as much
234230
while classstack and \
@@ -254,8 +250,7 @@ def readmodule_ex(module, path=[], inpackage=0):
254250
while classstack and \
255251
classstack[-1][1] >= thisindent:
256252
del classstack[-1]
257-
lineno = lineno + \
258-
countnl(src, '\n', last_lineno_pos, start)
253+
lineno += src.count('\n', last_lineno_pos, start)
259254
last_lineno_pos = start
260255
class_name = m.group("ClassName")
261256
inherit = m.group("ClassSupers")

Lib/rlcompleter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
defaults to __main__); when completing NAME.NAME..., it evaluates (!) the
66
expression up to the last dot and completes its attributes.
77
8-
It's very cool to do "import string" type "string.", hit the
8+
It's very cool to do "import sys" type "sys.", hit the
99
completion key (twice), and see the list of names defined by the
10-
string module!
10+
sys module!
1111
1212
Tip: to use the tab key as the completion key, call
1313

Lib/smtplib.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import socket
4545
import re
4646
import rfc822
47-
import types
4847
import base64
4948
import hmac
5049

@@ -651,7 +650,7 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
651650
self.rset()
652651
raise SMTPSenderRefused(code, resp, from_addr)
653652
senderrs={}
654-
if isinstance(to_addrs, types.StringTypes):
653+
if isinstance(to_addrs, basestring):
655654
to_addrs = [to_addrs]
656655
for each in to_addrs:
657656
(code,resp)=self.rcpt(each, rcpt_options)

0 commit comments

Comments
 (0)