Skip to content

Commit

Permalink
Closes #5 Python 3.3/3.4 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewrankin committed Aug 7, 2014
1 parent a438b8d commit 591074c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ os:
python:
- '2.6'
- '2.7'
- '3.3'
- '3.4'
install:
- pip install -r requirements.txt
- pip install coveralls
Expand Down
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

### develop (unreleased)

### v0.2 (August 7, 2014)

#### Enhancements

- Made Python 3.3/3.4 compatible [#5][]

### v0.1 (August 7, 2014)

#### Enhancements

- Add Travis-CI testing [#1][]
- Create invoke tasks to automate PyPi deploy [#2][]
- Made PEP8 compliant [#3][], [#4][]

[#1]: https://github.com/questrail/sdfascii/issues/1
[#2]: https://github.com/questrail/sdfascii/issues/2
[#3]: https://github.com/questrail/sdfascii/issues/3
[#4]: https://github.com/questrail/sdfascii/issues/4
[#5]: https://github.com/questrail/sdfascii/issues/5
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[![PyPi Version][pypi ver image]][pypi ver link]
[![Coverage Status][coveralls image]][coveralls link]

A Python (2.6+) module for reading Standard Data Format (SDF) and ASCII
files saved by HP/Agilent Dynamic Signal Analyzers (DSA).
A Python (2.6+/3.3+) module for reading Standard Data Format (SDF) and
ASCII files saved by HP/Agilent Dynamic Signal Analyzers (DSA).

The HP/Agilent 35670A Dynamic Signal Analyzer has the ability to save
files as either SDF or ASCII format.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ numpy==1.8.1
pep8==1.5.7
pyflakes==0.8.1
pypandoc==0.8.2
six==1.7.3
34 changes: 23 additions & 11 deletions sdfascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Try to future proof code so that it's Python 3.x ready
from __future__ import print_function
# from __future__ import unicode_literals
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import

Expand All @@ -23,16 +23,22 @@
# Data analysis related imports
import numpy as np

__version__ = '0.1'
import six

__version__ = '0.2'

def _strip_nonprintable(input_string):

def _strip_nonprintable(input_bytes):
'''
(str) -> str
Return input_string without nonprintable characters
Return string without nonprintable characters
Actually this take a bytes object and converts it to a string only
returning any character up to but not including the first instance of
"\x00"
'''
return input_string.split('\x00', 1)[0]
return input_bytes.decode(errors="replace").split('\x00', 1)[0]


def _convert_sdf_unit_to_dictionary(sdf_unit):
Expand Down Expand Up @@ -88,8 +94,12 @@ def read_ascii_files(input_ascii_base_filename):
ydata = np.loadtxt(ascii_ydata_filename)

# Return the x and y data as a structured array
return np.core.records.fromarrays([xdata, ydata],
names='frequency,amplitude')
if six.PY2:
return np.core.records.fromarrays(
[xdata, ydata], names=b'frequency,amplitude')
elif six.PY3:
return np.core.records.fromarrays(
[xdata, ydata], names='frequency,amplitude')


def _decode_sdf_file_hdr(record_size, binary_data):
Expand Down Expand Up @@ -144,6 +154,9 @@ def _decode_sdf_meas_hdr(record_size, sdf_revision, binary_data):
Decode the measurement header binary data
'''
meas_hdr = {}
record_size_from_binary_data = struct.unpack('>l', binary_data[2:6])[0]
if record_size != record_size_from_binary_data:
sys.exit('Bad record size in SDF_MEAS_HDR')
meas_hdr['record_size'] = record_size
(meas_hdr['offset_unique_record'],) = struct.unpack(
'>1l', binary_data[6:10])
Expand All @@ -157,8 +170,7 @@ def _decode_sdf_meas_hdr(record_size, sdf_revision, binary_data):
meas_hdr['average_type'] = average_type_decoder[coded_average_type]
meas_hdr['average_num'], = struct.unpack('>l', binary_data[30:34])
meas_hdr['pct_overlap'], = struct.unpack('>f', binary_data[34:38])
meas_hdr['meas_title'] = _strip_nonprintable(
struct.unpack('>60s', binary_data[38:98])[0])
meas_hdr['meas_title'] = _strip_nonprintable(binary_data[38:98])
meas_hdr['video_bw'], = struct.unpack('>f', binary_data[98:102])
(meas_hdr['center_freq'], meas_hdr['span_freq'],
meas_hdr['sweep_freq']) = struct.unpack(
Expand Down Expand Up @@ -397,11 +409,11 @@ def read_sdf_file(sdf_filename):
with open(sdf_filename, 'rb') as sdf_file:
# Read SDF file_identfication
file_identifier = sdf_file.read(2)
if file_identifier == 'B\x00':
if file_identifier == b'B\x00':
sdf_hdr['valid_file_identifier'] = True
else:
# Didn't find a valid file identifer, so bail out
sys.exit('Did not find a valid file identifier.')
sys.exit('Invalid file identifier: {}'.format(file_identifier))

# Determine record type (short) and record size (long)
# Every record has these two special fields at the start
Expand Down
25 changes: 16 additions & 9 deletions tests/test_sdfascii.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# -*- coding: utf-8 -*-

# Try to future proof code so that it's Python 3.x ready
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import

import datetime
import os
import unittest
Expand All @@ -10,22 +17,22 @@

class TestReadingSDFFormat(unittest.TestCase):

def setUp(self):
source_10mVrms_3kHz_directory = os.path.join(
def setUp(self): # noqa
source_10mvrms_3khz_directory = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'source_10mVrms_3kHz')

sdf_file = os.path.join(source_10mVrms_3kHz_directory,
sdf_file = os.path.join(source_10mvrms_3khz_directory,
'SDF3KHZ.DAT')

ascii_ydata_file = os.path.join(source_10mVrms_3kHz_directory,
ascii_ydata_file = os.path.join(source_10mvrms_3khz_directory,
'ASCII3KH.TXT')

self.sdf_hdr, self.sdf_data = sdfascii.read_sdf_file(sdf_file)

self.ascii_ydata = np.loadtxt(ascii_ydata_file)

def tearDown(self):
def tearDown(self): # noqa
pass

def test_reading_sdf_file_identifier(self):
Expand Down Expand Up @@ -477,17 +484,17 @@ def test_ydata(self):

class TestReadingASCIIFormat(unittest.TestCase):

def setUp(self):
source_10mVrms_3kHz_directory = os.path.join(
def setUp(self): # noqa
source_10mvrms_3khz_directory = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'source_10mVrms_3kHz')

ascii_file_basename = os.path.join(source_10mVrms_3kHz_directory,
ascii_file_basename = os.path.join(source_10mvrms_3khz_directory,
'ASCII3KH')

self.ascii_data = sdfascii.read_ascii_files(ascii_file_basename)

def tearDown(self):
def tearDown(self): # noqa
pass

def test_starting_frequency(self):
Expand Down

0 comments on commit 591074c

Please sign in to comment.