Skip to content

Commit f94f68f

Browse files
Merge pull request #15 from vasily-v-ryabov/master
Get rid of string module functions, reduce types module usage
2 parents 2445b6e + 700c827 commit f94f68f

File tree

12 files changed

+63
-77
lines changed

12 files changed

+63
-77
lines changed

Xlib/error.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
# along with this program; if not, write to the Free Software
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

19-
# Standard modules
20-
import string
21-
2219
# Xlib modules
2320
from . import X
2421

@@ -76,9 +73,9 @@ def __str__(self):
7673
s = []
7774
for f in ('code', 'resource_id', 'sequence_number',
7875
'major_opcode', 'minor_opcode'):
79-
s.append('%s = %s' % (f, self._data[f]))
76+
s.append('{0} = {1}'.format(f, self._data[f]))
8077

81-
return '%s: %s' % (self.__class__, string.join(s, ', '))
78+
return '{0}: {1}'.format(self.__class__, ', '.join(s))
8279

8380
class XResourceError(XError):
8481
_fields = rq.Struct( rq.Card8('type'), # Always 0

Xlib/protocol/rq.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import sys
2222
import traceback
2323
import struct
24-
import string
2524
from array import array
2625
import types
2726

@@ -421,8 +420,8 @@ def __init__(self, name, pad = 1):
421420
self.pad = pad
422421

423422
def pack_value(self, val):
424-
# Convert 8-byte string into 16-byte list
425-
if type(val) is types.StringType:
423+
"""Convert 8-byte string into 16-byte list"""
424+
if type(val) is bytes:
426425
val = map(lambda c: ord(c), val)
427426

428427
slen = len(val)
@@ -533,7 +532,7 @@ def pack_value(self, val):
533532
for v in val:
534533
data.append(self.type.pack_value(v))
535534

536-
data = string.join(data, '')
535+
data = ''.join(data)
537536

538537
if self.pad:
539538
dlen = len(data)
@@ -573,7 +572,7 @@ def pack_value(self, val):
573572
return self.type.pack_value(val)
574573

575574
def check_value(self, val):
576-
if type(val) is types.TupleType:
575+
if type(val) is tuple:
577576
vals = []
578577
i = 0
579578
for f in self.type.fields:
@@ -589,7 +588,7 @@ def check_value(self, val):
589588
i = i + 1
590589
return vals
591590

592-
if type(val) is types.DictType:
591+
if type(val) is dict:
593592
data = val
594593
elif isinstance(val, DictWrapper):
595594
data = val._data
@@ -643,7 +642,7 @@ def pack_value(self, value):
643642
if fmt not in (8, 16, 32):
644643
raise BadDataError('Invalid property data format %d' % fmt)
645644

646-
if type(val) is types.StringType:
645+
if type(val) is bytes:
647646
size = fmt / 8
648647
vlen = len(val)
649648
if vlen % size:
@@ -655,7 +654,7 @@ def pack_value(self, value):
655654
dlen = vlen / size
656655

657656
else:
658-
if type(val) is types.TupleType:
657+
if type(val) is tuple:
659658
val = list(val)
660659

661660
size = fmt / 8
@@ -1190,16 +1189,16 @@ def pack_value(self, value):
11901189

11911190
for v in value:
11921191
# Let values be simple strings, meaning a delta of 0
1193-
if type(v) is types.StringType:
1192+
if type(v) is bytes:
11941193
v = (0, v)
11951194

11961195
# A tuple, it should be (delta, string)
11971196
# Encode it as one or more textitems
11981197

1199-
if type(v) in (types.TupleType, types.DictType) or \
1198+
if type(v) in (tuple, dict) or \
12001199
isinstance(v, DictWrapper):
12011200

1202-
if type(v) is types.TupleType:
1201+
if type(v) is tuple:
12031202
delta, str = v
12041203
else:
12051204
delta = v['delta']
@@ -1391,7 +1390,7 @@ def __repr__(self):
13911390
val = val | 0x80
13921391
kwlist.append('%s = %s' % (kw, repr(val)))
13931392

1394-
kws = string.join(kwlist, ', ')
1393+
kws = ', '.join(kwlist)
13951394
return '%s(%s)' % (self.__class__, kws)
13961395

13971396
def __cmp__(self, other):

Xlib/rdb.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323

2424
# Standard modules
25-
import string
26-
import types
25+
import locale
2726
import re
2827
import sys
2928

@@ -69,7 +68,7 @@ def insert_file(self, file):
6968
7069
"""
7170

72-
if type(file) is types.StringType:
71+
if type(file) is bytes:
7372
file = open(file, 'r')
7473

7574
self.insert_string(file.read())
@@ -84,7 +83,7 @@ def insert_string(self, data):
8483
"""
8584

8685
# First split string into lines
87-
lines = string.split(data, '\n')
86+
lines = data.split('\n')
8887

8988
while lines:
9089
line = lines[0]
@@ -122,15 +121,15 @@ def insert_string(self, data):
122121
for i in range(1, len(splits), 2):
123122
s = splits[i]
124123
if len(s) == 3:
125-
splits[i] = chr(string.atoi(s, 8))
124+
splits[i] = chr(locale.atoi(s, 8))
126125
elif s == 'n':
127126
splits[i] = '\n'
128127

129128
# strip the last value part to get rid of any
130129
# unescaped blanks
131-
splits[-1] = string.rstrip(splits[-1])
130+
splits[-1] = splits[-1].rstrip()
132131

133-
value = string.join(splits, '')
132+
value = ''.join(splits)
134133

135134
self.insert(res, value)
136135

@@ -199,8 +198,8 @@ def __getitem__(self, keys_tuple):
199198
# Split name and class into their parts
200199
name, cls = keys_tuple
201200

202-
namep = string.split(name, '.')
203-
clsp = string.split(cls, '.')
201+
namep = name.split('.')
202+
clsp = cls.split('.')
204203

205204
# It is an error for name and class to have different number
206205
# of parts
@@ -381,7 +380,7 @@ class _Match(object):
381380
def __init__(self, path, dbs):
382381
self.path = path
383382

384-
if type(dbs) is types.TupleType:
383+
if type(dbs) is tuple:
385384
self.skip = 0
386385
self.group = dbs
387386

@@ -537,7 +536,7 @@ def output_escape(value):
537536
('\000', '\\000'),
538537
('\n', '\\n')):
539538

540-
value = string.replace(value, char, esc)
539+
value = value.replace(char, esc)
541540

542541
# If first or last character is space or tab, escape them.
543542
if value[0] in ' \t':

Xlib/support/connect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

1919
import sys
20-
import string
2120

2221
# List the modules which contain the corresponding functions
2322

@@ -43,7 +42,7 @@
4342
# Figure out which OS we're using.
4443
# sys.platform is either "OS-ARCH" or just "OS".
4544

46-
_parts = string.split(sys.platform, '-')
45+
_parts = sys.platform.split('-')
4746
platform = _parts[0]
4847
del _parts
4948

Xlib/support/unix_connect.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

1919
import re
20-
import string
20+
import locale
2121
import os
2222
import platform
2323
import socket
@@ -106,8 +106,8 @@ def new_get_auth(sock, dname, host, dno):
106106

107107
# Convert the prettyprinted IP number into 4-octet string.
108108
# Sometimes these modules are too damn smart...
109-
octets = string.split(sock.getpeername()[0], '.')
110-
addr = string.join(map(lambda x: chr(int(x)), octets), '')
109+
octets = sock.getpeername()[0].split('.')
110+
addr = ''.join(map(lambda x: chr(int(x)), octets))
111111
else:
112112
family = xauth.FamilyLocal
113113
addr = socket.gethostname()
@@ -143,17 +143,17 @@ def old_get_auth(sock, dname, host, dno):
143143
# DISPLAY SCHEME COOKIE
144144
# We're interested in the two last parts for the
145145
# connection establishment
146-
lines = string.split(data, '\n')
146+
lines = data.split('\n')
147147
if len(lines) >= 1:
148-
parts = string.split(lines[0], None, 2)
148+
parts = lines[0].split(None, 2)
149149
if len(parts) == 3:
150150
auth_name = parts[1]
151151
hexauth = parts[2]
152152
auth = ''
153153

154154
# Translate hexcode into binary
155155
for i in range(0, len(hexauth), 2):
156-
auth = auth + chr(string.atoi(hexauth[i:i+2], 16))
156+
auth = auth + chr(locale.atoi(hexauth[i:i+2], 16))
157157

158158
auth_data = auth
159159
except os.error:

Xlib/xobject/colormap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import resource
2323

2424
import re
25-
import string
25+
import locale
2626

2727
rgb_res = [
2828
re.compile(r'\Argb:([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})\Z'),
@@ -73,13 +73,13 @@ def alloc_named_color(self, name):
7373
m = r.match(name)
7474
if m:
7575
rs = m.group(1)
76-
r = string.atoi(rs + '0' * (4 - len(rs)), 16)
76+
r = locale.atoi(rs + '0' * (4 - len(rs)), 16)
7777

7878
gs = m.group(2)
79-
g = string.atoi(gs + '0' * (4 - len(gs)), 16)
79+
g = locale.atoi(gs + '0' * (4 - len(gs)), 16)
8080

8181
bs = m.group(3)
82-
b = string.atoi(bs + '0' * (4 - len(bs)), 16)
82+
b = locale.atoi(bs + '0' * (4 - len(bs)), 16)
8383

8484
return self.alloc_color(r, g, b)
8585

Xlib/xobject/drawable.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
# along with this program; if not, write to the Free Software
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

19-
import string
20-
2119
from Xlib import X, Xatom, Xutil
2220
from Xlib.protocol import request, rq
2321

@@ -668,7 +666,7 @@ def get_wm_class(self):
668666
if d is None or d.format != 8:
669667
return None
670668
else:
671-
parts = string.split(d.value, '\0')
669+
parts = d.value.split('\0')
672670
if len(parts) < 2:
673671
return None
674672
else:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy