From fc035814bd76333dfc69e1529c6326ec7d01a5be Mon Sep 17 00:00:00 2001 From: Brian Crowell Date: Mon, 15 Oct 2012 20:58:46 -0500 Subject: [PATCH] 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. --- PIL/BdfFontFile.py | 4 ++-- PIL/EpsImagePlugin.py | 2 +- PIL/FliImagePlugin.py | 4 ++-- PIL/GimpGradientFile.py | 2 +- PIL/GimpPaletteFile.py | 2 +- PIL/ImImagePlugin.py | 4 ++-- PIL/Image.py | 6 +++--- PIL/ImageOps.py | 2 +- PIL/ImagePalette.py | 2 +- PIL/OleFileIO.py | 4 ++-- PIL/PaletteFile.py | 4 ++-- PIL/TiffImagePlugin.py | 4 ++-- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/PIL/BdfFontFile.py b/PIL/BdfFontFile.py index 8652ed0b6..eb656d3ac 100644 --- a/PIL/BdfFontFile.py +++ b/PIL/BdfFontFile.py @@ -69,8 +69,8 @@ def bdf_char(f): bitmap.append(s[:-1]) bitmap = "".join(bitmap) - [x, y, l, d] = map(int, props["BBX"].split()) - [dx, dy] = map(int, props["DWIDTH"].split()) + [x, y, l, d] = [int(s) for s in props["BBX"].split()] + [dx, dy] = [int(s) for s in props["DWIDTH"].split()] bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y) diff --git a/PIL/EpsImagePlugin.py b/PIL/EpsImagePlugin.py index bef485258..450ba2422 100644 --- a/PIL/EpsImagePlugin.py +++ b/PIL/EpsImagePlugin.py @@ -183,7 +183,7 @@ class EpsImageFile(ImageFile.ImageFile): # Note: The DSC spec says that BoundingBox # fields should be integers, but some drivers # 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.tile = [("eps", (0,0) + self.size, offset, (length, box))] diff --git a/PIL/FliImagePlugin.py b/PIL/FliImagePlugin.py index 9f85a0ae4..8f48e29b8 100644 --- a/PIL/FliImagePlugin.py +++ b/PIL/FliImagePlugin.py @@ -63,7 +63,7 @@ class FliImageFile(ImageFile.ImageFile): self.info["duration"] = duration # 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) @@ -82,7 +82,7 @@ class FliImageFile(ImageFile.ImageFile): elif i16(s[4:6]) == 4: 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)) # set things up to decode first frame diff --git a/PIL/GimpGradientFile.py b/PIL/GimpGradientFile.py index 1e9baa28d..c2db7bc7c 100644 --- a/PIL/GimpGradientFile.py +++ b/PIL/GimpGradientFile.py @@ -105,7 +105,7 @@ class GimpGradientFile(GradientFile): for i in range(count): s = fp.readline().split() - w = map(float, s[:11]) + w = [float(x) for x in s[:11]] x0, x1 = w[0], w[2] xm = w[1] diff --git a/PIL/GimpPaletteFile.py b/PIL/GimpPaletteFile.py index a0887a31a..a904cf6dc 100644 --- a/PIL/GimpPaletteFile.py +++ b/PIL/GimpPaletteFile.py @@ -25,7 +25,7 @@ class GimpPaletteFile: 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": raise SyntaxError("not a GIMP palette file") diff --git a/PIL/ImImagePlugin.py b/PIL/ImImagePlugin.py index d7f38cb68..bc4910234 100644 --- a/PIL/ImImagePlugin.py +++ b/PIL/ImImagePlugin.py @@ -209,7 +209,7 @@ class ImImageFile(ImageFile.ImageFile): if self.mode == "L" or self.mode == "LA": if greyscale: if not linear: - self.lut = map(ord, palette[:256]) + self.lut = [ord(c) for c in palette[:256]] else: if self.mode == "L": self.mode = self.rawmode = "P" @@ -218,7 +218,7 @@ class ImImageFile(ImageFile.ImageFile): self.palette = ImagePalette.raw("RGB;L", palette) elif self.mode == "RGB": if not greyscale or not linear: - self.lut = map(ord, palette) + self.lut = [ord(c) for c in palette] self.frame = 0 diff --git a/PIL/Image.py b/PIL/Image.py index 9d12954bd..64027cdec 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -930,7 +930,7 @@ class Image: self.load() try: - return map(ord, self.im.getpalette()) + return [ord(c) for c in self.im.getpalette()] except ValueError: return None # no palette @@ -959,7 +959,7 @@ class Image: self.load() 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 @@ -1129,7 +1129,7 @@ class Image: scale, offset = _getscaleoffset(lut) return self._new(self.im.point_transform(scale, offset)) # 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": # FIXME: _imaging returns a confusing error message for this case diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index 9ff0c4527..9825d9ced 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -209,7 +209,7 @@ def equalize(image, mask=None): h = image.histogram(mask) lut = [] 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: lut.extend(range(256)) else: diff --git a/PIL/ImagePalette.py b/PIL/ImagePalette.py index 0bc717316..a57688b53 100644 --- a/PIL/ImagePalette.py +++ b/PIL/ImagePalette.py @@ -59,7 +59,7 @@ class ImagePalette: except KeyError: # allocate new color slot if Image.isStringType(self.palette): - self.palette = map(int, self.palette) + self.palette = [int(x) for x in self.palette] index = len(self.colors) if index >= 256: raise ValueError("cannot allocate more than 256 colors") diff --git a/PIL/OleFileIO.py b/PIL/OleFileIO.py index 269f602a9..f8c25181f 100644 --- a/PIL/OleFileIO.py +++ b/PIL/OleFileIO.py @@ -307,7 +307,7 @@ class OleFileIO: if ix == -2 or ix == -1: # ix == 0xFFFFFFFEL or ix == 0xFFFFFFFFL: break 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 def loadminifat(self): @@ -316,7 +316,7 @@ class OleFileIO: 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): # Read given sector diff --git a/PIL/PaletteFile.py b/PIL/PaletteFile.py index bd842c3dc..7df149301 100644 --- a/PIL/PaletteFile.py +++ b/PIL/PaletteFile.py @@ -22,7 +22,7 @@ class PaletteFile: 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: @@ -35,7 +35,7 @@ class PaletteFile: if len(s) > 100: raise SyntaxError("bad palette file") - v = map(int, s.split()) + v = [int(x) for x in s.split()] try: [i, r, g, b] = v except ValueError: diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index cdc17d242..bd860454b 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -701,7 +701,7 @@ class TiffImageFile(ImageFile.ImageFile): # fixup palette descriptor 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)) # # -------------------------------------------------------------------- @@ -823,7 +823,7 @@ def _save(im, fp, filename): if im.mode == "P": 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 stride = len(bits) * ((im.size[0]*bits[0]+7)/8)