Python 3 compatibility: imap->map, izip->zip, xrange->range

This commit is contained in:
km4yri 2017-01-02 18:43:59 -05:00
parent 810f604a5a
commit c2c6dbe541
3 changed files with 33 additions and 22 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import division from __future__ import division
from builtins import range # python 2/3 compatibility
from pysstv.sstv import byte_to_freq, FREQ_BLACK, FREQ_WHITE, FREQ_VIS_START from pysstv.sstv import byte_to_freq, FREQ_BLACK, FREQ_WHITE, FREQ_VIS_START
from pysstv.grayscale import GrayscaleSSTV from pysstv.grayscale import GrayscaleSSTV
from itertools import chain from itertools import chain
@ -18,7 +19,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 +93,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
@ -102,19 +103,19 @@ class Robot36(ColorSSTV):
[(self.INTER_CH_FREQS[channel], self.INTER_CH_GAP), [(self.INTER_CH_FREQS[channel], self.INTER_CH_GAP),
(FREQ_VIS_START, self.PORCH)], (FREQ_VIS_START, self.PORCH)],
((byte_to_freq(p[channel]), uv_pixel_time) for p in pixels)) ((byte_to_freq(p[channel]), uv_pixel_time) for p in pixels))
class PasokonP3(ColorSSTV): class PasokonP3(ColorSSTV):
""" """
[ VIS code or horizontal sync here ] [ VIS code or horizontal sync here ]
Back porch - 5 time units of black (1500 Hz). Back porch - 5 time units of black (1500 Hz).
Red component - 640 pixels of 1 time unit each. Red component - 640 pixels of 1 time unit each.
Gap - 5 time units of black. Gap - 5 time units of black.
Green component - 640 pixels of 1 time unit each. Green component - 640 pixels of 1 time unit each.
Gap - 5 time units of black. Gap - 5 time units of black.
Blue component - 640 pixels of 1 time unit each. Blue component - 640 pixels of 1 time unit each.
Front porch - 5 time units of black. Front porch - 5 time units of black.
Horizontal Sync - 25 time units of 1200 Hz. Horizontal Sync - 25 time units of 1200 Hz.
""" """
TIMEUNIT = 1000/4800. # ms TIMEUNIT = 1000/4800. # ms
COLOR_SEQ = (RED, GREEN, BLUE) COLOR_SEQ = (RED, GREEN, BLUE)

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import division from __future__ import division
from builtins import range # python 2/3 compatibility
from pysstv.sstv import SSTV, byte_to_freq from pysstv.sstv import SSTV, byte_to_freq
@ -9,7 +10,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 +19,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

View file

@ -4,7 +4,16 @@ 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 try:
import itertools.imap as map # python 2
except ImportError:
pass # python 3
try:
import itertools.izip as zip # python 2
except ImportError:
pass # python 3
from builtins import range # python 2/3 compatibility
from itertools import cycle, chain
from array import array from array import array
import wave import wave
@ -46,7 +55,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 +73,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 +94,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 +113,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 +124,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