py3k: map and filter to list comprehensions

What's really going on is that map() and filter() return iterators in py3k.
I've just gone ahead and turned them all into list comprehensions, because
I find them much easier to read.
This commit is contained in:
Brian Crowell 2012-10-15 20:58:46 -05:00 committed by Brian Crowell
parent e514912378
commit fc035814bd
12 changed files with 20 additions and 20 deletions

View File

@ -69,8 +69,8 @@ def bdf_char(f):
bitmap.append(s[:-1]) bitmap.append(s[:-1])
bitmap = "".join(bitmap) bitmap = "".join(bitmap)
[x, y, l, d] = map(int, props["BBX"].split()) [x, y, l, d] = [int(s) for s in props["BBX"].split()]
[dx, dy] = map(int, props["DWIDTH"].split()) [dx, dy] = [int(s) for s in props["DWIDTH"].split()]
bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y) bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y)

View File

@ -183,7 +183,7 @@ class EpsImageFile(ImageFile.ImageFile):
# Note: The DSC spec says that BoundingBox # Note: The DSC spec says that BoundingBox
# fields should be integers, but some drivers # fields should be integers, but some drivers
# put floating point values there anyway. # put floating point values there anyway.
box = map(int, map(float, v.split())) box = [int(float(s)) for s in v.split()]
self.size = box[2] - box[0], box[3] - box[1] self.size = box[2] - box[0], box[3] - box[1]
self.tile = [("eps", (0,0) + self.size, offset, self.tile = [("eps", (0,0) + self.size, offset,
(length, box))] (length, box))]

View File

@ -63,7 +63,7 @@ class FliImageFile(ImageFile.ImageFile):
self.info["duration"] = duration self.info["duration"] = duration
# look for palette # look for palette
palette = map(lambda a: (a,a,a), range(256)) palette = [(a,a,a) for a in range(256)]
s = self.fp.read(16) s = self.fp.read(16)
@ -82,7 +82,7 @@ class FliImageFile(ImageFile.ImageFile):
elif i16(s[4:6]) == 4: elif i16(s[4:6]) == 4:
self._palette(palette, 0) self._palette(palette, 0)
palette = map(lambda (r,g,b): chr(r)+chr(g)+chr(b), palette) palette = [chr(r)+chr(g)+chr(b) for (r,g,b) in palette]
self.palette = ImagePalette.raw("RGB", "".join(palette)) self.palette = ImagePalette.raw("RGB", "".join(palette))
# set things up to decode first frame # set things up to decode first frame

View File

@ -105,7 +105,7 @@ class GimpGradientFile(GradientFile):
for i in range(count): for i in range(count):
s = fp.readline().split() s = fp.readline().split()
w = map(float, s[:11]) w = [float(x) for x in s[:11]]
x0, x1 = w[0], w[2] x0, x1 = w[0], w[2]
xm = w[1] xm = w[1]

View File

@ -25,7 +25,7 @@ class GimpPaletteFile:
def __init__(self, fp): def __init__(self, fp):
self.palette = map(lambda i: chr(i)*3, range(256)) self.palette = [chr(i)*3 for i in range(256)]
if fp.readline()[:12] != "GIMP Palette": if fp.readline()[:12] != "GIMP Palette":
raise SyntaxError("not a GIMP palette file") raise SyntaxError("not a GIMP palette file")

View File

@ -209,7 +209,7 @@ class ImImageFile(ImageFile.ImageFile):
if self.mode == "L" or self.mode == "LA": if self.mode == "L" or self.mode == "LA":
if greyscale: if greyscale:
if not linear: if not linear:
self.lut = map(ord, palette[:256]) self.lut = [ord(c) for c in palette[:256]]
else: else:
if self.mode == "L": if self.mode == "L":
self.mode = self.rawmode = "P" self.mode = self.rawmode = "P"
@ -218,7 +218,7 @@ class ImImageFile(ImageFile.ImageFile):
self.palette = ImagePalette.raw("RGB;L", palette) self.palette = ImagePalette.raw("RGB;L", palette)
elif self.mode == "RGB": elif self.mode == "RGB":
if not greyscale or not linear: if not greyscale or not linear:
self.lut = map(ord, palette) self.lut = [ord(c) for c in palette]
self.frame = 0 self.frame = 0

View File

@ -930,7 +930,7 @@ class Image:
self.load() self.load()
try: try:
return map(ord, self.im.getpalette()) return [ord(c) for c in self.im.getpalette()]
except ValueError: except ValueError:
return None # no palette return None # no palette
@ -959,7 +959,7 @@ class Image:
self.load() self.load()
x, y = self.im.getprojection() x, y = self.im.getprojection()
return map(ord, x), map(ord, y) return [ord(c) for c in x], [ord(c) for c in y]
## ##
# Returns a histogram for the image. The histogram is returned as # Returns a histogram for the image. The histogram is returned as
@ -1129,7 +1129,7 @@ class Image:
scale, offset = _getscaleoffset(lut) scale, offset = _getscaleoffset(lut)
return self._new(self.im.point_transform(scale, offset)) return self._new(self.im.point_transform(scale, offset))
# for other modes, convert the function to a table # for other modes, convert the function to a table
lut = map(lut, range(256)) * self.im.bands lut = [lut(i) for i in range(256)] * self.im.bands
if self.mode == "F": if self.mode == "F":
# FIXME: _imaging returns a confusing error message for this case # FIXME: _imaging returns a confusing error message for this case

View File

@ -209,7 +209,7 @@ def equalize(image, mask=None):
h = image.histogram(mask) h = image.histogram(mask)
lut = [] lut = []
for b in range(0, len(h), 256): for b in range(0, len(h), 256):
histo = filter(None, h[b:b+256]) histo = [_f for _f in h[b:b+256] if _f]
if len(histo) <= 1: if len(histo) <= 1:
lut.extend(range(256)) lut.extend(range(256))
else: else:

View File

@ -59,7 +59,7 @@ class ImagePalette:
except KeyError: except KeyError:
# allocate new color slot # allocate new color slot
if Image.isStringType(self.palette): if Image.isStringType(self.palette):
self.palette = map(int, self.palette) self.palette = [int(x) for x in self.palette]
index = len(self.colors) index = len(self.colors)
if index >= 256: if index >= 256:
raise ValueError("cannot allocate more than 256 colors") raise ValueError("cannot allocate more than 256 colors")

View File

@ -307,7 +307,7 @@ class OleFileIO:
if ix == -2 or ix == -1: # ix == 0xFFFFFFFEL or ix == 0xFFFFFFFFL: if ix == -2 or ix == -1: # ix == 0xFFFFFFFEL or ix == 0xFFFFFFFFL:
break break
s = self.getsect(ix) s = self.getsect(ix)
fat = fat + map(lambda i, s=s: i32(s, i), range(0, len(s), 4)) fat = fat + [i32(s, i) for i in range(0, len(s), 4)]
self.fat = fat self.fat = fat
def loadminifat(self): def loadminifat(self):
@ -316,7 +316,7 @@ class OleFileIO:
s = self._open(self.minifatsect).read() s = self._open(self.minifatsect).read()
self.minifat = map(lambda i, s=s: i32(s, i), range(0, len(s), 4)) self.minifat = [i32(s, i) for i in range(0, len(s), 4)]
def getsect(self, sect): def getsect(self, sect):
# Read given sector # Read given sector

View File

@ -22,7 +22,7 @@ class PaletteFile:
def __init__(self, fp): def __init__(self, fp):
self.palette = map(lambda i: (i, i, i), range(256)) self.palette = [(i, i, i) for i in range(256)]
while 1: while 1:
@ -35,7 +35,7 @@ class PaletteFile:
if len(s) > 100: if len(s) > 100:
raise SyntaxError("bad palette file") raise SyntaxError("bad palette file")
v = map(int, s.split()) v = [int(x) for x in s.split()]
try: try:
[i, r, g, b] = v [i, r, g, b] = v
except ValueError: except ValueError:

View File

@ -701,7 +701,7 @@ class TiffImageFile(ImageFile.ImageFile):
# fixup palette descriptor # fixup palette descriptor
if self.mode == "P": if self.mode == "P":
palette = map(lambda a: chr(a / 256), self.tag[COLORMAP]) palette = [chr(a / 256) for a in self.tag[COLORMAP]]
self.palette = ImagePalette.raw("RGB;L", "".join(palette)) self.palette = ImagePalette.raw("RGB;L", "".join(palette))
# #
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -823,7 +823,7 @@ def _save(im, fp, filename):
if im.mode == "P": if im.mode == "P":
lut = im.im.getpalette("RGB", "RGB;L") lut = im.im.getpalette("RGB", "RGB;L")
ifd[COLORMAP] = tuple(map(lambda v: ord(v) * 256, lut)) ifd[COLORMAP] = tuple(ord(v) * 256 for v in lut)
# data orientation # data orientation
stride = len(bits) * ((im.size[0]*bits[0]+7)/8) stride = len(bits) * ((im.size[0]*bits[0]+7)/8)