From cd5e8a1a88c5fa480a4b3a8b3551fbc39095b2c0 Mon Sep 17 00:00:00 2001 From: Luong Anh Tuan Date: Wed, 12 Oct 2016 12:40:13 +0700 Subject: [PATCH] Remove xrange for run both Python 2 and Python 3 In python 3, range() does what xrange() used to do and xrange() does not exist. If you want to write code that will run on both Python 2 and Python 3, you can't use xrange(). range() can actually be faster in some cases - eg. if iterating over the same sequence multiple times. xrange() has to reconstruct the integer object every time, but range() will have real integer objects. (It will always perform worse in terms of memory however) xrange() isn't usable in all cases where a real list is needed. For instance, it doesn't support slices, or any list methods. Change-Id: I15369a8b9335f5221a52d292a4aa1e36a0850b8f --- pyghmi/ipmi/command.py | 10 +++++----- pyghmi/ipmi/oem/lenovo/handler.py | 12 ++++++------ pyghmi/ipmi/oem/lenovo/imm.py | 2 +- pyghmi/ipmi/oem/lenovo/nextscale.py | 4 ++-- pyghmi/ipmi/private/util.py | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pyghmi/ipmi/command.py b/pyghmi/ipmi/command.py index 732d5d13..ceae7232 100644 --- a/pyghmi/ipmi/command.py +++ b/pyghmi/ipmi/command.py @@ -938,7 +938,7 @@ class Command(object): numpol = ord(rsp['data'][1]) desiredchandest = (channel << 4) | destination availpolnum = None - for polnum in xrange(1, numpol + 1): + for polnum in range(1, numpol + 1): currpol = self.xraw_command(netfn=4, command=0x13, data=(9, polnum, 0)) polidx, chandest = struct.unpack_from('>BB', currpol['data'][2:4]) @@ -1091,7 +1091,7 @@ class Command(object): return retstr def _chunkwise_dcmi_set(self, command, data): - chunks = [data[i:i+15] for i in xrange(0, len(data), 15)] + chunks = [data[i:i+15] for i in range(0, len(data), 15)] offset = 0 for chunk in chunks: chunk = bytearray(chunk, 'utf-8') @@ -1498,7 +1498,7 @@ class Command(object): data = response['data'] if len(data) == 16: # convert int array to string - n = ''.join(chr(data[i]) for i in xrange(0, len(data))) + n = ''.join(chr(data[i]) for i in range(0, len(data))) # remove padded \x00 chars n = n.rstrip("\x00") if len(n) > 0: @@ -1595,7 +1595,7 @@ class Command(object): channel = self.get_network_channel() uid_list = [] max_ids = self.get_channel_max_user_count(channel) - for uid in xrange(1, max_ids): + for uid in range(1, max_ids): if name == self.get_user_name(uid=uid): uid_list.append(uid) return uid_list @@ -1620,7 +1620,7 @@ class Command(object): channel = self.get_network_channel() names = {} max_ids = self.get_channel_max_user_count(channel) - for uid in xrange(1, max_ids+1): + for uid in range(1, max_ids+1): name = self.get_user_name(uid=uid) if name is not None: names[uid] = self.get_user(uid=uid, channel=channel) diff --git a/pyghmi/ipmi/oem/lenovo/handler.py b/pyghmi/ipmi/oem/lenovo/handler.py index 2e595e87..7e27ae9c 100755 --- a/pyghmi/ipmi/oem/lenovo/handler.py +++ b/pyghmi/ipmi/oem/lenovo/handler.py @@ -346,7 +346,7 @@ class OEMHandler(generic.OEMHandler): tmp_command = dict(catspec["command"]) tmp_command["data"] = list(tmp_command["data"]) count = 0 - for i in xrange(0x01, 0xff): + for i in range(0x01, 0xff): tmp_command["data"][-1] = i try: partrsp = self.ipmicmd.xraw_command(**tmp_command) @@ -538,7 +538,7 @@ class OEMHandler(generic.OEMHandler): def get_oem_domain_name(self): if self.has_tsm: name = '' - for i in xrange(1, 5): + for i in range(1, 5): rsp = self.ipmicmd.xraw_command(netfn=0x32, command=0x6b, data=(4, i)) name += rsp['data'][:] @@ -552,7 +552,7 @@ class OEMHandler(generic.OEMHandler): # set the domain name content name = name.ljust(256, "\x00") - for i in xrange(0, 4): + for i in range(0, 4): data = [4, i+1] offset = i*64 data.extend([ord(x) for x in name[offset:offset+64]]) @@ -630,7 +630,7 @@ class OEMHandler(generic.OEMHandler): else: # fall back to a dumber, but more universal formatter ipv6str = binascii.b2a_hex(ipv6_addr) - ipv6str = ':'.join([ipv6str[x:x+4] for x in xrange(0, 32, 4)]) + ipv6str = ':'.join([ipv6str[x:x+4] for x in range(0, 32, 4)]) netdata['ipv6_addresses'] = [ '{0}/{1}'.format(ipv6str, ipv6_prefix)] @@ -688,7 +688,7 @@ class OEMHandler(generic.OEMHandler): self.ipmicmd.xraw_command(netfn=0x32, command=0x9f, data=(1, selector, 0, 1)) # now do the set - for x in xrange(0, 256, 64): + for x in range(0, 256, 64): currdata = padded[x:x+64] currchunk = x // 64 + 1 cmddata = [1, selector, currchunk] + currdata @@ -702,7 +702,7 @@ class OEMHandler(generic.OEMHandler): data=(7, 1, 0)) imgnames = rsp['data'][1:] shortnames = [] - for idx in xrange(0, len(imgnames), 22): + for idx in range(0, len(imgnames), 22): shortnames.append(imgnames[idx+2:idx+22].rstrip('\0')) return shortnames diff --git a/pyghmi/ipmi/oem/lenovo/imm.py b/pyghmi/ipmi/oem/lenovo/imm.py index 4b31d2e4..0afe63ae 100644 --- a/pyghmi/ipmi/oem/lenovo/imm.py +++ b/pyghmi/ipmi/oem/lenovo/imm.py @@ -242,7 +242,7 @@ def hardware_inventory_map(ipmicmd, certverify): for lp in portinfo['logicalPorts']: ma = lp['networkAddr'] ma = ':'.join( - [ma[i:i+2] for i in xrange( + [ma[i:i+2] for i in range( 0, len(ma), 2)]).lower() bdata['MAC Address {0}'.format( portinfo['portIndex'])] = ma diff --git a/pyghmi/ipmi/oem/lenovo/nextscale.py b/pyghmi/ipmi/oem/lenovo/nextscale.py index c1868a89..e2afe115 100644 --- a/pyghmi/ipmi/oem/lenovo/nextscale.py +++ b/pyghmi/ipmi/oem/lenovo/nextscale.py @@ -140,7 +140,7 @@ def get_sensor_names(): for name in fpc_sensors: sensor = fpc_sensors[name] if 'elements' in sensor: - for elemidx in xrange(sensor['elements']): + for elemidx in range(sensor['elements']): elemidx += 1 yield '{0} {1}'.format(name, elemidx) else: @@ -152,7 +152,7 @@ def get_sensor_descriptions(): for name in fpc_sensors: sensor = fpc_sensors[name] if 'elements' in sensor: - for elemidx in xrange(sensor['elements']): + for elemidx in range(sensor['elements']): elemidx += 1 yield {'name': '{0} {1}'.format(name, elemidx), 'type': sensor['type']} diff --git a/pyghmi/ipmi/private/util.py b/pyghmi/ipmi/private/util.py index 46ec5127..9be86381 100644 --- a/pyghmi/ipmi/private/util.py +++ b/pyghmi/ipmi/private/util.py @@ -59,4 +59,4 @@ def get_ipv4(hostname): """ addrinfo = socket.getaddrinfo(hostname, None, socket.AF_INET, socket.SOCK_STREAM) - return [addrinfo[x][4][0] for x in xrange(len(addrinfo))] + return [addrinfo[x][4][0] for x in range(len(addrinfo))]