From 9e625c4821f4c63421b3d3747c0c4f358fef6c5f Mon Sep 17 00:00:00 2001 From: Douglas Anderson <danderson@ntia.doc.gov> Date: Sun, 12 Feb 2017 15:52:19 -0800 Subject: python3: update non-GRC components to use python2 or python3 --- gr-utils/python/utils/plot_fft_base.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) mode change 100755 => 100644 gr-utils/python/utils/plot_fft_base.py (limited to 'gr-utils/python/utils/plot_fft_base.py') diff --git a/gr-utils/python/utils/plot_fft_base.py b/gr-utils/python/utils/plot_fft_base.py old mode 100755 new mode 100644 index c99462147d..c0651529ea --- a/gr-utils/python/utils/plot_fft_base.py +++ b/gr-utils/python/utils/plot_fft_base.py @@ -20,22 +20,26 @@ # Boston, MA 02110-1301, USA. # +from __future__ import print_function +from __future__ import division +from __future__ import unicode_literals + try: import scipy from scipy import fftpack except ImportError: - print "Please install SciPy to run this script (http://www.scipy.org/)" - raise SystemExit, 1 + print("Please install SciPy to run this script (http://www.scipy.org/)") + raise SystemExit(1) try: from pylab import * except ImportError: - print "Please install Python Matplotlib (http://matplotlib.sourceforge.net/) and Python TkInter https://wiki.python.org/moin/TkInter to run this script" - raise SystemExit, 1 + print("Please install Python Matplotlib (http://matplotlib.sourceforge.net/) and Python TkInter https://wiki.python.org/moin/TkInter to run this script") + raise SystemExit(1) from argparse import ArgumentParser -class plot_fft_base: +class plot_fft_base(object): def __init__(self, datatype, filename, options): self.hfile = open(filename, "r") self.block_length = options.block @@ -79,33 +83,33 @@ class plot_fft_base: show() def get_data(self): - self.position = self.hfile.tell()/self.sizeof_data + self.position = self.hfile.tell() / self.sizeof_data self.text_file_pos.set_text("File Position: %d" % (self.position)) try: self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length) except MemoryError: - print "End of File" + print("End of File") else: self.iq_fft = self.dofft(self.iq) tstep = 1.0 / self.sample_rate #self.time = scipy.array([tstep*(self.position + i) for i in xrange(len(self.iq))]) - self.time = scipy.array([tstep*(i) for i in xrange(len(self.iq))]) + self.time = scipy.array([tstep*(i) for i in range(len(self.iq))]) self.freq = self.calc_freq(self.time, self.sample_rate) def dofft(self, iq): N = len(iq) iq_fft = scipy.fftpack.fftshift(scipy.fft(iq)) # fft and shift axis - iq_fft = 20*scipy.log10(abs((iq_fft+1e-15)/N)) # convert to decibels, adjust power + iq_fft = 20*scipy.log10(abs((iq_fft+1e-15) / N)) # convert to decibels, adjust power # adding 1e-15 (-300 dB) to protect against value errors if an item in iq_fft is 0 return iq_fft def calc_freq(self, time, sample_rate): N = len(time) - Fs = 1.0 / (time.max() - time.min()) + Fs = 1.0 / (time.max( - time.min())) Fn = 0.5 * sample_rate - freq = scipy.array([-Fn + i*Fs for i in xrange(N)]) + freq = scipy.array([-Fn + i*Fs for i in range(N)]) return freq def make_plots(self): @@ -228,9 +232,9 @@ class plot_fft_base: def find(item_in, list_search): try: - return list_search.index(item_in) != None + return list_search.index(item_in) != None except ValueError: - return False + return False def main(): parser = plot_fft_base.setup_options() @@ -243,6 +247,3 @@ if __name__ == "__main__": main() except KeyboardInterrupt: pass - - - -- cgit v1.2.3 From c40c0f3243711bbf25afdd0147d45f2e96b91c06 Mon Sep 17 00:00:00 2001 From: Andrej Rode <mail@andrejro.de> Date: Sat, 23 Jun 2018 19:01:34 +0200 Subject: python: Py3k compat --- gr-blocks/python/blocks/qa_socket_pdu.py | 4 ++-- gr-digital/python/digital/qa_scrambler.py | 4 ++-- gr-qtgui/apps/gr_constellation_plot | 2 +- gr-uhd/apps/uhd_fft | 8 ++++---- gr-uhd/apps/uhd_rx_cfile | 6 +++--- gr-utils/python/utils/gr_plot_qt | 2 +- gr-utils/python/utils/plot_fft_base.py | 2 +- gr-utils/python/utils/plot_psd_base.py | 2 +- gr-zeromq/python/zeromq/qa_zeromq_sub.py | 4 ++-- 9 files changed, 17 insertions(+), 17 deletions(-) (limited to 'gr-utils/python/utils/plot_fft_base.py') diff --git a/gr-blocks/python/blocks/qa_socket_pdu.py b/gr-blocks/python/blocks/qa_socket_pdu.py index 2e033491d3..961dc914ea 100644 --- a/gr-blocks/python/blocks/qa_socket_pdu.py +++ b/gr-blocks/python/blocks/qa_socket_pdu.py @@ -105,7 +105,7 @@ class qa_socket_pdu (gr_unittest.TestCase): # Test that the TCP server can stream PDUs <= the MTU size. port = str(random.Random().randint(0, 30000) + 10000) mtu = 10000 - srcdata = tuple([x % 256 for x in xrange(mtu)]) + srcdata = tuple(x % 256 for x in range(mtu)) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) @@ -125,7 +125,7 @@ class qa_socket_pdu (gr_unittest.TestCase): received = self.pdu_sink.get_message(0) received_data = pmt.cdr(received) msg_data = [] - for i in xrange(mtu): + for i in range(mtu): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data)) diff --git a/gr-digital/python/digital/qa_scrambler.py b/gr-digital/python/digital/qa_scrambler.py index 8de35e8ed0..93f77a9ef7 100644 --- a/gr-digital/python/digital/qa_scrambler.py +++ b/gr-digital/python/digital/qa_scrambler.py @@ -30,7 +30,7 @@ def additive_scramble_lfsr(mask, seed, reglen, bpb, data): out = [] for d in data: scramble_word = 0 - for i in xrange(0,bpb): + for i in range(0,bpb): scramble_word ^= l.next_bit() << i out.append(d ^ scramble_word) return tuple(out) @@ -122,7 +122,7 @@ class test_scrambler(gr_unittest.TestCase): reset_tag3 = gr.tag_t() reset_tag3.key = pmt.string_to_symbol(reset_tag_key) reset_tag3.offset = 20 - src = blocks.vector_source_b(src_data * 3, False, 1, (reset_tag1, reset_tag2, reset_tag3)) + src = blocks.vector_source_b([s * 3 for s in src_data], False, 1, (reset_tag1, reset_tag2, reset_tag3)) scrambler = digital.additive_scrambler_bb(0x8a, 0x7f, 7, 0, 8, reset_tag_key) dst = blocks.vector_sink_b() self.tb.connect(src, scrambler, dst) diff --git a/gr-qtgui/apps/gr_constellation_plot b/gr-qtgui/apps/gr_constellation_plot index fe5bdb56df..8ca7104a61 100755 --- a/gr-qtgui/apps/gr_constellation_plot +++ b/gr-qtgui/apps/gr_constellation_plot @@ -168,7 +168,7 @@ def main(): tb = my_top_block(filelist, args.start, nsamples, max_nsamples); main_box = plot_constellation_form(tb, 'GNU Radio Constellation Plot', 10000.0) - for n in xrange(tb._nsigs): + for n in range(tb._nsigs): main_box._style_edit[n].setCurrentIndex(0) main_box.show() diff --git a/gr-uhd/apps/uhd_fft b/gr-uhd/apps/uhd_fft index 4cb5eed0d1..777461fd63 100755 --- a/gr-uhd/apps/uhd_fft +++ b/gr-uhd/apps/uhd_fft @@ -218,7 +218,7 @@ class uhd_fft(gr.top_block, Qt.QWidget, UHDApp): self.qtgui_waterfall_sink_x_0.enable_grid(False) self.qtgui_waterfall_sink_x_0.disable_legend() alpha = 10.**(len(self.channels)-1) - for i in xrange(len(self.channels)): + for i in range(len(self.channels)): self.qtgui_waterfall_sink_x_0.set_line_label(i, "Channel {0}".format(i)) self.qtgui_waterfall_sink_x_0.set_color_map(i, 0) self.qtgui_waterfall_sink_x_0.set_line_alpha(i, alpha) @@ -240,7 +240,7 @@ class uhd_fft(gr.top_block, Qt.QWidget, UHDApp): self.qtgui_time_sink_x_0.enable_grid(False) self.qtgui_time_sink_x_0.enable_control_panel(True) self.qtgui_time_sink_x_0.disable_legend() - for i in xrange(2*len(self.channels)): + for i in range(2*len(self.channels)): if(i % 2 == 0): self.qtgui_time_sink_x_0.set_line_label(i, "Re{{Channel {0}}}".format(i/2)) else: @@ -268,7 +268,7 @@ class uhd_fft(gr.top_block, Qt.QWidget, UHDApp): self.qtgui_freq_sink_x_0.set_fft_average(self.fft_average) self.qtgui_freq_sink_x_0.enable_control_panel(True) self.qtgui_freq_sink_x_0.disable_legend() - for i in xrange(len(self.channels)): + for i in range(len(self.channels)): self.qtgui_freq_sink_x_0.set_line_label(i, "Channel {0}".format(i)) self.qtgui_freq_sink_x_0.set_line_width(i, widths[i]) self.qtgui_freq_sink_x_0.set_line_color(i, colors[i]) @@ -305,7 +305,7 @@ class uhd_fft(gr.top_block, Qt.QWidget, UHDApp): self.qtgui_phase_plot.enable_grid(True) self.qtgui_phase_plot.enable_control_panel(True) self.qtgui_phase_plot.disable_legend() - for i in xrange(len(self.channels) - 1): + for i in range(len(self.channels) - 1): self.qtgui_phase_plot.set_line_label(i, "Phase Delta Channels {0}/{1}".format(i, i+1)) self.qtgui_phase_plot.set_line_width(i, widths[i]) self.qtgui_phase_plot.set_line_color(i, colors[i]) diff --git a/gr-uhd/apps/uhd_rx_cfile b/gr-uhd/apps/uhd_rx_cfile index 2a1ed0fcb5..d41027ff39 100755 --- a/gr-uhd/apps/uhd_rx_cfile +++ b/gr-uhd/apps/uhd_rx_cfile @@ -79,7 +79,7 @@ class rx_cfile_block(gr.top_block): ) # Set the subdevice spec: if options.spec: - for mb_idx in xrange(self._u.get_num_mboards()): + for mb_idx in range(self._u.get_num_mboards()): self._u.set_subdev_spec(options.spec, mb_idx) # Set the antenna: if options.antenna is not None: @@ -125,7 +125,7 @@ class rx_cfile_block(gr.top_block): self._u.set_time_unknown_pps(uhd.time_spec()) cmd_time = self._u.get_time_now() + uhd.time_spec(COMMAND_DELAY) try: - for mb_idx in xrange(self._u.get_num_mboards()): + for mb_idx in range(self._u.get_num_mboards()): self._u.set_command_time(cmd_time, mb_idx) command_time_set = True except RuntimeError: @@ -136,7 +136,7 @@ class rx_cfile_block(gr.top_block): sys.stderr.write('[UHD_RX] [ERROR] Failed to set center frequency on channel {chan}\n'.format(chan=chan)) exit(1) if command_time_set: - for mb_idx in xrange(self._u.get_num_mboards()): + for mb_idx in range(self._u.get_num_mboards()): self._u.clear_command_time(mb_idx) print("[UHD_RX] Syncing channels...") time.sleep(COMMAND_DELAY) diff --git a/gr-utils/python/utils/gr_plot_qt b/gr-utils/python/utils/gr_plot_qt index cd2db87fd9..854ecc53b8 100755 --- a/gr-utils/python/utils/gr_plot_qt +++ b/gr-utils/python/utils/gr_plot_qt @@ -338,7 +338,7 @@ class gr_plot_qt(QtGui.QMainWindow): tstep = 1.0 / self.sample_rate self.iq = iq - self.time = [tstep*(self.position + i) for i in xrange(len(self.iq))] + self.time = [tstep*(self.position + i) for i in range(len(self.iq))] self.set_file_pos_box(start, end) except MemoryError: diff --git a/gr-utils/python/utils/plot_fft_base.py b/gr-utils/python/utils/plot_fft_base.py index c0651529ea..5040aefa87 100644 --- a/gr-utils/python/utils/plot_fft_base.py +++ b/gr-utils/python/utils/plot_fft_base.py @@ -93,7 +93,7 @@ class plot_fft_base(object): self.iq_fft = self.dofft(self.iq) tstep = 1.0 / self.sample_rate - #self.time = scipy.array([tstep*(self.position + i) for i in xrange(len(self.iq))]) + #self.time = scipy.array([tstep*(self.position + i) for i in range(len(self.iq))]) self.time = scipy.array([tstep*(i) for i in range(len(self.iq))]) self.freq = self.calc_freq(self.time, self.sample_rate) diff --git a/gr-utils/python/utils/plot_psd_base.py b/gr-utils/python/utils/plot_psd_base.py index 7c5138be4d..0a0f3cab34 100644 --- a/gr-utils/python/utils/plot_psd_base.py +++ b/gr-utils/python/utils/plot_psd_base.py @@ -103,7 +103,7 @@ class plot_psd_base(object): # returns a zero-length array if(len(self.iq) > 0): tstep = 1.0 / self.sample_rate - #self.time = scipy.array([tstep*(self.position + i) for i in xrange(len(self.iq))]) + #self.time = scipy.array([tstep*(self.position + i) for i in range(len(self.iq))]) self.time = scipy.array([tstep*(i) for i in range(len(self.iq))]) self.iq_psd, self.freq = self.dopsd(self.iq) diff --git a/gr-zeromq/python/zeromq/qa_zeromq_sub.py b/gr-zeromq/python/zeromq/qa_zeromq_sub.py index f916901ff7..d6006780dc 100755 --- a/gr-zeromq/python/zeromq/qa_zeromq_sub.py +++ b/gr-zeromq/python/zeromq/qa_zeromq_sub.py @@ -44,7 +44,7 @@ class qa_zeromq_sub (gr_unittest.TestCase): def test_001 (self): vlen = 10 - src_data = numpy.array(range(vlen)*100, 'float32') + src_data = numpy.array(range(vlen), 'float32')*100 zeromq_sub_source = zeromq.sub_source(gr.sizeof_float, vlen, "tcp://127.0.0.1:5555") sink = blocks.vector_sink_f(vlen) self.tb.connect(zeromq_sub_source, sink) @@ -60,7 +60,7 @@ class qa_zeromq_sub (gr_unittest.TestCase): vlen = 10 # Construct multipart source data to publish - raw_data = [numpy.array(range(vlen)*100, 'float32'), numpy.array(range(vlen, 2*vlen)*100, 'float32')] + raw_data = [numpy.array(range(vlen), 'float32')*100, numpy.array(range(vlen, 2*vlen), 'float32')*100] src_data = [a.tostring() for a in raw_data] zeromq_sub_source = zeromq.sub_source(gr.sizeof_float, vlen, "tcp://127.0.0.1:5555") sink = blocks.vector_sink_f(vlen) -- cgit v1.2.3