replaced Python 2 iter calls with Python 3 ones

python3
András Veres-Szentkirályi 2014-06-07 11:27:21 +02:00
rodzic 09df062547
commit c432be2447
5 zmienionych plików z 19 dodań i 19 usunięć

Wyświetl plik

@ -18,7 +18,7 @@ class ColorSSTV(GrayscaleSSTV):
for index in self.COLOR_SEQ: for index in self.COLOR_SEQ:
for item in self.before_channel(index): for item in self.before_channel(index):
yield item yield item
for col in xrange(self.WIDTH): for col in range(self.WIDTH):
pixel = image[col, line] pixel = image[col, line]
freq_pixel = byte_to_freq(pixel[index]) freq_pixel = byte_to_freq(pixel[index])
yield freq_pixel, msec_pixel yield freq_pixel, msec_pixel
@ -92,7 +92,7 @@ class Robot36(ColorSSTV):
self.yuv = self.image.convert('YCbCr').load() self.yuv = self.image.convert('YCbCr').load()
def encode_line(self, line): def encode_line(self, line):
pixels = [self.yuv[col, line] for col in xrange(self.WIDTH)] pixels = [self.yuv[col, line] for col in range(self.WIDTH)]
channel = (line % 2) + 1 channel = (line % 2) + 1
y_pixel_time = self.Y_SCAN / self.WIDTH y_pixel_time = self.Y_SCAN / self.WIDTH
uv_pixel_time = self.C_SCAN / self.WIDTH uv_pixel_time = self.C_SCAN / self.WIDTH

Wyświetl plik

@ -120,10 +120,10 @@ class ProgressCanvas(Canvas):
pixels = image.load() pixels = image.load()
RED, GREEN, BLUE = range(3) RED, GREEN, BLUE = range(3)
self.colors = ['#{0:02x}{1:02x}{2:02x}'.format( self.colors = ['#{0:02x}{1:02x}{2:02x}'.format(
contrast(sum(pixels[x, y][RED] for x in xrange(width)) / width), contrast(sum(pixels[x, y][RED] for x in range(width)) / width),
contrast(sum(pixels[x, y][GREEN] for x in xrange(width)) / width), contrast(sum(pixels[x, y][GREEN] for x in range(width)) / width),
contrast(sum(pixels[x, y][BLUE] for x in xrange(width)) / width)) contrast(sum(pixels[x, y][BLUE] for x in range(width)) / width))
for y in xrange(height)] for y in range(height)]
if height / float(width) > 1.5: if height / float(width) > 1.5:
width *= 2 width *= 2
elif width < 200: elif width < 200:

Wyświetl plik

@ -9,7 +9,7 @@ class GrayscaleSSTV(SSTV):
self.pixels = self.image.convert('LA').load() self.pixels = self.image.convert('LA').load()
def gen_image_tuples(self): def gen_image_tuples(self):
for line in xrange(self.HEIGHT): for line in range(self.HEIGHT):
for item in self.horizontal_sync(): for item in self.horizontal_sync():
yield item yield item
for item in self.encode_line(line): for item in self.encode_line(line):
@ -18,7 +18,7 @@ class GrayscaleSSTV(SSTV):
def encode_line(self, line): def encode_line(self, line):
msec_pixel = self.SCAN / self.WIDTH msec_pixel = self.SCAN / self.WIDTH
image = self.pixels image = self.pixels
for col in xrange(self.WIDTH): for col in range(self.WIDTH):
pixel = image[col, line] pixel = image[col, line]
freq_pixel = byte_to_freq(pixel[0]) freq_pixel = byte_to_freq(pixel[0])
yield freq_pixel, msec_pixel yield freq_pixel, msec_pixel

Wyświetl plik

@ -4,7 +4,7 @@ from __future__ import division, with_statement
from math import sin, pi from math import sin, pi
from random import random from random import random
from contextlib import closing from contextlib import closing
from itertools import imap, izip, cycle, chain from itertools import cycle, chain
from array import array from array import array
import wave import wave
@ -46,7 +46,7 @@ class SSTV(object):
data = array(fmt, self.gen_samples()) data = array(fmt, self.gen_samples())
if self.nchannels != 1: if self.nchannels != 1:
data = array(fmt, chain.from_iterable( data = array(fmt, chain.from_iterable(
izip(*([data] * self.nchannels)))) zip(*([data] * self.nchannels))))
with closing(wave.open(filename, 'wb')) as wav: with closing(wave.open(filename, 'wb')) as wav:
wav.setnchannels(self.nchannels) wav.setnchannels(self.nchannels)
wav.setsampwidth(self.bits // 8) wav.setsampwidth(self.bits // 8)
@ -64,8 +64,8 @@ class SSTV(object):
amp = max_value // 2 amp = max_value // 2
lowest = -amp lowest = -amp
highest = amp - 1 highest = amp - 1
alias_cycle = cycle((alias * (random() - 0.5) for _ in xrange(1024))) alias_cycle = cycle((alias * (random() - 0.5) for _ in range(1024)))
for value, alias_item in izip(self.gen_values(), alias_cycle): for value, alias_item in zip(self.gen_values(), alias_cycle):
sample = int(value * amp + alias_item) sample = int(value * amp + alias_item)
yield (lowest if sample <= lowest else yield (lowest if sample <= lowest else
sample if sample <= highest else highest) sample if sample <= highest else highest)
@ -85,7 +85,7 @@ class SSTV(object):
samples += spms * msec samples += spms * msec
tx = int(samples) tx = int(samples)
freq_factor = freq * factor freq_factor = freq * factor
for sample in xrange(tx): for sample in range(tx):
yield sin(sample * freq_factor + offset) yield sin(sample * freq_factor + offset)
offset += (sample + 1) * freq_factor offset += (sample + 1) * freq_factor
samples -= tx samples -= tx
@ -104,7 +104,7 @@ class SSTV(object):
yield FREQ_SYNC, MSEC_VIS_BIT # start bit yield FREQ_SYNC, MSEC_VIS_BIT # start bit
vis = self.VIS_CODE vis = self.VIS_CODE
num_ones = 0 num_ones = 0
for _ in xrange(7): for _ in range(7):
bit = vis & 1 bit = vis & 1
vis >>= 1 vis >>= 1
num_ones += bit num_ones += bit
@ -115,8 +115,8 @@ class SSTV(object):
yield FREQ_SYNC, MSEC_VIS_BIT # stop bit yield FREQ_SYNC, MSEC_VIS_BIT # stop bit
for freq_tuple in self.gen_image_tuples(): for freq_tuple in self.gen_image_tuples():
yield freq_tuple yield freq_tuple
for fskid_byte in imap(ord, self.fskid_payload): for fskid_byte in map(ord, self.fskid_payload):
for _ in xrange(6): for _ in range(6):
bit = fskid_byte & 1 bit = fskid_byte & 1
fskid_byte >>= 1 fskid_byte >>= 1
bit_freq = FREQ_FSKID_BIT1 if bit == 1 else FREQ_FSKID_BIT0 bit_freq = FREQ_FSKID_BIT1 if bit == 1 else FREQ_FSKID_BIT0

Wyświetl plik

@ -1,5 +1,5 @@
import unittest import unittest
from itertools import islice, izip from itertools import islice
import pickle import pickle
import mock import mock
from mock import MagicMock from mock import MagicMock
@ -46,7 +46,7 @@ class TestSSTV(unittest.TestCase):
def test_gen_values(self): def test_gen_values(self):
gen_values = self.s.gen_values() gen_values = self.s.gen_values()
expected = pickle.load(open(get_asset_filename("SSTV_gen_values.p"))) expected = pickle.load(open(get_asset_filename("SSTV_gen_values.p")))
for e, g in izip(expected, gen_values): for e, g in zip(expected, gen_values):
self.assertAlmostEqual(e, g, delta=0.000000001) self.assertAlmostEqual(e, g, delta=0.000000001)
def test_gen_samples(self): def test_gen_samples(self):
@ -59,7 +59,7 @@ class TestSSTV(unittest.TestCase):
sstv.random = MagicMock(return_value=0.4) # xkcd:221 sstv.random = MagicMock(return_value=0.4) # xkcd:221
expected = pickle.load(open(get_asset_filename("SSTV_gen_samples.p"))) expected = pickle.load(open(get_asset_filename("SSTV_gen_samples.p")))
actual = list(islice(gen_values, 0, 1000)) actual = list(islice(gen_values, 0, 1000))
for e, a in izip(expected, actual): for e, a in zip(expected, actual):
self.assertAlmostEqual(e, a, delta=1) self.assertAlmostEqual(e, a, delta=1)
def test_write_wav(self): def test_write_wav(self):