Skip to content

Get rid of string module functions, reduce types module usage #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Xlib/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# Standard modules
import string

# Xlib modules
from . import X

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

return '%s: %s' % (self.__class__, string.join(s, ', '))
return '{0}: {1}'.format(self.__class__, ', '.join(s))

class XResourceError(XError):
_fields = rq.Struct( rq.Card8('type'), # Always 0
Expand Down
23 changes: 11 additions & 12 deletions Xlib/protocol/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import sys
import traceback
import struct
import string
from array import array
import types

Expand Down Expand Up @@ -421,8 +420,8 @@ def __init__(self, name, pad = 1):
self.pad = pad

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

slen = len(val)
Expand Down Expand Up @@ -533,7 +532,7 @@ def pack_value(self, val):
for v in val:
data.append(self.type.pack_value(v))

data = string.join(data, '')
data = ''.join(data)

if self.pad:
dlen = len(data)
Expand Down Expand Up @@ -573,7 +572,7 @@ def pack_value(self, val):
return self.type.pack_value(val)

def check_value(self, val):
if type(val) is types.TupleType:
if type(val) is tuple:
vals = []
i = 0
for f in self.type.fields:
Expand All @@ -589,7 +588,7 @@ def check_value(self, val):
i = i + 1
return vals

if type(val) is types.DictType:
if type(val) is dict:
data = val
elif isinstance(val, DictWrapper):
data = val._data
Expand Down Expand Up @@ -643,7 +642,7 @@ def pack_value(self, value):
if fmt not in (8, 16, 32):
raise BadDataError('Invalid property data format %d' % fmt)

if type(val) is types.StringType:
if type(val) is bytes:
size = fmt / 8
vlen = len(val)
if vlen % size:
Expand All @@ -655,7 +654,7 @@ def pack_value(self, value):
dlen = vlen / size

else:
if type(val) is types.TupleType:
if type(val) is tuple:
val = list(val)

size = fmt / 8
Expand Down Expand Up @@ -1190,16 +1189,16 @@ def pack_value(self, value):

for v in value:
# Let values be simple strings, meaning a delta of 0
if type(v) is types.StringType:
if type(v) is bytes:
v = (0, v)

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

if type(v) in (types.TupleType, types.DictType) or \
if type(v) in (tuple, dict) or \
isinstance(v, DictWrapper):

if type(v) is types.TupleType:
if type(v) is tuple:
delta, str = v
else:
delta = v['delta']
Expand Down Expand Up @@ -1391,7 +1390,7 @@ def __repr__(self):
val = val | 0x80
kwlist.append('%s = %s' % (kw, repr(val)))

kws = string.join(kwlist, ', ')
kws = ', '.join(kwlist)
return '%s(%s)' % (self.__class__, kws)

def __cmp__(self, other):
Expand Down
21 changes: 10 additions & 11 deletions Xlib/rdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@


# Standard modules
import string
import types
import locale
import re
import sys

Expand Down Expand Up @@ -69,7 +68,7 @@ def insert_file(self, file):

"""

if type(file) is types.StringType:
if type(file) is bytes:
file = open(file, 'r')

self.insert_string(file.read())
Expand All @@ -84,7 +83,7 @@ def insert_string(self, data):
"""

# First split string into lines
lines = string.split(data, '\n')
lines = data.split('\n')

while lines:
line = lines[0]
Expand Down Expand Up @@ -122,15 +121,15 @@ def insert_string(self, data):
for i in range(1, len(splits), 2):
s = splits[i]
if len(s) == 3:
splits[i] = chr(string.atoi(s, 8))
splits[i] = chr(locale.atoi(s, 8))
elif s == 'n':
splits[i] = '\n'

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

value = string.join(splits, '')
value = ''.join(splits)

self.insert(res, value)

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

namep = string.split(name, '.')
clsp = string.split(cls, '.')
namep = name.split('.')
clsp = cls.split('.')

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

if type(dbs) is types.TupleType:
if type(dbs) is tuple:
self.skip = 0
self.group = dbs

Expand Down Expand Up @@ -537,7 +536,7 @@ def output_escape(value):
('\000', '\\000'),
('\n', '\\n')):

value = string.replace(value, char, esc)
value = value.replace(char, esc)

# If first or last character is space or tab, escape them.
if value[0] in ' \t':
Expand Down
3 changes: 1 addition & 2 deletions Xlib/support/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import sys
import string

# List the modules which contain the corresponding functions

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

_parts = string.split(sys.platform, '-')
_parts = sys.platform.split('-')
platform = _parts[0]
del _parts

Expand Down
12 changes: 6 additions & 6 deletions Xlib/support/unix_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import re
import string
import locale
import os
import platform
import socket
Expand Down Expand Up @@ -106,8 +106,8 @@ def new_get_auth(sock, dname, host, dno):

# Convert the prettyprinted IP number into 4-octet string.
# Sometimes these modules are too damn smart...
octets = string.split(sock.getpeername()[0], '.')
addr = string.join(map(lambda x: chr(int(x)), octets), '')
octets = sock.getpeername()[0].split('.')
addr = ''.join(map(lambda x: chr(int(x)), octets))
else:
family = xauth.FamilyLocal
addr = socket.gethostname()
Expand Down Expand Up @@ -143,17 +143,17 @@ def old_get_auth(sock, dname, host, dno):
# DISPLAY SCHEME COOKIE
# We're interested in the two last parts for the
# connection establishment
lines = string.split(data, '\n')
lines = data.split('\n')
if len(lines) >= 1:
parts = string.split(lines[0], None, 2)
parts = lines[0].split(None, 2)
if len(parts) == 3:
auth_name = parts[1]
hexauth = parts[2]
auth = ''

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

auth_data = auth
except os.error:
Expand Down
8 changes: 4 additions & 4 deletions Xlib/xobject/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import resource

import re
import string
import locale

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

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

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

return self.alloc_color(r, g, b)

Expand Down
4 changes: 1 addition & 3 deletions Xlib/xobject/drawable.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import string

from Xlib import X, Xatom, Xutil
from Xlib.protocol import request, rq

Expand Down Expand Up @@ -668,7 +666,7 @@ def get_wm_class(self):
if d is None or d.format != 8:
return None
else:
parts = string.split(d.value, '\0')
parts = d.value.split('\0')
if len(parts) < 2:
return None
else:
Expand Down
Loading