Python 3 compatibility: imap->map, izip->zip, xrange->range (#9)

This commit is contained in:
KM4YRI 2017-01-03 09:04:11 -05:00 committed by András Veres-Szentkirályi
parent 810f604a5a
commit 56743c7afb
3 changed files with 49 additions and 22 deletions

View file

@ -1,6 +1,12 @@
#!/usr/bin/env python
from __future__ import division
try: # python 2/3 compatibility
xrange # will fail in python 3
except NameError:
pass
else:
range = xrange
from pysstv.sstv import byte_to_freq, FREQ_BLACK, FREQ_WHITE, FREQ_VIS_START
from pysstv.grayscale import GrayscaleSSTV
from itertools import chain
@ -18,7 +24,7 @@ class ColorSSTV(GrayscaleSSTV):
for index in self.COLOR_SEQ:
for item in self.before_channel(index):
yield item
for col in xrange(self.WIDTH):
for col in range(self.WIDTH):
pixel = image[col, line]
freq_pixel = byte_to_freq(pixel[index])
yield freq_pixel, msec_pixel
@ -92,7 +98,7 @@ class Robot36(ColorSSTV):
self.yuv = self.image.convert('YCbCr').load()
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
y_pixel_time = self.Y_SCAN / self.WIDTH
uv_pixel_time = self.C_SCAN / self.WIDTH
@ -102,19 +108,19 @@ class Robot36(ColorSSTV):
[(self.INTER_CH_FREQS[channel], self.INTER_CH_GAP),
(FREQ_VIS_START, self.PORCH)],
((byte_to_freq(p[channel]), uv_pixel_time) for p in pixels))
class PasokonP3(ColorSSTV):
"""
[ VIS code or horizontal sync here ]
Back porch - 5 time units of black (1500 Hz).
Red component - 640 pixels of 1 time unit each.
Gap - 5 time units of black.
Green component - 640 pixels of 1 time unit each.
Gap - 5 time units of black.
Blue component - 640 pixels of 1 time unit each.
Front porch - 5 time units of black.
Horizontal Sync - 25 time units of 1200 Hz.
[ VIS code or horizontal sync here ]
Back porch - 5 time units of black (1500 Hz).
Red component - 640 pixels of 1 time unit each.
Gap - 5 time units of black.
Green component - 640 pixels of 1 time unit each.
Gap - 5 time units of black.
Blue component - 640 pixels of 1 time unit each.
Front porch - 5 time units of black.
Horizontal Sync - 25 time units of 1200 Hz.
"""
TIMEUNIT = 1000/4800. # ms
COLOR_SEQ = (RED, GREEN, BLUE)

View file

@ -1,6 +1,13 @@
#!/usr/bin/env python
from __future__ import division
try: # python 2/3 compatibility
xrange # will fail in python 3
except NameError:
pass
else:
range = xrange
from pysstv.sstv import SSTV, byte_to_freq
@ -9,7 +16,7 @@ class GrayscaleSSTV(SSTV):
self.pixels = self.image.convert('LA').load()
def gen_image_tuples(self):
for line in xrange(self.HEIGHT):
for line in range(self.HEIGHT):
for item in self.horizontal_sync():
yield item
for item in self.encode_line(line):
@ -18,7 +25,7 @@ class GrayscaleSSTV(SSTV):
def encode_line(self, line):
msec_pixel = self.SCAN / self.WIDTH
image = self.pixels
for col in xrange(self.WIDTH):
for col in range(self.WIDTH):
pixel = image[col, line]
freq_pixel = byte_to_freq(pixel[0])
yield freq_pixel, msec_pixel

View file

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